Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
When using -r, change the bare * (a list of files) to . (a directory name). It would also be good to link to the other post. :) –  Mikel Jul 12 '13 at 20:03
add comment

1 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
share|improve this answer
    
Thanks for the link. Somehow I didn't find it while googling. $ grep --version grep (GNU grep) 2.10 Sounds like it's been fixed and re-broken a few times. –  user2100564 Jul 14 '13 at 18:06
    
@user2100564 - yup. Not sure what the issue is. If you get a sec. can you please mark this as the accepted answer so that others know this question has been solved? Thanks. –  slm Jul 14 '13 at 18:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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