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]

 

4 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'

Leave a Reply