I'm very familiar with using find coupled with -exec, in fact it's become one of my personal favorite BASH commands because of how cool it looks visually and how powerful and useful it can be.
I need to find a string contained within certain files on a VPS, the directory structure is far too massive for me to want to manually go through and find which files contain the string, so I figure its a perfect job for find -exec grep.
The full syntax of the command I have is as follows:
find ./ -type f -name "*.*" -exec grep "section for more information." {} \;
...and this works great for confirming that this string is in fact found within some files......but which ones? I would love to know if there is a syntax to display which files contain the string, preferably with the full path to them, although I guess it's not mandatory.
Thanks in advance!
POSIX grep
will output the filename only when you specify multiple filenames. The typical trick is therefore to add /dev/null
to ensure there's always more than 1:
find ./ -type f -name "*.*" -exec grep "for more information." /dev/null {} \;
The GNU and BusyBox grep
s additionally has a -H
you could use:
-H, --with-filename
Print the file name for each match. This is the default
when there is more than one file to search.
Alternatively, if you don't care about the matched lines themselves and just want the filename:
find ./ -type f -name "*.*" -exec grep -q "for more information." {} \; -print