Exclude .svn directories from grep

61,040

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:

http://gist.github.com/573928

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.

Share:
61,040

Related videos on Youtube

Kees Kist
Author by

Kees Kist

Updated on February 28, 2021

Comments

  • Kees Kist
    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?

  • gen_Eric
    gen_Eric over 13 years
    On the Windows version of GNU Grep, I had to use --exclude-dir=\.svn
  • Noufal Ibrahim
    Noufal Ibrahim over 13 years
    +1 for ack. I have a shell alias and use it by default.
  • osgx
    osgx over 13 years
    Only for gnu grep version >=2.5.1a
  • osgx
    osgx over 13 years
    Only for gnu grep version >=2.5.1a
  • coderintherye
    coderintherye over 13 years
    The 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
    Darragh Enright over 12 years
    ha. I was just about to upvote but I had already. seems I was here before :)
  • Dave
    Dave over 12 years
    This should be a comment to the accepted answer, not an answer in itself.
  • karatedog
    karatedog over 12 years
    @Dave: Thanks for the tip. You have just commented on a 2 year old comment.
  • Chandranshu
    Chandranshu over 12 years
    The second grep removes all color formatting.
  • rymo
    rymo over 12 years
    --exclude only matches file patterns, like *.java
  • jperelli
    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
    Adrian Gunawan about 12 years
    You don't need to have double quote - it didnt work for me when the double quote is present
  • Andy Lester
    Andy Lester about 12 years
    Another 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
    José Tomás Tocino almost 12 years
    Really 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
    Shane over 10 years
    alias grepsvn='grep --exclude-dir=".svn"' grepsvn -R searchstring .
  • spaaarky21
    spaaarky21 about 10 years
    This would also waste time finding matches in .svn only to filter them after the fact.
  • user193130
    user193130 over 8 years
    As @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
    dwanderson over 8 years
    Was 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
    Hi-Angel almost 8 years
    That 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 grep alias grepexclhid="grep --exclude-dir=\".*\"".
  • Hi-Angel
    Hi-Angel almost 8 years
    An alias to exclude all dot-starting dirs: alias grepexclhid="grep --exclude-dir=\".*\"". One might even want to make the alias name to be grep, then to use it without an alias you'd have to type full path /usr/bin/grep.
  • shjeff
    shjeff over 5 years
    Have you noticed difference, this is --exclude-dir no --exclude-dirs?
  • yukashima huksay
    yukashima huksay over 3 years
    GREP_OPTIONS stopped being a supported environment variable after 2.20