Grep –include acts like –exclude

grep

I'm trying to make use of the –include option of grep, but it doesn't behave as I expect.

Consider this simplified test:

Setup

me@de31:~/tmp$ cat file.h
This is a .h file
me@de31:~/tmp$ cat file.c
This is a .c file

Verify

me@de31:~/tmp$ grep "This is a" *
file.c:This is a .c file
file.h:This is a .h file

Use –include

me@de31:~/tmp$ grep "This is a" * --include="*.c"
file.h:This is a .h file

me@de31:~/tmp$ grep "This is a" * --include="*.h"
file.c:This is a .c file

Use –exclude

me@de31:~/tmp$ grep "This is a" * --exclude="*.c"
file.h:This is a .h file

me@de31:~/tmp$ grep "This is a" * --exclude="*.h"
file.c:This is a .c file

As you can see, –include has the same output as –exclude. Another post on stackexchange claims "-r" is required, but I tried that as well, and it did not change the output.

Best Answer

This would appear to be a bug of some sort. Found this bug filing on Ubuntu launchpad, titled: --include does the same as --exclude!.

Your examples on my Fedora 14 system:

$ more file.*
::::::::::::::
file.c
::::::::::::::
This is a .c file

::::::::::::::
file.h
::::::::::::::
This is a .h file

include

$ grep "This is a" * --include="*.c"
file.c:This is a .c file
$ grep "This is a" * --include="*.h"
file.h:This is a .h file

exclude

$ grep "This is a" * --exclude="*.c"
file.h:This is a .h file
$ grep "This is a" * --exclude="*.h"
file.c:This is a .c file

grep version

$ grep --version
grep (GNU grep) 2.8
Related Question