Excluding nested directories with grep

grep

I would like to exclude a nested directory from my grep searches, such as /path/to/file. For example:

jake@jake-laptop:~/test$ egrep -r --exclude-dir="path" "hello" .
jake@jake-laptop:~/test$ egrep -r --exclude-dir="to" "hello" .
jake@jake-laptop:~/test$ egrep -r --exclude-dir="file" "hello" .
jake@jake-laptop:~/test$ egrep -r --exclude-dir="path/to/file" "hello" .
./path/to/file/f.txt:hello

In the last line, the file is not excluded, apparently because I have multiple nested directories provided with the exclude-dir argument. How can I get the last example to exclude searches in the path/to/file directory?

Best Answer

It seems that --exclude-dir is only compared against the basename of the path, ie the current subdir. So path/to/file will never match dir "file", but only --exclude-dir=file will, or a glob version eg --exclude-dir=*ile.

The usual alternative is to use find, eg if it handles option -path:

find . -path ./path/to/file -prune -o -type f -exec egrep -l 'hello' {} +

The pattern after -path has to match the path including your starting dir, but you can use globs to simplify, eg '*path*file*', where * matches / as well.

Otherwise, you can resort to find | sed | xargs egrep.

Related Question