exclude directories in recursive grep without -exclude-dir

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

I'm trying to do a recursive grep on a directory while excluding all the .svn folders. How can I do this? I've read about the -exclude-dirs but I don't seem to have that available on my system.

Thanks!

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

This works:

grep -iR "string" * | grep -v ".svn"

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

What if I want to exclude more than one directory? Say, .svn and log?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Try ack. Your two searches would look something like this:

ack -ai "string"
ack -ai --ignore-dir=log "string"

By default, it ignores .svn and other common version-control or backup cruft. Also, it defaults to a recursive search (use -n to prevent that).

Btw, on Debian the program is called ack-grep -- may not be the same on your distro.

Smiling

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

"command not found" Sad

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Megan wrote:
"command not found" :(

It's not likely to have been installed by default. Try adding it with your package manager. On Debian or Ubuntu:

$ sudo aptitude install ack-grep
$ ack-grep --help
Usage: ack [OPTION]... PATTERN [FILE]

Search for PATTERN in each source file in the tree from cwd on down.
...

Smiling

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

I don't have root access on this server. Maybe I'll have to bug the sys admins to either install this or upgrade grep.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

How about:
find . -not -path '*/.svn*' | xargs grep -i "string"

ack seems interesting. (offtopic) are you a Debian man now Abhi?

a Padded Cell our articles site!

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

JeevesBond wrote:
ack seems interesting. (offtopic) are you a Debian man now Abhi?

I have been using Debian mainly since at least early 2007, maybe even 2006 if memory serves. Smiling

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Hmmm, that means we've been acquanted for quite a while. You used to use Gentoo, if memory serves. Smiling

That would have been back in 2005/6 we were talking about such things.

a Padded Cell our articles site!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.