1

In bash, how to list files that do NOT contain a given string?


Given that

grep --include=*.c  -rlw './' -e "pattern"

return any file that matches the pattern I was expecting that

grep --include=*.c  -rlwv './' -e "pattern"

would return any file that does not match the pattern but it just returns all the *.c files regardless of wether they match the pattern.

Remi.b
  • 17,389
  • 28
  • 87
  • 168
  • This command is working fine for me – anubhava Sep 16 '16 at 22:26
  • 1
    `-v` means to match lines that don't contain the pattern. So your command will list any files that contain a line that doesn't match the pattern. It doesn't mean that **no** lines match the pattern. – Barmar Sep 16 '16 at 23:38
  • The only files that would be excluded from the result would be ones where **all** lines match the pattern. – Barmar Sep 16 '16 at 23:39

2 Answers2

4

You can try to use -L option:

grep  -L -r -i --include \*.c "pattern" ./
Arsen Davtyan
  • 1,891
  • 8
  • 23
  • 40
0

You can use -v option also

grep -v -rwl "pattern"
pavaniiitn
  • 311
  • 1
  • 4
  • 18