1

Looking for solaris command for getting list of all files containing search pattern (recursively). I know how to do it for linux but same command is not working in solaris:

bash-3.2# uname -a
SunOS D1NCIC-CL01 5.10 Generic_148888-03 sun4u sparc SUNW,Sun-Fire-15000

bash-3.2# find . -type f -print0 | xargs -0 grep -l "contentInFile"
xargs: illegal option -- 0
xargs: Usage: xargs: [-t] [-p] [-e[eofstr]] [-E eofstr] [-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-s size] [cmd [args ...]]
find: bad option -print0
find: [-H | -L] path-list predicate-list
Amit Anand
  • 85
  • 1
  • 1
  • 11
  • possible duplicate of [How do I grep recursively?](http://stackoverflow.com/questions/1987926/how-do-i-grep-recursively) – Joe Jan 01 '15 at 07:38
  • `bash-3.2# grep -r "include"` `grep: illegal option -- r` `Usage: grep -hblcnsviw pattern file . . .` i am looking for alternate command for solaris – Amit Anand Jan 01 '15 at 07:40

2 Answers2

6

As is so often the case when combined with find, xargs is useless here. You can run this portable command on both Solaris and Linux to get what you want:

find . -type f -exec grep -l "contentInFile" {} +
jlliagre
  • 29,783
  • 6
  • 61
  • 72
  • subquestion: what is the + for? – Beginner Feb 17 '15 at 15:51
  • @Beginner The `+` is there to perform the same argument aggregation as `xargs` does, but a simpler way, i.e. the `grep` command won't be called as much times as there are executable files but typically once. See http://stackoverflow.com/questions/6085156/using-semicolon-vs-plus-with-exec-in-find – jlliagre Feb 17 '15 at 20:14
1

If your filenames don't contain any whitespace, simply use -print and omit the -0 from xargs.

If they do, upgrade to Solaris 11, and use /usr/gnu/bin/find and /usr/gnu/bin/xargs (GNU Tools out of the box in Solaris 11).

Alternatively, if you're stuck on Solaris 10, install the GNU Find Utilities (How do I grep recursively?), or one of the alternative search tools suggested in How do I grep recursively? (Ag, ack).

Community
  • 1
  • 1
Joe
  • 29,416
  • 12
  • 68
  • 88
  • Search location can contain spaces in files/directory names. `bash-3.2# find . -type f -print | xargs grep -l "contentInFile"` `grep: can't open ./l2dashboard` `grep: can't open rules/syslog_cl1.rules` – Amit Anand Jan 01 '15 at 07:48