Coder Perfect

grep [duplicate] exclude.svn directories


I get a lot of files from the.svn directories when I grep my Subversion working copy directory. Is it feasible to grep a directory recursively while excluding all results from.svn directories?

Asked by Kees Kist

Solution #1

It should function like this if you have GNU Grep:

grep --exclude-dir=".svn"

If you’re using a Unix system that doesn’t include GNU Grep, try the following:

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

Answered by psychoschlumpf

Solution #2

For grep >=2.5.1a

This is something you can implement in your environment (e.g. .bashrc)

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

PS: In my version, due to Adrinan, there are a few extra quotes:

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

PPS: The following env option has been tagged as deprecated: node/Environment-Variables.html “This feature will be removed in a future release of grep, and grep will warn if it is used, because it presents complications when developing portable scripts. Instead, please use an alias or a script.”

Answered by osgx

Solution #3

If you use ack (a ‘better grep’), it will handle this for you (along with a slew of other useful features!). It’s definitely worth a look.

Answered by Brian Agnew

Solution #4

Psychoschlumpf is right, but only if you have the most recent version of grep will it function. The —exclude-dir option was not available in earlier versions. Double-grep-ing, on the other hand, can take an eternity if you have a vast codebase. Put this in your.bashrc file to make it portable. grep svn-less:

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

You can now accomplish the following:

sgrep some_var

… and receive the intended outcomes.

Of course, if you’re like me and have to use the same.bashrc everywhere, you could spend 4 hours building an overly convoluted bash function to put there instead. You could also just wait for a lunatic like me to post it online:

Answered by Max Cantor

Solution #5

grep --exclude-dir=".svn"

Because the name “.svn” is so unique, it works. However, on a broader scale, this could fail.

grep --exclude-dir="work"

It will skip both “/home/user/work” and “/home/user/stuff/work” if you have “/home/user/work” and “/home/user/stuff/work.” It is not feasible to use the expression “/*/work/*” to limit the exclusion to the former folder name. As far as I could tell, the simple —exclude option in GNU grep does not exclude folders.

Answered by karatedog

Post is based on