3
3
How can I do recursive grep on Solaris?
When I tried, I received this error:
-r: invalid option.
3
3
How can I do recursive grep on Solaris?
When I tried, I received this error:
-r: invalid option.
7
Recursive grep on Solaris:
find . -name "*.[chix]" | xargs grep -i -n pattern_to_search
2
find . | xargs grep whatsyrexpression
or use cpan
to install the ack
command.
Didn't know about ack before. Thanks. – ubiyubix – 2011-04-29T20:18:35.187
Will break on spaces in filenames. – None – 2011-04-30T18:31:36.837
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" *
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 ... {} +
Also, note that solaris (well, unix) grep doesn't have (for example) -E option, and you should use egrep
for it.
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.
That's very bad idea, as it will call grep for every file separately - which means slow. very slow. – None – 2011-04-30T12:44:45.200
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 – 2011-04-30T13:29:01.857
After reading your answer, I quite like the "+" thing. – ubiyubix – 2011-04-30T13:36:00.647
+1 That's what I use as well – Andy White – 2011-04-29T19:55:11.870
That won’t work if the filenames have spaces in them. – tchrist – 2011-04-29T20:09:03.853
@tchrist In that case, we will use: find . -name "*.[chix]" | xargs grep -i -n "pattern_to_search" – None – 2011-04-29T20:17:36.180
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 ... – None – 2011-04-30T18:32:45.323