Glossary

wc

wc counts lines, words, characters, and bytes in its input. Without flags, it prints all four counts in that order; with -l, -w, -c, or -m, it prints only the requested count.

wc file.txt                       # lines, words, bytes
wc -l file.txt                    # line count
wc -l < file.txt                  # line count, no filename
ls | wc -l                        # count files in current dir
find . -type f | wc -l            # count all files recursively
grep -c pattern file              # same as grep | wc -l

wc -l is one of the first shell tricks newcomers learn, and for good reason: it turns any enumerable output into a count. "How many users have bash as their shell?" grep /bin/bash /etc/passwd | wc -l. "How many lines of Python in this project?" find . -name "*.py" | xargs wc -l | tail -1.

One subtlety: wc counts terminated lines by counting newlines. A file ending without a newline still has its content counted but the last "line" does not increment -l. This can occasionally mislead you by a count of one.

Related terms: grep, cut

Discussed in:

Also defined in: Textbook of Linux