Wednesday, March 07, 2007

How to ignore .svn directories in grep?

This has bothered me for a while. A google search shows two popular implementations to work around this: using grep's --exclude parameter or use find to filter out the hidden directories. I chose the --exclude parameter approach since it is pretty neat and easy to integrate into my own bash environment by defining an alias.

Here is the alias definition in my ~/.bash_profile

alias g="egrep --exclude=\*.svn\* -r -n "

9 comments:

Anonymous said...

Thanks for the post; after typing "| grep -v svn" for the 100th time this is just what I needed.

Tony said...

I couldn't get your script to ignore DIRECTORIES. I came up with a bash function and instructions on how to install here. Let me know what you think.

Oskar said...

Thanks for this neat little trick!

Anonymous said...

amazing! thanks !

house9 said...

Thanks!

very helpful

gabriel barros said...

exclude works on file name only. you need exclude-dir

also, grep does all you need and is more available.

alias g='grep --color=always -n --exclude-dir=\*.svn\*'

Nicolas_Raoul said...

Thanks!
Greping through .svn directories takes more time and makes finding results difficult.
Before your tip, I was using a piped "grep -v .svn" but it wastes time and removes terminal colors because of the pipe.

Bottomless said...

You can also set the GREP_OPTIONS in your .bashrc, details here:
http://blog.bottomlessinc.com/2008/06/ignore-svn-directories-in-grep-searches/

Vinny said...

I prefer setting GREP_OPTIONS="--exclude-dir=.svn" in ~/.bashrc