Putorius
Basic Commands

wc Command - Count Output by Lines, Words, or Characters

wc command in linux

The Linux wc command it one of the many text utilities that come packaged with the GNU Core Utilities. It allows you to print a count of words, lines, characters, or even bytes in a file or standard input (STDIN). In this article we will show you the basics of using the wc command, and include some real world examples.

Jump to how to count lines, words, characters, bytes or using multiple input files.

Using the wc Command

To use the wc command, you simply invoke the command followed by the file you want to use as input.

[savona@putor ~]$ wc Harrison_Bergeron.txt 
  318  2222 12925 Harrison_Bergeron.txt

When you use wc without any options it will print three different numbers followed by the name of the input file. The first column shows the numbers of lines in the file. The second columns is the word count. The third column is the character count.

There are a few options to select which output you want to see. Let's take a look at some of the available options and how to use them.

Count the Number of Lines in a File

With the -l (--lines) option, you can display only the line count. So if we wanted to count the number of lines in our text file, we pass the -l option followed by the name of the file.

[savona@putor ~]$ wc -l Harrison_Bergeron.txt 
318 Harrison_Bergeron.txt

NOTE: It's important to understand that the wc command is counting newline characters, not actual lines in the terminal.

Count Number of Words in a File

Similarly, you can print the number of words in a file using the -w (--words) option.

[savona@putor ~]$ wc -w Harrison_Bergeron.txt 
2222 Harrison_Bergeron.txt

Count the Number of Characters in a File

You can also count the number of characters in a file using the -m (--chars) option like so:

[savona@putor ~]$ wc -m Harrison_Bergeron.txt 
12925 Harrison_Bergeron.txt

NOTE: Spaces count as characters.

Print the Byte Count for a File

Optionally, you can print the byte count for a file using the -c (--bytes) option.

[savona@putor ~]$ wc -c Harrison_Bergeron.txt 
12925 Harrison_Bergeron.txt

In most ASCII text files the byte count will equal the character count.

Print the Length of the Longest Line in a File

A rarely used option is -L (--max-line-length). It will print the number of characters in the longest line of the file.

[savona@putor ~]$ wc -L Harrison_Bergeron.txt 
79 Harrison_Bergeron.txt

This shows us the longest line in the file has 79 characters.

Print Word, Line, or Character Counts for Multiple Files with the wc Command

You can get the counts for multiple files by using the --files0-from option. In my opinion this option is convoluted. To use it you must create a file which lists the input files you want to run the count on. That doesn't sound too bad, but each file name must be terminated with a NUL character.

You can't just type a NUL character. For example, if you want to create a NUL character in vim, you need to hit CTRL+V then CTRL+SHIFT+@. This is what a file with NUL terminated file names looks like:

[savona@putor ~]$ cat -A Short-Stories.txt 
Harrison_Bergeron.txt^@Exhalation.txt^@

NOTE: A detailed explanation of NUL characters are outside the scope of this tutorial. Maybe an idea for a future article?

Once you have your file with a list of NUL-terminated filenames, you can count the words like so:

[savona@putor ~]$ wc -w --files0-from=Short-Stories.txt 
 2222 Harrison_Bergeron.txt
 6505 Exhalation.txt
 8727 total

The reason I think this is convoluted is because this can be easily achieved by using other commands. For example, you can use the cat command to concatenate (combine) the two files, then pipe the output to wc. This is much easier and delivers the same end result. Admittedly, it doesn't show you the word count for each individual file, only the total.

[savona@putor ~]$ cat Harrison_Bergeron.txt Exhalation.txt  | wc -w
8727

Real World Examples of the wc Command

As a Linux administrator I use wc fairly often. Here are some examples of how I used it recently (pulled from my bash history).

Find how many gnome-shell-extensions are available in my DNF repositories:

[savona@putor ~]$ sudo dnf search gnome-shell-extension* | wc -l
45

How many files are in my Pictures directory:

[savona@putor ~]$ ls -l ~/Pictures/ | wc -l
222

Count the number of if statements in my bash script:

[savona@putor ~]$ grep if $(which putorius) | wc -l
41

Conclusion

The wc command is a very useful tool. It is pre-installed on almost all Linux system as part of the core-utils package. It can be used in many different ways and provides a simple method for counting all kinds of output (or input depending on how you look at it).

If you have any interesting uses for the wc command we would love to hear about them. Sound off in the comments below!

Resources and Links

Exit mobile version