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 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 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.

I hope these Unix/Linux wc command examples have been helpful.

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

Add new comment

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

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image.