3

How can I do recursive grep on Solaris?

When I tried, I received this error:

-r: invalid option.

5 Answers 5

7

Recursive grep on Solaris:

find . -name "*.[chix]" | xargs grep -i -n pattern_to_search
4
  • +1 That's what I use as well
    – Andy White
    Apr 29, 2011 at 19:55
  • That won’t work if the filenames have spaces in them.
    – tchrist
    Apr 29, 2011 at 20:09
  • @tchrist In that case, we will use: find . -name "*.[chix]" | xargs grep -i -n "pattern_to_search"
    – Sandeep Singh
    Apr 29, 2011 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, 2011 at 18:32
2

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

2
  • Didn't know about ack before. Thanks.
    – ubiyubix
    Apr 29, 2011 at 20:18
  • Will break on spaces in filenames.
    – user7385
    Apr 30, 2011 at 18:31
1

-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.

1

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" *
0
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.

3
  • That's very bad idea, as it will call grep for every file separately - which means slow. very slow.
    – user7385
    Apr 30, 2011 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, 2011 at 13:29
  • After reading your answer, I quite like the "+" thing.
    – ubiyubix
    Apr 30, 2011 at 13:36

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.