21

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?

1
  • 1
    What Solaris release are you using ?
    – jlliagre
    Commented Dec 13, 2011 at 21:50

6 Answers 6

34

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.

4
  • You could also make use of the -name option with find. Commented Mar 3, 2017 at 19:56
  • @sleepyweasel -name won't search inside the files but only search the file names which is not what the OP asks for.
    – jlliagre
    Commented Mar 4, 2017 at 15:21
  • This doesn't show name of the file, when it is grepped due to how grep works. Do you know how to make it so grep shows filename ONLY when it find the pattern in it? I'm on Solaris 11.3, if it matters.
    – a_girl
    Commented Jun 17, 2020 at 17:13
  • @a_girl Can you clarify the issue you are experiencing? Unless there is a single file in the current directory, grep does show the name of the files where it finds the pattern in it.
    – jlliagre
    Commented Jun 18, 2020 at 5:00
7

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

1
  • 1
    At least on my system, ggrep is indeed GNU grep. Thanks!
    – jpaugh
    Commented Aug 26, 2016 at 12:55
4

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

3
  • --prefix=${HOME}}/.local is probably more advisable... no need to clutter the home directory with share and lib folders
    – jpaugh
    Commented Aug 26, 2016 at 12:53
  • 1
    One man's cluttered is another man's organized
    – bsd
    Commented Aug 26, 2016 at 19:47
  • No doubt! Honestly, I'm protecting against the off-day when I say, "share? That's got to be a junk directory!" rm -rf Doh!
    – jpaugh
    Commented Aug 26, 2016 at 21:27
4

Solaris grep does not have -r option.

You can use following command.

find . -type f | xargs grep 'sometext'
3
  • 2
    You don't need "control over the system" to compile GNU grep and install it somewhere under your home directory. Commented Dec 10, 2011 at 0:27
  • @KeithThompson thanks to point it out. I have corrected my answer. Commented Dec 10, 2011 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
    Commented Mar 30, 2015 at 4:17
3

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.

1

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.

2
  • 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. Commented Feb 27, 2013 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
    Commented Feb 28, 2013 at 14:40

You must log in to answer this question.

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