Recursive grep

Posted by roshi in 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 search string.

Here’s additional examples:

find -maxdepth 1 -type f -print0 | xargs grep -F ‘example’
#Will Search all regular files for ‘example’ in current dir

find -type f -print0 | xargs -r0 grep -F ‘example’
#Will Search all regular files for ‘example’ in current dir and below

The Conversation

Follow the reactions below and share your own thoughts.

One Response to “Recursive grep”

  1. December 17, 2008 at 10:07 pm, Lee said:

    This is a fantastic tip, thank you!

    Reply

Leave a Reply

You may also like-

7-Zip: How to list contents of a folder and subfolder of zip files without extracting (Win ...7-Zip: How to list contents of a folder and subfolder of zip files without extracting (Win ...This tech-recipe will explain how to use 7-Zip to create a text file (tab delimited) listing of the contents of a zip file.  You ... UNIX recursive directory listingUNIX recursive directory listingListing the contents of a directory and all subdirectories, a recursive listing, is sometimes useful. This recipe describes some techniques for listing files recursively ...