The Linux “wc” command is an abbreviation for word count. The command is used to count the number of lines, words, bytes, and even characters and bytes in a text file. In this tutorial, we look at the Linux “wc” command and demonstrate practical examples of its usage.
The Linux “wc” command takes the following syntax:
The “wc” command, in its basic form, displays the output in a columnar format, as shown in the snippet below.
Let’s briefly examine what each column represents:
Column 1: Displays the number of lines existing in the text file. As observed from the output, the file has seven lines. Be advised that this accounts for both the blank and the non-blank lines.
Column 2: This prints the word count.
Column 3: This displays the number of bytes in the file.
Column 4: This is the file name of the text file.
Pass Multiple Files as Arguments in One Command
The “wc” command can also take multiple files in one command and display the statistics of each file on a separate file. Suppose you have two files, like in our example where we have two text files — fruits.txt and vegetables.txt.
Instead of using the “wc” command twice to view the statistics of each file, you can use the following syntax to accept both files as arguments.
For our example, to count the number of lines, words, and bytes in each file, run the following command:
$ wc fruits.txt vegetables.txt
From the output, you can see that the “wc” command displays the output of both files in a columnar output. The result of each file is placed on a separate row, and the very last row provides the total count for the lines, words, and bytes of both files.
Count the Number of Lines Only in a File
Let’s consider the text file hello.txt that we started with. Let’s recap the line, word, and character count as follows:
The -l option is used to count the number of lines only.
Print Word Count Only in a File
To display the word count in a text file, use the -w option as follows. This is relatively straightforward, and as you can see, it counts the number of words only contained in the file.
Count the Number of Bytes only in a File
To print the number of bytes only in a file, use the -c option as provided in the command below:
Count the Number of Characters Only in a File
Additionally, you can count the number of characters by passing the -m option as provided in the command below:
For more command options and usage of the “wc” command, be sure to check the man pages:
If you are interested in checking the version, simply invoke the simple command:
The Linux “wc” command is a really simple and easy-to-use command that gives you a clue on the number of lines, words, bytes, and characters contained in a file. For any queries, do get in touch. We will endeavor to give a prompt response.