Fork me on GitHub

grep excluding .svn dirs

Grep is a fantastic search command that let you filter results and give you what you need. It is used to search in the content of files and is very useful for programmers like us :)

Typically, when you search in a directory recursively and you want to exclude hidden directory such as the .svn, you so this:
grep -ri "your password is" * | grep -v .
this waste time cause search in all directories including the ones you want to exclude and then remove the unwanted results.
This other method, is the correct and quickest one:
grep -r 'content_graphic' assets/js --exclude=*\.svn*
COOOL!

If you want to make this permanent when you use grep, just put it in your profile file: 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:
export GREP_OPTIONS="--exclude=*\.svn*"
save and close!

Happy grepping to everybody ;)


Responses


  1. Just what I needed. Thanks!


    Comment by Jesper — September 2, 2009 @ 3:27 pm


  2. This is a great tip, but it seems like the regular expression is strange — shouldn’t you escape the . (dot) and not the * (asterix)? For me, this is what worked: –exclude=*\.svn*


    Comment by Andrew — October 14, 2009 @ 4:12 pm


  3. thanks Andrew, it was a mistake. I fixed the error.


    Comment by antonio lorusso — October 14, 2009 @ 5:00 pm


  4. Actually, I was wrong again. The –exclude option of grep only excludes files, NOT directories. Grep 2.5.3 added the –exclude-dir option specifically for excluding directories (in which case –exclude-dir=\.svn works); in earlier versions, one needs to use the find command and pipe it to grep (though I never got that to work properly). Best by far, however, is to just use ack instead (see betterthangrep.com). It’s faster, ignores .svn directories by default, and simply requires this statement: ack [pattern]


    Comment by Andrew — October 14, 2009 @ 5:30 pm



Leave a comment

(required)

(required)