How To Count Words, Lines, of A File with wc command In Linux/Unix?


While working with files especially with text files in Linux operating system sometimes you may need to count the number of words, number of lines, total character, etc. Linux/Unix provides wc command which is a simple and powerful command-line utility that makes this task easier. In this article, we will see it in more detail along with some examples.

wc command

The wc stands for word count. This command takes either standard input or a list of files and displays newline count, word count, and byte count along with the file name which was passed as the argument. GNU wc used to be the part of the textutils package but now it is the part of the GNU Coreutils package. The newer version of wc can differentiate between byte and character count.

Syntax of wc command

wc [Options][File(s)]

Options used with wc command

OptionsDescription
-l or --linesThis will print the number of lines present in a file
-w or --wordsThis will print the number of words in a file
-c or --bytesPrints the counts of bytes present in a file
-m or --charPrint the number of characters in a file
-L or --max-line-lengthPrint the maximum display width
--helpdisplay the help and exit
--versiondisplay the version and exit

Usage with examples

Let us consider we have three text files rpm-based.txt, debian-based.txt, and independent.txt which contains some text in it. The description of these files is given below.

rpm-based.txt

Red Hat Linux
CentOS
Fedora
openSUSE
Clear Linux
Mandrake Linux

debian-based.txt

BackTrack
Kali Linux
Parsix
PureOS
Astra Linux
Ubuntu

independent.txt

Alpine Linux
Android
Bilix
Coyote Linux
CRUX
Dragora GNU/Linux-Libre
ELinOS
Familiar Linux
Foresight Linux
Guix System Distribution
GoboLinux
Lunar Linux
Nitix
Puppy Linux

1. Passing a single filename to wc command without using any options-

wc rpm-based.txt

Now look at the output of the command there are four columns here the first column shows the number of lines in the file i.e. 6, the second column shows the number of words in the file i.e.10, the third column shows the number of characters in the file i.e. 64  and the last column shows the name of the file.

2. Using multiple filenames as the input to wc command-

wc rpm-based.txt debian-based.txt independent.txt

If you pass multiple files the output will contain an extra row which contains the total number of lines, total number of words, and the total number of characters in all the three files.

3. For Counting the number of lines in a file you have to use -l option with wc command look at the example below-

wc -l rpm-based.txt


4. Use the following command to count the words in a file –

wc -w rpm-based.txt


5. To count the characters in a file use the following command –

wc -m rpm-based.txt

6. To display the number of bytes in a file use the following command –

wc -c rpm-based.txt

7. Use the following command to display the length of the longest line in a file –

wc -L rpm-based.txt

8. Count the number of files and directories under a directory –

Using ls command will display the files and directories under the current working directory. Look at the image that is given below, It shows the files and directories inside the home directory –

ls

Now run the following command to count the number of files and directory –

ls | wc -l

For more information regarding usages of wc command and its options, use the following command in your terminal.

man wc

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.