Grep Recursively Through Single File Extension

You can use the following grep command to search through a directory, recursively, but only looking at a specific file pattern.

grep -r --include=<pattern> <string> <directory>

Here is an example that searches recursively starting at /home/joel/ and including only php files.

grep -r --include=*.php "public auction" ./

Other options you might want to add to the command:

-i Case insensitive match.
-c Count of matches; suppressing normal output. One line per file match.
-o Show only the part of a matching line that matches.

Here's another example with the -r, -i and -o options all added.

grep -rio --include=*.php "public auction" ./
 
linux/grep_recursively_through_single_file_extension.txt · Last modified: 2011/03/22 14:51 by Joel Dare
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki