Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude .svn directories from grep [duplicate]

Tags:

linux

grep

bash

svn

People also ask

How do I delete a .SVN folder recursively?

The find command returns a list of all the subfolders matching “. svn”, and this is then piped to the rm command to recursively delete the directory. Running rm using the full path will remove the confirmation prompt, and the “rf” arguments will recursively delete any folder contents.


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

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."


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.


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