(Reading time: 1 minute)

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

Linux

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
 
 
 

 

 

Oxygen
Author: Oxygen
Other articles by this author

Chillzee Tag Cloud

Let's Socialize

About Chillzee

Chillzee.com is an entrepreneurship portal.

The site provides informative topics on Organizational and Strategic needs.