Recursive grep on Solaris

The grep utility that is included with Solaris 10 does not appear to support recursive behaviour, unlike many flavours of Linux which do so using the -r switch. Until recently I had been using something like:

$ grep 'somestring' `find . -name '*'`

in an attempt to emulate a recursive grep but this doesn’t work too well when you run it from a high-level directory on your system (the size of the output from the find becomes too large for grep to handle).

However, I recently found a much better alternative:

$ find . | xargs grep 'somestring'

This is shorter and much easier to remember, although from initial observations, it seems to take a little longer to run but I can live with that since it works.

12 thoughts on “Recursive grep on Solaris”

  1. Thank YOU!! I have been looking all over for a simple description of a recursive grep I could use. It worked GREAT!!

    I used the following on SunOS 5.9 and got exactly what I needed. Thank you thank you. So many web sites did not have a simple, straightforward cmd.

    find . | xargs grep -li ‘diana’

  2. Agreed with Diana. Thank you SO much for this post! Couldn’t find it so simply anywhere else! Have it bookmarked for sure.

  3. Great discovery! I had looked for a while to find a recursive grep for solaris and your example of using find with xargs works wonderfully. Much thanks!

  4. xargs is an enemy in disguise if you at a later date get files with “interesting” names:

    touch important_file
    touch ‘not important_file’
    ls not* | xargs rm

    mkdir -p ’12” records’
    ls | xargs rmdir

    Parallel does not cause these issues.

  5. Bless you for sharing this command! It’s 2013, we’re still using SunOS 5.8 in places, and this syntax is still useful!

Leave a Reply