Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

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
up vote 4 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

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

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.

export PATH

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

share|improve this answer

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

Solaris grep does not have -r option.

You can use following command.

find . -type f | xargs grep 'sometext'
share|improve this answer
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
This is very useful to find all files matching a name pattern and show results with the file. e.g. find /opt/ -name "*.sh" -type f | xargs grep "start" – Underverse Mar 30 '15 at 4:17

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:


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


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. :-) – peterh Feb 28 '13 at 14:40

Your Answer


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.