Find files that does not contain a string

后端 未结 5 1003
不思量自难忘°
不思量自难忘° 2020-12-31 18:18

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 curren

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 18:39

    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.

提交回复
热议问题