## Ignoring .svn directories with grep

At least a dozen times a day I need to find a bit of text in an unknown file that is buried deep in a tree of directories. If I'm on a Mac or Linux, grep is included and I use cygwin on all of my Windows installs. While it's a great tool, it's also annoying to get a screen full of results from Subversion's .svn directories.

One way to filter out those directories is to use the -v option which only shows lines that don't match a pattern.

grep -ri "your password is" * | grep -v .svn

Sure that works, but grep is still wasting time and processing power searching through all of the .svn directories. It's also more to type, and do u rly wnt 2 type more than u alrdy do? 1

You can use the --exclude option to skip processing any files or directories matching a specific pattern, but that's also more typing. Luckily nobody likes to waste keystrokes and grep looks for the GREP_OPTIONS environment variable which allows you to specify default options.

On a Mac open ~/.profile with a text editor. If you're using Linux, edit ~/.bashrc or ~/.bash_profile or ~/.profile.

Add the following line to the top of the file:

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

Save and close the file and re-open your session or type source ~/.bashrc.

If you're using Windows:

1. Open up the System Control panel (hold down the Start Menu key and press the Pause/Break key or Start>Run>sysdm.cpl).
2. Click on the Advanced tab and then click the Environment Variables button at the bottom of the window.
3. Under the User variables section click the New button.
4. Set Variable name to GREP_OPTIONS and Variable value to --exclude-dir=\.svn
5. Click OK three times and reopen any command prompts you had open.

The setting is effective in a command or bash prompt.

UPDATE Feb 2, 2009: Thanks to Iain I've been enjoying ack so much that I didn't realize that –exclude only works on the basename, meaning it won't exclude subdirectories. At long last grep 2.5.3 introduces the –exclude-dir option which works the way it should.

1. I feel dirty now. [back]

### 12 Responses to “Ignoring .svn directories with grep”

1. James Asher Says:

Actually, you have to add 'export GREP_OPTIONS="–exclude=\*.svn\*"' to the top of your .bashrc file.

2. Corey Says:

Good catch. I updated the post to reflect that.

3. Iain Says:

Or use ack; http://www.petdance.com/ack/

4. Corey Says:

OK, ack is truly awesome. On Windows if you'd like to invoke it by typing 'ack', download the stand-alone version of ack, and create a cmd script called ack.cmd (can be in the same directory as ack) in a directory that's in your PATH with this:

@perl c:\path\to\ack.pl %*

For cygwin you'll need to add an alias in your profile/bashrc file,

alias ack='ack.cmd'
5. Jonathan Hartley Says:

This still doesn't quite work right for me. I can't figure out why. Some (but not all) of the files in .svn directories are still included in the output. I'm using Cygwin under Windows XP.

I've added this to my ~/.profile:

  export GREP_OPTIONS="-I --color --exclude=\*.svn\*"


When I open a new cygwin window, I can see the env variable has been created properly:

  $echo$GREP_OPTIONS
-I --color --exclude=\*.svn\*


I enter a grep command:

  \$ grep -r LibraryDocumentation .


The results include lines like this one:

  ./Documentation/.svn/entries:LibraryDocumentation


Clearly 'entries' is inside an '.svn' directory. Why isn't it being filtered out?

Some files within '.svn' are filtered out, such as this, which appears in the output if I unset GREP_OPTIONS, but is removed if I set it:

  ./FunctionalTests/T0321_US_ThreeInstalls.py:        docPath = path.join("Documentation", "LibraryDocumentation")


The '-I' and '–color' options to grep are being honoured, so grep is seeing the 'GREP_OPTIONS' variable.

I'm most perplexed. If anyone has any clues or ideas, I'd be very grateful. Thanks!

Jonathan

6. Jonathan Hartley Says:

Ah, apologies, I lied slightly. The example I gave of a line that *is* correctly filtered out (starting './FunctiojnalTests/T0321…') is wrong. That line always appears in the output. The following is an example of a line that is correctly filtered out by the '–exclude' flag is:

  ./.svn/text-base/makedocs.py.svn-base:outputDir = os.path.join(os.getcwd(), 'Documentation', 'LibraryDocumentation')

7. Jonathan Hartley Says:

Sigh. One year one, I'm still googling for a solution to this, and all I can find are my own comments from last year. :-(

8. Corey Says:

@Jonathan: There's a new option, –exclude-dir in grep 2.5.3 (grep –version) that works as expected. I've updated the post accordingly.

9. Jonathan Hartley Says:

Hooray! (Which I understand is spelt '\o/' nowadays)

This now works brilliantly. Thanks heaps Corey.

Today I'm at an Ubuntu bash prompt. I hope Cygwin grep is >= v2.5.3…

10. Rob Marscher Says:

Came across this post today… for Mac users, even my Snow Leopard OS only came with grep 2.5.1 which doesn't have the –exclude-dir option. But you can install grep via macports which gives you 2.5.3+. Then following the instructions to add GREP_OPTIONS to .profile works.

11. raylu Says:

Thanks! This is life-changing.

12. Zvika Says:

Hi.
For user that cannot upgrade to the new 'grep' command, there is another option – use a 'grep wrapper', created by SVN team:

http://svn.apache.org/repos/asf/subversion/trunk/contrib/client-side/wcgrep