How do I use grep to find a text string in files in sub folders of a parent folder without -r switch
The standard (POSIX) syntax is:
find /path/to/parent -type f -exec grep 'XXX' /dev/null {} +
(the /dev/null
is to make sure grep
always prints a file name). That will work on all POSIX systems including Solaris. The only known post-90s systems where that's known not to work is old (very old now) GNU systems.
GNU initially introduced a -print0
predicate and a -0
option to xargs
instead for that:
find /path/to/parent -type f -print0 | xargs -0 grep 'XXX' /dev/null
There are now a few other implementations that support that, but not Solaris.
Above, in theory, you'd want to add -r
option to avoid running grep
if there's not file, but that's not as portable and in this particular case, doesn't make a functional difference