linux - Exclude .svn directories from grep

ID : 20135

viewed : 21

Tags : linuxbashsvngreplinux

Top 5 Answer for linux - Exclude .svn directories from grep

vote vote


If you have GNU Grep, it should work like this:

grep --exclude-dir=".svn" 

If happen to be on a Unix System without GNU Grep, try the following:

grep -R "whatever you like" *|grep -v "\.svn/*"  
vote vote


For grep >=2.5.1a

You can put this into your environment (e.g. .bashrc)

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

PS: thanks to Adrinan, there are extra quotes in my version:

export GREP_OPTIONS='--exclude-dir=.svn' 

PPS: This env option is marked for deprecation: "As this causes problems when writing portable scripts, this feature will be removed in a future release of grep, and grep warns if it is used. Please use an alias or script instead."

vote vote


If you use ack (a 'better grep') it will handle this automatically (and do a lot of other clever things too!). It's well worth checking out.

vote vote


psychoschlumpf is correct, but it only works if you have the latest version of grep. Earlier versions do not have the --exclude-dir option. However, if you have a very large codebase, double-grep-ing can take forever. Drop this in your .bashrc for a portable .svn-less grep:

alias sgrep='find . -path "*/.svn" -prune -o -print0 | xargs -0 grep' 

Now you can do this:

sgrep some_var 

... and get expected results.

Of course, if you're an insane person like me who just has to use the same .bashrc everywhere, you could spend 4 hours writing an overcomplicated bash function to put there instead. Or, you could just wait for an insane person like me to post it online:

vote vote


grep --exclude-dir=".svn" 

works because the name ".svn" is rather unique. But this might fail on a more generalized name.

grep --exclude-dir="work" 

is not bulletproof, if you have "/home/user/work" and "/home/user/stuff/work" it will skip both. It is not possible to define "/*/work/*" to restrict the exclusion to only the former folder name. As far as I could experiment, in GNU grep the simple --exclude won't exclude directories.

Top 3 video Explaining linux - Exclude .svn directories from grep