Linux Commands

Linux “wc” Command

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.

Basic Syntax

The Linux “wc” command takes the following syntax:

$ wc [OPTION] [FILE]

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.

$ wc file1.txt file2.txt

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:

$ wc hello.txt

The -l option is used to count the number of lines only.

$ wc -l  hello.txt

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.

$ wc -w  hello.txt

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:

$ wc -c  hello.txt

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:

$ wc -m  hello.txt

For more command options and usage of the “wc” command, be sure to check the man pages:

$ man wc

If you are interested in checking the version, simply invoke the simple command:

$ wc --version

Conclusion

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.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.