Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count occurrences of a word in all the files of a directory?

I’m trying to count a particular word occurrence in a whole directory. Is this possible?

Say for example there is a directory with 100 files all of whose files may have the word “aaa” in them. How would I count the number of “aaa” in all the files under that directory?

I tried something like:

 zegrep "xception" `find . -name '*auth*application*' | wc -l  

But it’s not working.

like image 570
Ashish Sharma Avatar asked May 26 '11 07:05

Ashish Sharma


People also ask

How do I count how many times a word appears in a file?

You can use grep command to count the number of times "mauris" appears in the file as shown. Using grep -c alone will count the number of lines that contain the matching word instead of the number of total matches.

How do you count all lines in all files in a directory?

The easiest way to count files in a directory on Linux is to use the “ls” command and pipe it with the “wc -l” command. The “wc” command is used on Linux in order to print the bytes, characters or newlines count.

How do you count the number of matches in grep?

The grep command has the -c flag, which will count the number of lines matched and print out a number.


1 Answers

grep -roh aaa . | wc -w

Grep recursively all files and directories in the current dir searching for aaa, and output only the matches, not the entire line. Then, just use wc to count how many words are there.

like image 93
Carlos Campderrós Avatar answered Sep 22 '22 18:09

Carlos Campderrós