Ignoring .svn directories with grep

At least a dozen times a day I need to find a bit of text in an unknown file that is buried deep in a tree of directories. If I'm on a Mac or Linux, grep is included and I use cygwin on all of my Windows installs. While it's a great tool, it's also annoying to get a screen full of results from Subversion's .svn directories.

One way to filter out those directories is to use the -v option which only shows lines that don't match a pattern.

grep -ri "your password is" * | grep -v .svn

Sure that works, but grep is still wasting time and processing power searching through all of the .svn directories. It's also more to type, and do u rly wnt 2 type more than u alrdy do? 1

You can use the --exclude option to skip processing any files or directories matching a specific pattern, but that's also more typing. Luckily nobody likes to waste keystrokes and grep looks for the GREP_OPTIONS environment variable which allows you to specify default options.

On a Mac open ~/.profile with a text editor. If you're using Linux, edit ~/.bashrc or ~/.bash_profile or ~/.profile.

Add the following line to the top of the file:

GREP_OPTIONS="--exclude=\*.svn\*"
export GREP_OPTIONS

Save and close the file and re-open your session or type source ~/.bashrc.

If you're using Windows:

  1. Open up the System Control panel (hold down the Start Menu key and press the Pause/Break key or Start>Run>sysdm.cpl).
  2. Click on the Advanced tab and then click the Environment Variables button at the bottom of the window.
  3. Under the User variables section click the New button.
  4. Set Variable name to GREP_OPTIONS and Variable value to --exclude=\*.svn\*
  5. Click OK three times and reopen any command prompts you had open.

The setting is effective in a command or bash prompt.

  1. I feel dirty now. [back]

 

6 Responses to “Ignoring .svn directories with grep”

  1. James Asher Says:

    Actually, you have to add 'export GREP_OPTIONS="–exclude=\*.svn\*"' to the top of your .bashrc file.

  2. Corey Says:

    Good catch. I updated the post to reflect that.

  3. Iain Says:

    Or use ack; http://www.petdance.com/ack/

  4. Corey Says:

    OK, ack is truly awesome. On Windows if you'd like to invoke it by typing 'ack', download the stand-alone version of ack, and create a cmd script called ack.cmd (can be in the same directory as ack) in a directory that's in your PATH with this:

    @perl c:\path\to\ack.pl %*

    For cygwin you'll need to add an alias in your profile/bashrc file,

    alias ack='ack.cmd'
  5. Jonathan Hartley Says:

    This still doesn't quite work right for me. I can't figure out why. Some (but not all) of the files in .svn directories are still included in the output. I'm using Cygwin under Windows XP.

    I've added this to my ~/.profile:

      export GREP_OPTIONS="-I --color --exclude=\*.svn\*"
    

    When I open a new cygwin window, I can see the env variable has been created properly:

      $ echo $GREP_OPTIONS
      -I --color --exclude=\*.svn\*
    

    I enter a grep command:

      $ grep -r LibraryDocumentation .
    

    The results include lines like this one:

      ./Documentation/.svn/entries:LibraryDocumentation
    

    Clearly 'entries' is inside an '.svn' directory. Why isn't it being filtered out?

    Some files within '.svn' are filtered out, such as this, which appears in the output if I unset GREP_OPTIONS, but is removed if I set it:

      ./FunctionalTests/T0321_US_ThreeInstalls.py:        docPath = path.join("Documentation", "LibraryDocumentation")
    

    The '-I' and '–color' options to grep are being honoured, so grep is seeing the 'GREP_OPTIONS' variable.

    I'm most perplexed. If anyone has any clues or ideas, I'd be very grateful. Thanks!

    Jonathan

  6. Jonathan Hartley Says:

    Ah, apologies, I lied slightly. The example I gave of a line that *is* correctly filtered out (starting './FunctiojnalTests/T0321…') is wrong. That line always appears in the output. The following is an example of a line that is correctly filtered out by the '–exclude' flag is:

      ./.svn/text-base/makedocs.py.svn-base:outputDir = os.path.join(os.getcwd(), 'Documentation', 'LibraryDocumentation')
    

Leave a Reply