Which command can be used to count the number lines, words, and characters in a file?

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.

Which command can be used to count the number lines, words, and characters in a file?

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.

Which command can be used to count the number lines, words, and characters in a file?

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

Which command can be used to count the number lines, words, and characters in a file?

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:

Which command can be used to count the number lines, words, and characters in a file?

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

Which command can be used to count the number lines, words, and characters 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.

Which command can be used to count the number lines, words, and characters in a 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:

Which command can be used to count the number lines, words, and characters in a file?

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:

Which command can be used to count the number lines, words, and characters in a file?

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

Which command can be used to count the number lines, words, and characters in a file?

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

Which command can be used to count the number lines, words, and characters in a file?

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

Which command can be used to count the number lines, words, and characters in a file?

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.

What command counts the number of lines words and characters in a text file?

Use the wc command to count the number of lines, words, and bytes in the files specified by the File parameter. If a file is not specified for the File parameter, standard input is used. The command writes the results to standard output and keeps a total count for all named files.

How do you count words lines and characters in a file in Linux?

wc stands for word count. As the name implies, it is mainly used for counting purpose. It is used to find out number of lines, word count, byte and characters count in the files specified in the file arguments.

What command is used to count the total number of lines words and characters in a file in Linux?

The most easiest way to count the number of lines, words, and characters in text file is to use the Linux command “wc” in terminal. The command “wc” basically means “word count” and with different optional parameters one can use it to count the number of lines, words, and characters in a text file.

Which command is used to count number of lines?

Use wc –lines command to count the number of lines. Use wc –word command to count the number of words. Print the both number of lines and the number of words using the echo command.