The wc Command

The wc command is a simple and useful Linux command. It stands for “word count” and it can be used to show the number of lines and words in a file.

Basic Syntax

The basic syntax of the wc command is to use it with a filename.

wc file.txt

By default the wc command outputs 3 numbers:

  1. number of lines
  2. number of words
  3. number of bytes

Flags

-l

If you only wanna see the number of lines, you can use this flag.

wc -l file.txt

-w

If you only wanna see the number of words, you can use this flag.

wc -w file.txt

-m

If you wanna see the number of characters, you can use this flag

wc -m file.txt

Piping Other Commands

Aside from counting lines or words in a file, you can also pipe other commands into the wc command, to count their output.

For instance if you wanna know the number of files and directories in /var/log/ you can run the ls command on it and then pipe the output into the wc command with the -l flag to count the number of lines in the output of the ls command.

ls /var/log/ | wc -l

Since each file or directory in the output of the ls command is considered a new line, by counting the number of lines, we’re also counting files and directories.

You can pipe the output of the find command into the wc command when you wanna know how many files match a specific criteria. For instance if you wanna know the number of HTML files in the current directory and all sub-directories, you can use:

find . -name “*.html” | wc -l

Similarly, if you wanna know the number of files larger than 10 MB, you can use:

find . -size +10M | wc -l

In this article, I went over the most common use cases of the wc command. As you can see, the wc command is useful on its own and also can be very powerful when used with other commands.