6

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.

15

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

-1

Using GNU grep (if installed):

ggrep -A3 -B23 'word' -R /log/

This will check every files in folder if the regular expression word is matched and will show output together with file path.

GNU grep supports the -R flag.

-1

Simply use ggrep which on solaris exists on /usr/sfw/bin/ggrep path like this:
/usr/sfw/bin/ggrep -H 'search-term' <file-name>*
it searches your search term on a group of file names starting with a specific word.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.