How to find all files containing specific text

in «tip» by Michael Beard
Tags: , , , ,

The link, How do I find all files containing specific text on Linux? shows how to use grep to accomplish this.

It also shows the typical find command that is recommended, which, like the poster, I didn't get to work for me either, so ...

The answer I used is by rakib_ and is

grep -rnw '/path/to/somewhere/' -e 'pattern'

This lets me find something not using find, but grep, which I remember much better.

For completeness, the typical find command is:

find / -exec grep -H 'text-to-find-here' {} \;

find . -exec zgrep -aiH 'text-to-find-here' {} \;

This usually ends up just printing a line for every file it finds and doesn't ever seem to find anything.

Though, if I use this:

find . -type f -exec fgrep -H 'text-to-find-here' {} \;

that seems to work. It seems that the secret sauce is adding the -type f, as it makes the others above work as well. So, there you go! Multiple ways to do the same thing.

It does seem that the find command takes longer to execute than the grep command, but I'm not sure if that's just perception or actuality.