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:
- Open up the System Control panel (hold down the Start Menu key and press the Pause/Break key or Start>Run>sysdm.cpl).
- Click on the Advanced tab and then click the Environment Variables button at the bottom of the window.
- Under the User variables section click the New button.
- Set Variable name to
GREP_OPTIONSand Variable value to--exclude=\*.svn\* - Click OK three times and reopen any command prompts you had open.
The setting is effective in a command or bash prompt.
- I feel dirty now. [back]

September 16th, 2007 at 1:09 pm
Actually, you have to add 'export GREP_OPTIONS="–exclude=\*.svn\*"' to the top of your .bashrc file.
September 16th, 2007 at 9:18 pm
Good catch. I updated the post to reflect that.
December 4th, 2007 at 10:36 am
Or use ack; http://www.petdance.com/ack/
December 5th, 2007 at 5:11 pm
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:
For cygwin you'll need to add an alias in your profile/bashrc file,