Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find files that does not contain a string

Tags:

regex

linux

grep

To find all the files that contain "foo" in current folder, I use:

grep -r "foo" .

To find all the files that contain "bar" in current folder, I use:

grep -r "bar" .

But how to find all files that does not contain 'foo' and 'bar'?

like image 492
nam Avatar asked Feb 11 '13 10:02

nam


2 Answers

To print lines that do not contain some string, you use the -v flag:

grep -r -v "bar" . | grep -v "foo"

This gives you all lines that do not contain foo or bar.

To print files that do not contain some string, you use the -L flag. To non-match several strings, you can use regular expressions with the -P flag (there are several regex flags you can use):

grep -r -L -P "(foo|bar)" .

This prints a list of files that don't contain foo or bar.

Thanks to Anton Kovalenko for pointing this out.

like image 164
alestanis Avatar answered Oct 12 '22 05:10

alestanis


Recursively searches directories for all files that do no contains XYZ

find . -type f | xargs grep -L "XYZ"
like image 29
Owen Lindsell Avatar answered Oct 12 '22 05:10

Owen Lindsell