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

Ashish Sharma picture Ashish Sharma · May 26, 2011 · Viewed 87.8k times · Source

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.

Answer

Carlos Campderrós picture Carlos Campderrós · May 26, 2011

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.