The Linux word count command is named
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 lines, words, and characters in a file, like this:
$ wc /etc/passwd 65 185 3667 /etc/passwd
In that example, the /etc/passwd file has 65 lines, 185 words (as
wc determines words), and 3,667 characters.
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
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.
I hope these Unix/Linux
wc command examples have been helpful.