2

I am working on an ancient UNIX whose grep lacks the -r/--recursive option.

I need to find an error that our application is causing, but I do not know which log file our application is writing errors to. However, I do know that the log file is somewhere in /opt. So I want to find FooErrorMessage under /opt in *.log. Here's what I tried:

find /opt | xargs grep FooErrorMessage

-- but this does not work, and I don't know where to specify that I just want *.log files in the command.

Brighid McDonnell
  • 389
  • 1
  • 8
  • 20

9 Answers9

5

You're just trying to find all log files under /opt and search them for somethnig_I_am_looking_for right? Why not:

find /opt -name '*.log' | xargs grep something

or

find /opt | grep .log | xargs grep something

?

Oh, and since I can't figure out how to comment on the other answers: be careful with *.log as the shell will interpret that as globbing, and match all files in the current directory that end in .log . You should use either \*.log or '*.log'

atk
  • 217
  • 1
  • 3
4

Beware of filenames with spaces in them, as it is allowed by Unix; xargs will choke on them. Use find /opt -name '*.log' ... -print0 | xargs -0 grep ..., these options are meant to go together.

Also, grep or your shell might have a limited number of arguments, and the list of files returned by find might exceed that. Use the -n number option of xargs to run grep by chunks of number files.

  • +1 for '`find ... -print0 | xargs -0 ...`'. The operating system will limit the total length of the arguments (and the size of the environment also comes out of the same space - so lots of environment variables reduces the number of arguments you can use), but `xargs` should be attuned to the o/s limit and not create overlong argument lists. Certainly, I don't recall that being a problem ever. – Jonathan Leffler Jul 31 '09 at 05:04
3

do you have perl?

try -- http://betterthangrep.com/

johnh
  • 595
  • 4
  • 9
1

find /opt -type f -name *.log -print | xargs grep 'something_I_am_looking_for'

Chad Huneycutt
  • 2,116
  • 1
  • 16
  • 14
  • You need quotes around '`*.log`' to be safe. (And, as written, you do not need quotes around '`something_I_am_looking_for`', but it is likely better to include them than omit them - certainly in general. – Jonathan Leffler Jul 31 '09 at 05:01
1

Apart from adding the "-name '*.log'", try adding -n1 to xargs.

Maybe your ancient grep can't take multiple input files, so -n1 will run grep for each of those files, instead of passing them as arguments at once

Jorge Bernal
  • 454
  • 1
  • 3
  • 9
1

You can also do this with the exec option to find instead of xargs

find /opt -exec grep "thing you are looking for" {} \;

Alex
  • 6,603
  • 1
  • 24
  • 32
1

GNU grep has its own “recurse” option:

 grep -R foo /path/to/directory

To grep files of a particular pattern, you’re best off with find and xargs:

 find /path/to/directory -name \*.log -print0 | xargs -0 grep foo
Mo.
  • 2,236
  • 17
  • 9
0
find /opt -name *.log | xargs grep yourstring 
Sven
  • 98,649
  • 14
  • 180
  • 226
0

Unless you have subdirectories that are many levels deep, grep *, grep / and grep //* is easy to remember and hard to type wrong...

igor
  • 121
  • 1
  • 3