Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I do recursive grep on Solaris?

When I tried, I received this error:

-r: invalid option.
share|improve this question

migrated from Apr 29 '11 at 22:58

This question came from our site for professional and enthusiast programmers.

Recursive grep on Solaris:

find . -name "*.[chix]" | xargs grep -i -n pattern_to_search
share|improve this answer
+1 That's what I use as well – Andy White Apr 29 '11 at 19:55
That won’t work if the filenames have spaces in them. – tchrist Apr 29 '11 at 20:09
@tchrist In that case, we will use: find . -name "*.[chix]" | xargs grep -i -n "pattern_to_search" – Sandeep Singh Apr 29 '11 at 20:17
it will still break on some filenames. The only safe way to do it with find and xargs is to use: find . -type f -print0 | xargs -0 grep ... – user7385 Apr 30 '11 at 18:32

find . | xargs grep whatsyrexpression or use cpan to install the ack command.

share|improve this answer
Didn't know about ack before. Thanks. – ubiyubix Apr 29 '11 at 20:18
Will break on spaces in filenames. – user7385 Apr 30 '11 at 18:31

-r option for grep works only with gnu grep. Solutions with xargs are good, but cause some problems - find | xargs grep will break on filenames with spaces, and besides - xargs is also gnu tool, so it might be not installed.

As far as I know, the proper way to do it on solaris is:

find . -type f -exec grep ... {} +
  • at the end means that there will be many files passed to every grep call so it will be relatively fast.

Also, note that solaris (well, unix) grep doesn't have (for example) -E option, and you should use egrep for it.

share|improve this answer

If you are lucky, you have gnu grep installed also. It will then be named "ggrep".

ggrep is usually located in /usr/sfw/bin/ggrep if it is installed. Use the -H -R -I flags: -H to show the filename, -R for recursive search, -I to ignore binary files.

Example: Show all lines in all files, except binary files, from this directory down including all subdirectories with the word "excel"

/usr/sfw/bin/ggrep -H -R -I "excel" *
share|improve this answer
find . -type f -exec grep hello {} /dev/null \;

This will also work for filenames with spaces. Why /dev/null? Because each grep instance will inspect a single file at a time and therefore doesn't print the filename if it finds a match. That's fine if you are really grepping a single file only, but doesn't help if grep is repetitively called from find. The additional /dev/null serves as an extra dummy file to search so that grep will prepend the current filename when it prints the matching line.

share|improve this answer
That's very bad idea, as it will call grep for every file separately - which means slow. very slow. – user7385 Apr 30 '11 at 12:44
I know. One answer suggested to use xargs which will call grep for multiple files at a time. However, this does not work if filenames have spaces in them. So, one solution works for filenames without spaces only, the other solution is slower but does work in this case. Thanks for downvoting. – ubiyubix Apr 30 '11 at 13:29
After reading your answer, I quite like the "+" thing. – ubiyubix Apr 30 '11 at 13:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .