Tuesday, July 14, 2009

Recursive Grepping

I recently had some need to search for certain text within files in a directory. This works with eclipse, but sometimes it can be slow and a bit limited in options. So I turned to good old grep. I wanted to exclude certain directory patterns from the search, but the problem I ran into was that the version of grep packaged with Fedora 10 and available in the yum repository is an older version (2.5.1) and does not include the "--exclude-dir" option. So I had to grab a recent copy of the source. I then did a ./configure, make, make install and I was good to go with version 2.5.4. The last tricky part for me was how to match multiple filename patterns using GLOB. Here is an example of the final command:

grep -R "apache.commons." . --include={*.xml,*.ent} --exclude-dir="output"

No comments: