Recursive grep

Contributor Icon Contributed by roshi Date Icon October 7, 2004  
Tag Icon Tagged: UNIX

This tip is for grepping a pattern even in the sub-directories of a particular directory in addition to the files in the current directory.

Since grep -R <> <> is not available on all flavors, this can be really helpful.


The command is as follows :-

find -name | xargs grep

The arguments are self-explanatory.
The output of find will be xarg‘ed to the input of grep
and these will be searched for searchstring.

Previous recipe | Next recipe |
 
  • htihan
    Thank you so much,

    It is one of those days.. I spent almost an hour for this.....

    Best, peace...
    Hal
  • Anonymous
    that's great and all, but you dont get filenames
  • seacoder
    find . -name "*" -exec grep <pattern> {} /dev/null ;
  • Anonymous
    find /users/ -type f -name "*" -exec grep -ls sqlload {} ;
    It gives output like this:
    /users/hattb/shirley_test_file

    The -type f switch tells it to only look in text files.
    If you want to redirect the output to a file:
    find /users/hattb -type f -name "*" -exec grep -ls sqlload {} ; >out.txt

    Hope this helps,
    Shirley
  • Anonymous
    It's so great. Thanks.
  • Anonymous
    find|xargs grep 'search text'

    is easier to type :-)

    xargs rocks.
  • Anonymous
    For those of you with a really stripped down config with no xargs and a find that doesn't support -exec, the old fashioned way:

    for i in `find <path>`; do grep <pattern> $i; done
  • Lee
    This is a fantastic tip, thank you!
blog comments powered by Disqus