Home About

Bash Shell Tips - wc

Created //

Tags // linux, shell, scripts, bash

The bash wc command

Use the wc command to count things.

This all revolves around piping something (the content of a file, the results of another command, etc.) into the unix/linux wc command.

You can use wc to count words (wc -w), lines (wc -l), or characters (wc -c).

Count the number of files in a folder and subfolders

$ find . -type f | wc -l

Count the number of words in a text file

$ cat filename.txt | wc -w

Count the number of lines in a text file

$ cat filename.txt | wc -l

Count the number of times the word "todo" appears in a file

$ grep -i "todo" filename.ext | wc -l

Count the number of files containing the word "todo" in this folder, or subfolders

$ grep -lir "todo" | wc -l

Count how many unique email addresses have committed to a git repo

(Assumes you're in a folder with a git repo.)

$ git log --format="%ae" | sort | uniq | wc -l

Learn how to use wc

Remember, most unix/linux commands already have documentation built into the operating system.

$ man wc

... will show the documentation for wc.

// ka

You might also be interested in...