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-dir=\.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-dir=\.svn - Click OK three times and reopen any command prompts you had open.
The setting is effective in a command or bash prompt.
UPDATE Feb 2, 2009: Thanks to Iain I've been enjoying ack so much that I didn't realize that –exclude only works on the basename, meaning it won't exclude subdirectories. At long last grep 2.5.3 introduces the –exclude-dir option which works the way it should.
- 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,
October 27th, 2008 at 9:37 am
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:
When I open a new cygwin window, I can see the env variable has been created properly:
I enter a grep command:
The results include lines like this one:
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
October 27th, 2008 at 9:40 am
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:
February 4th, 2009 at 6:04 am
Sigh. One year one, I'm still googling for a solution to this, and all I can find are my own comments from last year. :-(
February 4th, 2009 at 12:29 pm
@Jonathan: There's a new option, –exclude-dir in grep 2.5.3 (grep –version) that works as expected. I've updated the post accordingly.
May 27th, 2009 at 2:33 am
Hooray! (Which I understand is spelt '\o/' nowadays)
This now works brilliantly. Thanks heaps Corey.
Today I'm at an Ubuntu bash prompt. I hope Cygwin grep is >= v2.5.3…
January 25th, 2010 at 1:47 pm
Came across this post today… for Mac users, even my Snow Leopard OS only came with grep 2.5.1 which doesn't have the –exclude-dir option. But you can install grep via macports which gives you 2.5.3+. Then following the instructions to add GREP_OPTIONS to .profile works.