0

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?

1 Answer 1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .