How would you count every occurrence of a term in all files in the current directory?

-1

How would you count every occurrence of a term in all files in the current directory? - and subdirectories(?)

I've read that to do this you would use grep; what is the exact command?

Also, is it possible to the above with some other command?

TellMeWhy

Posted 2015-11-06T14:16:58.653

Reputation: 101

Answers

2

Easy, just pipe it to wc:

grep -Ro "searchTerm" . | wc -w

-R means recursive, -o means it will return only the matching words. Then you pipe it into wc (wordcount) -w means it will count words. Might be a bit trickier if the pattern you are searching for includes spaces, in which case they have to be escaped.

Note that the -w option will return the number of matches, so you have N matches on 1 line, it will return N, not 1.

ventsyv

Posted 2015-11-06T14:16:58.653

Reputation: 262