If you work at all in Solaris (and maybe just older versions I don’t know) you will eventually find out that the version of “grep” shipped with the OS is missing the “-r” option for doing recursive greps. Over the years I have found myself becoming a huge fan of this ability and I am always amazed to find there are versions that don’t include it. Today I set out to come up with a way to emulate the behavior.
I came up with two solutions. First we have:
find . |xargs grep PATTERN
While this seemed to work fine at first I noticed that it had issues with file that contained spaces. It would treat each “word” of a file name with spaces as a separate filename and I would get errors from grep about not being able to find the file. If you know that you don’t have any filenames with spaces though the above should work just fine.
As I played around further I managed to come up with the following second iteration:
find . -exec grep PATTERN {} /dev/null \;
This one worked just as well as the first one but had the added advantage of treating file names with spaces in the correctly. I don’t claim to be a find master. Unfortunately I can’t tell you why the “/dev/null” is required except that I can tell you without it you will get the lines in files matching the pattern returned but not the filename the line is contained in.