I have noticed an annoying change in grep -r --exclude-dir behaviour. Consider the following test:

mkdir -p ./d1/d2
echo 'randomtext' > ./d1/d2/testfile

grep -nr --exclude-dir ./d1/d2 'randomtext'

An expected behaviour is that grep would not show "testfile" as it should be filtered via --exclude-dir. That works for grep v2.10 and 2.11. However for versions 2.12+ the output is:


Moreover, the behaviour is inconsistent:

grep -nr --exclude-dir d1 'randomtext'
# or 
grep -nr --exclude-dir d2 'randomtext'

Work fine, without any output, as expected.

I believe that it is a bug. Can you please confirm my theory? Thank you.


Between v2.11 and v2.12, the grep changelog mentions only the following change about --exclude-dir:

grep: fix segfault with -r --exclude-dir and no file operand

I suppose that the change of behavior is a side effect of this fix (which is exactly your case), i.e. a bug has been introduced, but as you can see, grep could also segfault in this case before v2.12. So, do not expect v2.11 to be reliable.

I suggest that you report this bug.

Note: This segfault was due to a fix in v2.11. The NEWS file says for v2.11:

The --include, --exclude, and --exclude-dir options now handle
command-line arguments more consistently.  --include and --exclude
apply only to non-directories and --exclude-dir applies only to
directories.  "-" (standard input) is never excluded, since it is
not a file name.
[bug introduced in grep-2.5]

and for v2.12:

grep no longer segfaults with -r --exclude-dir and no file operand.
I.e., ":|grep -r --exclude-dir=D PAT" would segfault.
[bug introduced in grep-2.11]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.