Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

On our Solaris server I'm finding that grep -r doesn't work, which is the usual way that I use grep. It seems that egrep has the same behavior.

Given that I have no control over the machine, is there a way to have grep perform a recursive search? Or do I need to pipe the output of find into grep?

share|improve this question
    
What Solaris release are you using ? –  jlliagre Dec 13 '11 at 21:50
add comment

6 Answers

up vote 3 down vote accepted

I'm not familiar with Solaris, but if you're switching from GNU/linux to Solaris you'll find most of the commands will behave slightly different. The GNU version of Unix tools have additional features missing in "proprietary" Unixes.

You can download GNU grep(s) here then compile and install.

If you do have root access you might want to run configure --prefix=/usr/local Such that the utilities install into /usr/local

If you do not have root access you might want to run configure --prefix=${HOME} Such that the utilities install into your home directory

share|improve this answer
add comment

If you want to use find, this would be the fastest way:

find . -type f -exec grep pattern {} +

However, Gnu grep is very likely already installed on your machine.

It is part of the default installation on Solaris 11 in /usr/gnu/bin/grep. Same for Solaris 10 where you find it in /usr/sfw/bin/ggrep. On older releases, it might have been installed from sunfreeware or other repositories, check /usr/local/bin/grep and /opt/csw/bin/grep for common locations.

share|improve this answer
add comment

Download the standalone ack perl script from http://betterthangrep.com/, put it in your $HOME/bin, and learn to use it instead. I find it is, as advertised, better than grep for many uses in my daily development work.

share|improve this answer
add comment

Use ggrep instead if it is installed.

E.g. Search for "mysearchstring" from the current directory down to all subdirectories (-R), and show the file name and path for matches (-H) and ignore binary files (-I). Don't forget the * on the end of the command.

/usr/sfw/bin/ggrep -H -R -I "mysearchstring" *

ggrep is located in /usr/sfw/bin You can add it to your profile with an EXPORT command so you can call it from the command line directly.

PATH=$PATH:/usr/sfw/bin
export PATH

Solaris version: SunOS 5.10 Generic_147440-13 sun4u sparc SUNW,SPARC-Enterprise

share|improve this answer
add comment

Solaris grep does not have -r option.

You can use following command.

find . -type f | xargs grep 'sometext'
share|improve this answer
1  
You don't need "control over the system" to compile GNU grep and install it somewhere under your home directory. –  Keith Thompson Dec 10 '11 at 0:27
    
@KeithThompson thanks to point it out. I have corrected my answer. –  Sachin Divekar Dec 10 '11 at 4:07
add comment

Yes, you will need the GNU grep. Personally I do not miss the "-r" flag because you can do the same with a combination of find and grep but this reminds that having the GNU tools available on your Solaris box is what I would consider best practice. So here we go:

I wouldn't go about downloading source code from various places on the Internet and then building yourself. That is not necessary. If you simply want your Solaris to have the typical GNU tools available then do as follows:

If you are on Solaris 10 you should get the Solaris 10 Companion Disk. This used to be distributed by Sun but it now lives here. Personally I would make it a habbit always to install the contents of this disk or at least the most important GNU parts. Make it part of your JumpStart (or whatever you use) for new servers in your organisation !

If you are on Solaris 11 life is a lot simpler. The GNU tools are typically installed by default but do not all of them get propagated into local zones. IFS, the packaging system on Solaris 11, makes your life a lot easier so you really should be moving to Solaris 11 (lots of other reasons as well). The list of packages that you should consider always to have available for any zone are these:

pkg://solaris/archiver/gnu-tar
pkg://solaris/developer/gnu-binutils
pkg://solaris/diagnostic/top
pkg://solaris/file/gnu-coreutils
pkg://solaris/file/gnu-findutils
pkg://solaris/text/gawk
pkg://solaris/text/gnu-diffutils
pkg://solaris/text/gnu-grep
pkg://solaris/text/gnu-sed

and if you use the host for development/build purpose you might want to add:

pkg://solaris/developer/build/gnu-make
pkg://solaris/developer/build/make
pkg://solaris/developer/gcc-45
pkg://solaris/system/header
pkg://solaris/developer/build/autoconf
pkg://solaris/developer/build/automake-110

Personally I would make the above (potentially less the dev tools) part of my company's default install for new servers / zones. Solaris sysadmins often insist on being different from Linux world but there's really no need to. Just do it and make it part of your default install. You won't regret it.

share|improve this answer
    
I'm not sure if you read the part where I said 'given that I have no control over the machine ...' I'm sure this is useful information to someone, but it is hardly an answer to the question that I asked. –  Eric Wilson Feb 27 '13 at 13:18
    
It is information for your sysadmin. He has given you a crippled machine IMHO. These things should be installed by default. But yes, you are right, the information was perhaps just as much intended for others that come to this place and read your question. Today you're missing GNU grep, tomorrow you'll be missing GNU tar, etc. You just don't know it yet. :-) –  nolan6000 Feb 28 '13 at 14:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.