Tuesday, January 5, 2010

How do I grep (i.e. search for a string) recursively through subdirectories on UNIX?

Excluding .svn subversion directories
# find . -not -path '*/.svn/*' -type f -print | xargs grep -I -n -e 'pattern' > result.txt

# cat result.txt

use -l option for grep command to show matched file name only.

# find . \! -path '*/.svn/*' -type f -print | xargs grep -I -n -e 'pattern'

To tell find to exclude .svn directories, use the -prune option:
# find . -path '*/.svn/*' -prune -o -type f -print | xargs grep -I -n -e 'pattern'
==========================================
How do I grep (i.e. search for a string) recursively through subdirectories on UNIX?

# grep -r "modules" .

This searches through all files starting from the current directory down. Note that this includes non-text files.

# find . | xargs grep some_pattern

This will restrict your search to certain file names or file types.
# find . -name "*.txt" | xargs grep some_pattern
================================
Searching for a String in Multiple Files

Ever need to search through all your files for a certain word or phrase? I did, and to make matters more complicated, the files were all in different sub-directories. A quick Google search turned up a few scripts involving multiple commands and temporary files. Then I found a simpler solution.

If you're a Unix/Linux/BSD user, you probably know about the grep command, but did you know it's recursive? That's right, grep can search through your entire directory tree, scanning every file along the way. It will even return the results in multiple formats!

Here's an example. In this case we're searching for the word "modules":

grep -r "modules" .

By using the "-r" switch, we're telling grep to scan files in the current directory and all sub-directories. It will return a list of files the string was found in, and a copy of the line it was found on.

If you'd rather just get the file names and skip the rest of the output, use the "-l" switch, like so:

grep -lr "modules" .

Here's another tip: grep also supports regular expressions, so matching against a wildcard pattern is easy:

grep -lr "mod.*" .

That command will print a list of files containing any word starting with "mod".

You can also use grep to search for multiple words:

grep -r "drupal\|joomla\|wordpress" .

And, of course, grep supports file name wildcards in the standard unix fashion. In this example, grep will search only file names starting with "log":

grep -lr "mod.*" ./log*

Unfortunately, not all versions of grep support recursive searching, and some use different switches, so if you're running into problems, check your man pages:

man grep

All commands listed in this tutorial have been tested on the latest version of FreeBSD.
Posted by John on 2008-02-05

No comments: