
grep returns .svn metadata files
What You'll Need:
grep 2.5.3+
I can't count how many times a day I use grep. It is an indispensable tool for searching the contents of configuration files, data files, and source code.
One of the annoying side effects of grep is when searching a Subversion working copy recursively, all the hidden .svn directories also get searched. This can yield a lot of false positive matches since these directories contain "pristine copies of all version-controlled files".
If you've got grep 2.5.3 or higher, it's easy to ignore .svn directories with the "--exclude-dir" option:
$ grep -r --exclude-dir='.svn' 'my search string' /some/directory
But that's a lot to type in each time you want to search your source code. To save keystrokes, try setting up an alias in your home directory's .bashrc file that utilizes the --exclude-dir option (for Mac, use your home directory's .profile file.). I call mine devgrep:
alias devgrep="grep --exclude-dir='.svn'"
After adding the alias, don't forget to source your .bashrc:
$ source ~/.bashrc
Now whenever you need to search a Subversion working copy recursively, use the new alias to skip .svn directories:
$ devgrep -rin 'doctype' /var/www/html/templates ./foo.tpl:1:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> ./bar.tpl:1:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> ./baz.phtml:1:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
You can also exclude file types when grep'ing. If you're a vim user, it can be helpful to skip .swp files in your alias:
alias devgrep="grep --exclude-dir='.svn' --exclude='*.swp'"
Yet another workaround would be to use ack - a Perl based, juiced up version of grep with a lot more features.
Great tip thanks!
What about when you want to exclude multiple directories?
Do you have to type --exclude-dir multiple times or is there another way?
Example:
$ grep -r --exclude-dir='.svn' --exclude-dir='debug' --exclude-dir='templates' 'my search string' /some/directory
@Chris AFAIK, yes, your example is the easiest way to do that.