Skip .svn directories when using grep

grep returns .svn metadata

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" "">
./baz.phtml:1:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "">
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.


  1. Chris

    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?

    $ grep -r --exclude-dir='.svn' --exclude-dir='debug' --exclude-dir='templates' 'my search string' /some/directory

  2. @Chris AFAIK, yes, your example is the easiest way to do that.

  3. I have this in my ~/.bashrc:

    export GREP_OPTIONS="--exclude-dir=.svn --exclude-dir=.git"

  4. Thomas Hunter

    $ grep --exclude-dir='.git'
    grep: unrecognized option `--exclude-dir=.git'

  5. @Thomas - exclude-dir is available with grep 2.5.3+. You can check your grep version with grep -V

Leave a Comment