(Reading time: 1 minute)

Linux - How to count total number of occurrences of a string in a file?


The following command counts and displays the total number of occurrences of teh string 'string' in the file 'filename'.

grep -o 'string' filename | wc -l


The above command will count even the sub-strings. If you would like to count only full words then you can also use the following command.

grep -o '\bstring\B' filename | wc -l



Author: Oxygen
Other articles by this author

Add comment

Security code

Similar articles

Chillzee Tag Cloud

Let's Socialize

About Chillzee

Chillzee.com is an entrepreneurship portal.

The site provides informative topics on Organizational and Strategic needs.