5

This works for me, in that it excludes the "foo" directory from the root directory of the search:

grep -rn --exclude-dir=foo 'custom'

However this doesn't work:

grep -rn --exclude-dir=foo/bar 'custom'

But the "foo/bar" directory is still searched. I also tried it with quotes:

grep -rn --exclude-dir='foo/bar' 'custom'

I'm using Ubuntu 20.

Update

Although not perfect, I used this workaround instead:

grep -rn 'custom'|grep -v 'foo/bar'

This will fail to find lines that contain both "foo/bar" and "custom".

gornvix
  • 175

2 Answers2

2

Use this operator "*variable*" as wildcard matching any directory you want to exclude from grep search.

In your case,

grep -rn --exclude-dir={"*foo*"} 'custom' will exclude directory or subdirectory with a name of 'foo'.

grep -rn --exclude-dir={"*foo*","*bar*"} 'custom' will exclude any directory and subdirectory with a name of 'foo' and 'bar'.

Faron
  • 497
1

I just ran into the same issue, solved it by calling

grep -rn --exclude="**/foo/**" 'custom'