Cyberithub

How to count number of words in a File from Linux Terminal

Advertisements

In this article, we will see how to count number of words in a File from Linux Terminal. It is often required to count number of words in a file to verify if the count is according to the requirement. You might have encountered use cases to create a document or a report with some minimum number of words or may be with some fixed number of words or sometimes with a maximum word limit. All these requires you to regularly check the number of words regularly to comply with the requirement. Although there are many GUI utilities are also available to perform this task but here we will mainly focus on performing this task through terminal.

How to count number of words in a File from Linux Terminal

How to count number of words in a File from Linux Terminal

Also Read: How to Install pronterface package on Ubuntu 20.04 LTS (Focal Fossa)

Advertisements

There are multiple ways to count number of words in a file from terminal in which one of the most preferred one is through wc command. wc is a free and open source utility in Linux which is used to count words, lines and characters. So let's suppose if you have a file called words.txt and you are looking to count the number of words in that file then you need to use wc -w words.txt command as shown below.

cyberithub@ubuntu:~$ wc -w words.txt
1253 words.txt

Similarly, if you want to count the number of lines in words.txt file then you need to use wc -l words.txt command as shown below.

Advertisements
cyberithub@ubuntu:~$ wc -l words.txt
109 words.txt

Likewise, if you want the count the number of characters in the file then you need to use wc -m words.txt command as shown below.

cyberithub@ubuntu:~$ wc -m words.txt
8118 words.txt

And, if you want all the information in a single command then you just need to use wc words.txt command as shown below.

Advertisements
cyberithub@ubuntu:~$ wc words.txt
109 1253 8118 words.txt

In case if you want to count the total number of words from multiple files then you can do that as well using wc command. For example, to count total number of words in both words.txt and example.py file, you can use wc -w words.txt example.py command as shown below.

cyberithub@ubuntu:~$ wc -w words.txt example.py
1253 words.txt
108  example.py
1361 total

Similarly, to count the total number of lines in both the files, you need to use wc -l words.txt example.py command as shown below.

Advertisements
cyberithub@ubuntu:~$ wc -l words.txt example.py
109 words.txt
12  example.py
121 total

Finally, to count the total number of characters, use wc -m words.txt example.py command as shown below.

cyberithub@ubuntu:~$ wc -m words.txt example.py
8118 words.txt
851  example.py
8969 total

Like earlier, you can also summarize all the counts from words.txt and example.py file by using wc words.txt example.py command as shown below.

cyberithub@ubuntu:~$ wc words.txt example.py
109 1253 8118 words.txt
12  108  851  example.py
121 1361 8969 total

Another command that you can use to perform the same task is awk. You can also use awk utility to count number of words in a file. For example - to count the number of words in the same file words.txt, you need to use awk '{num+=NF} END{print num+0}' words.txt command as shown below. You may notice that the output of awk command is exactly the same as wc command.

cyberithub@ubuntu:~$ awk '{num+=NF} END{print num+0}' words.txt
1253

If you are looking to count the number of occurrences of a specific word then grep command can be effectively used to perform that task. For example, here I am counting the number of occurrences of dataset word in words.txt file by using grep -c "dataset" words.txt command as shown below.

cyberithub@ubuntu:~$ grep -c "dataset" words.txt
3

You can also use single quote instead of double quote to count the occurrences.

cyberithub@ubuntu:~$ grep -c 'dataset' words.txt
3

Leave a Comment