tags:

views:

57

answers:

4

the line

\ls -1 | grep -v log | xargs grep -r foobar

partially works except it will also skip the "blog" directory since it also gets excluded by grep -v log. (the \ls above is to make ls not do any alias such as ls -F)

+2  A: 
find . -name log -prune -o -type f -print0 |xargs -0 grep foobar
Paul Tomblin
Sorry, it took me a couple of edits to get that right.
Paul Tomblin
great answer. I think on my system i happen to have `.hg` (mercurial) so it grep for those files as well. Is there a way to skip those too?
動靜能量
my first version ignores hidden directorys as well as the --exclude-dir version. To dig into these hidden dirs as well you have to change the wildcard from * to * .*
wose
@Jian, you can add more directories to skip like `find . -name log -prune -o -name .hg -prune -o -type f -print0 | xargs -0 grep foobar`
Paul Tomblin
You can ignore all hidden directories by adding a `-name '.??*' -prune -o` into there before the `-type ...` part.
Paul Tomblin
+3  A: 
\ls -1 | grep -v ^log$ | xargs grep -r foobar

or

grep --exclude-dir=log -r foobar *
wose
+1  A: 

GNU grep has this option:

--exclude-dir=DIR
              Exclude directories matching the pattern DIR from recursive searches.
nos
I am trying on Mac OS X Snow Leopard and it seems like it is not in the man page. But I do see it on Ubuntu 10.10.10
動靜能量
A: 

with bash, you can use extglob

shopt -s extglob
grep "foobar" !(log)
ghostdog74