Exclude .svn directories from grep
Solution 1
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
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
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
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:
Solution 5
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.
Related videos on Youtube
Kees Kist
Updated on February 28, 2021Comments
-
Kees Kist over 3 years
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?
-
Ciro Santilli OurBigBook.com over 7 years
-
-
gen_Eric over 13 yearsOn the Windows version of GNU Grep, I had to use
--exclude-dir=\.svn
-
Noufal Ibrahim over 13 years+1 for ack. I have a shell alias and use it by default.
-
osgx over 13 yearsOnly for gnu grep version >=2.5.1a
-
osgx over 13 yearsOnly for gnu grep version >=2.5.1a
-
coderintherye over 13 yearsThe method you list above using find and xargs does not work, at least on RHEL5 in my environment. I still get .svn-base listings.
-
Darragh Enright over 12 yearsha. I was just about to upvote but I had already. seems I was here before :)
-
Dave over 12 yearsThis should be a comment to the accepted answer, not an answer in itself.
-
karatedog over 12 years@Dave: Thanks for the tip. You have just commented on a 2 year old comment.
-
Chandranshu over 12 yearsThe second grep removes all color formatting.
-
rymo over 12 years
--exclude
only matches file patterns, like*.java
-
jperelli over 12 years+1 for the second example, the first didn't work for me with GNU grep 2.6.3 using
export GREP_OPTIONS="--exclude-dir=\".svn\" -nR --color"
-
Adrian Gunawan about 12 yearsYou don't need to have double quote - it didnt work for me when the double quote is present
-
Andy Lester about 12 yearsAnother problem is that it will remove any lines that happen to have ".svn" in them, whether or not the .svn is because of the filename, or is actually in the file. Another problem is that you're spending the time searching the files in the .svn directory that you know you're going to exclude. Better to --exclude from the beginning.
-
José Tomás Tocino almost 12 yearsReally nice that ack. Careful that, in Ubuntu and Linux Mint, the "ack" package has nothing to do with the "ack - better grep", which is located in the "ack-grep" package.
-
Shane over 10 years
alias grepsvn='grep --exclude-dir=".svn"'
grepsvn -R searchstring .
-
spaaarky21 about 10 yearsThis would also waste time finding matches in .svn only to filter them after the fact.
-
user193130 over 8 yearsAs @osgx said,
exclude-dir
doesn't work on my GNU grep 2.5. Instead, the following works well for me as all the accidental matches are within the *.svn-base files:grep --exclude="*.svn-base"
-
dwanderson over 8 yearsWas doing it the latter way, but that lost
--color
support, which made me sad.--exclude-dir
gets me what I need, and color-coded. -
Hi-Angel almost 8 yearsThat unfortunately didn't solve problem with all hidden dirs (e.g. I don't want it to descend to
.stack-work
dir), also I didn't quite like that its output takes more space by having filenames on a separate line. So I end up making an alias to grepalias grepexclhid="grep --exclude-dir=\".*\""
. -
Hi-Angel almost 8 yearsAn alias to exclude all dot-starting dirs:
alias grepexclhid="grep --exclude-dir=\".*\""
. One might even want to make the alias name to begrep
, then to use it without an alias you'd have to type full path/usr/bin/grep
. -
shjeff over 5 yearsHave you noticed difference, this is
--exclude-dir
no--exclude-dirs
? -
yukashima huksay over 3 yearsGREP_OPTIONS stopped being a supported environment variable after 2.20