Exclude .svn directories from grep

LinuxBashSvnGrep

Linux Problem Overview


When I grep my Subversion working copy directory, the results include a lot of files from the .svn directories. Is it possible to recursively grep a directory, but exclude all results from .svn directories?

Linux Solutions


Solution 1 - Linux

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/*" 

Solution 2 - Linux

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: https://www.gnu.org/software/grep/manual/html_node/Environment-Variables.html "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."

Solution 3 - Linux

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.

Solution 4 - Linux

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:

http://gist.github.com/573928

Solution 5 - Linux

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.

Solution 6 - Linux

On my GNU grep 2.5, --exclude-dirs is not a valid option. As an alternative, this worked well for me:

grep --exclude="*.svn-base"

This should be a better solution than excluding all lines which contain .svn/ since it wouldn't accidentally filter out such lines in a real file.

Solution 7 - Linux

Two greps will do the trick:

  1. The first grep will get everything.

  2. The second grep will use output of first grep as input (via piping). By using the -v flag, grep will select the lines which DON'T match the search terms. Voila. You are left with all the ouputs from the first grep which do not contain .svn in the filepath.

    -v, --invert-match Invert the sense of matching, to select non-matching lines.

grep the_text_you_want_to_search_for * | grep -v .svn

Solution 8 - Linux

I tried double grep'in on my huge code base and it took forever so I got this solution with the help of my co-worker

Pruning is much faster as it stops find from processing those directories compared to 'grep -v' which processes everything and only excludes displaying results

find . -name .svn -prune -o -type f -print0 | xargs -0 egrep 'YOUR STRING'

You can also alias this command in your .bashrc as

alias sgrep='find . -name .svn build -prune -o -type f -print0 | xargs -0 egrep '

Now simply use

sgrep 'whatever' 

Solution 9 - Linux

Another option, albeit one that may not be perceived as an acceptable answer is to clone the repo into git and use git grep.

Rarely, I run into svn repositories that are so massive, it's just impractical to clone via git-svn. In these rare cases, I use a double grep solution, svngrep, but as many answers here indicate, this could be slow on large repositories, and exclude '.svn' occurrences that aren't directories. I would argue that these would be extremely seldom though...

Also regarding slow performance of multiple greps, once you've used something like git, pretty much everything seems slow in svn!

One last thing.., my variation of svngrep passes through colorization, beware, the implementation is ugly! Roughly grep -rn "$what" $where | egrep -v "$ignore" | grep --color "$what"

Solution 10 - Linux

For grep version 2.5.1 you can add multiple --exclude items to filter out the .svn files.

$ grep -V | grep grep
grep (GNU grep) 2.5.1

GREP_OPTIONS="--exclude=*.svn-base --exclude=entries --exclude=all-wcprops" grep -l -R  whatever ./

Solution 11 - Linux

I think the --exclude option of recursion is what you are searching for.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKees KistView Question on Stackoverflow
Solution 1 - LinuxpsychoschlumpfView Answer on Stackoverflow
Solution 2 - LinuxosgxView Answer on Stackoverflow
Solution 3 - LinuxBrian AgnewView Answer on Stackoverflow
Solution 4 - LinuxMax CantorView Answer on Stackoverflow
Solution 5 - LinuxkaratedogView Answer on Stackoverflow
Solution 6 - Linuxuser193130View Answer on Stackoverflow
Solution 7 - LinuxJoseph HuttnerView Answer on Stackoverflow
Solution 8 - LinuxSid SarasvatiView Answer on Stackoverflow
Solution 9 - LinuxquickshiftinView Answer on Stackoverflow
Solution 10 - LinuxZaSterView Answer on Stackoverflow
Solution 11 - LinuxPatrice BernassolaView Answer on Stackoverflow