Linux “wc” Command

8

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:

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.

linux “wc” command Linux “wc” Command 1622792410 731 Linux wc Command

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

linux “wc” command Linux “wc” Command 1622792410 739 Linux wc Command

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:

linux “wc” command Linux “wc” Command 1622792410 646 Linux wc Command

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

linux “wc” command Linux “wc” Command 1622792411 239 Linux wc Command

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.

linux “wc” command Linux “wc” Command 1622792411 333 Linux wc Command

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:

linux “wc” command Linux “wc” Command 1622792412 111 Linux wc Command

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:

linux “wc” command Linux “wc” Command 1622792412 950 Linux wc Command

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

linux “wc” command Linux “wc” Command 1622792413 180 Linux wc Command

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

linux “wc” command Linux “wc” Command 1622792413 699 Linux wc Command

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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More