Linux WC Command Examples to Count Number of Lines, Words, Characters

Operating SystemLinuxMCA

The wc command also known as word count command is a fundamental command any Linux user should be aware of. It is mostly used with the l switch to do a line count, but it can actually give count of multiple things with various arguments supplied to it. Below is a list of what are those possible arguments. The we will see examples on each of them.

Sr.NoCommandUsage
1wc -cDisplay number of bytes
2wc -mDisplay number of characters
3wc -wDisplay number of words
4wc -lDisplay number of lines
5wc -LDisplay length of longest line

Lets consider the below file for our examples.

ubuntu@ubuntu:~$ cat inspire.txt
Mastering anything needs practice.
It aslo needs patience.
And it needs time and other resources.

-c Option

Display the number of bytes in the file.

ubuntu@ubuntu:~$ wc -c inspire.txt

Running the above code gives us the following result:

98 inspire.txt

-m Option

Display the number of characters in the file.

ubuntu@ubuntu:~$ wc -m inspire.txt

Running the above code gives us the following result −

98 inspire.txt

-w Option

Display the number of words in the file.

ubuntu@ubuntu:~$ wc -w inspire.txt

Running the above code gives us the following result −

15 inspire.txt

-l Option

Display the number of lines in the file.

ubuntu@ubuntu:~$ wc -l inspire.txt

Running the above code gives us the following result:

3 inspire.txt

-L Option

Display the length of longest line in the file.

ubuntu@ubuntu:~$ wc -L inspire.txt

Running the above code gives us the following result −

38 inspire.txt

Only wc

Display the number of lines, words, and characters one after the other.

ubuntu@ubuntu:~$ wc inspire.txt

Running the above code gives us the following result −

3 15 98 inspire.txt
raja
Published on 03-Jan-2020 11:35:37
Advertisements