The Linux wc command (word count)

The Linux word count command is named wc. The wc command counts the number of characters, words, and lines that are contained in a text stream. If that sounds simple or boring, it's anything but; the wc command can be used in Linux command pipelines to do all sorts of interesting things.

Let's take a look at some Linux wc command examples to show the power of this terrific little command.

Linux wc command examples (words, lines, characters)

In its most basic use, the wc command can be used to count the number of characters, words, and lines in a file, like this:

$ wc /etc/passwd
      65     185    3667 /etc/passwd

If you just want to know the number of lines in a file just add the -l argument, like this:

$ wc -l /etc/passwd
      65 /etc/passwd

Or, if you want to know the number of words in a file, add the -w argument, like this:

$ wc -w MyStory.txt
     185 MyStory.txt

Using the Linux wc command in command pipelines

The wc command follows the paradigm of reading input from STDIN and writing output to STDOUT, so it can be used in all sorts of Linux command pipelines. This command shows the number of users currently logged into your Linux system:

who | wc -l

It does that by piping the output of the who command into the input of the wc command, which in this case is used to count the number of lines of output in the who command.

Similarly, this next command shows the number of processes currently running on your Linux system:

ps -e | wc -l

This works the same way as the previous example: Generate output using one command (the ps command), and use the wc -l command to count the number of lines of output from that command.

The Linux who command - Summary

I hope these Linux who command examples have been helpful. If you have any questions, or any examples you'd like to share, just use the Comment form below.

Post new comment

The content of this field is kept private and will not be shown publicly.