2

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
  • 145
  • 1
    In my Kubuntu man 1 grep says "skip any subdirectory whose base name matches". The base name of foo is foo, so it works; but the base name of foo/bar is bar. Can you exclude bar? or must it be foo/bar? (because you don't want to exclude other bars). – Kamil Maciorowski Nov 02 '20 at 11:24
  • @KamilMaciorowski I want to exclude bar which is a subdirectory of foo, my directory structure is foo/bar. – gornvix Nov 02 '20 at 11:27
  • firejail --blacklist is a general way to exclude any path, even if the tool you want to use doesn't support excluding. – Kamil Maciorowski Nov 02 '20 at 11:36
  • @KamilMaciorowski firejail sounds a bit complicated/convoluted. I found a workaround and updated my question. – gornvix Nov 02 '20 at 11:50

2 Answers2

0

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

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

0

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
  • 397