How do I use grep to find a text string in files in sub folders of a parent folder without -r switch

48,937

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

Share:
48,937

Related videos on Youtube

DemiSheep
Author by

DemiSheep

Computer geek turned computer scientist.

Updated on September 18, 2022

Comments

  • DemiSheep
    DemiSheep over 1 year

    I'm using Solaris 10 and have two grep versions one in /usr/bin and one in /usr/xpg4/bin. I have been searching for an answer on how to search for text within files within sub folders of a parent directory using grep. All answers talk about -r or -R switches which I do not have available with my version of grep.