54

How do I find files not containing some text on Linux? Basically I'm looking for the inverse of the following

find . -print | xargs grep -iL "somestring"
Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
  • 1
    Belongs on SuperUser or unix.stackexchange.com – Eric Wilson Dec 21 '11 at 15:58
  • This does not work correctly if one of the file or directory names contains a white space. Use `find . -type f -exec grep -iL "somestring" +` (or `grep -r`). – hagello Jan 28 '16 at 05:27
  • Possible duplicate of [Using grep to find files that don't contain a given string pattern](https://stackoverflow.com/questions/1748129/using-grep-to-find-files-that-dont-contain-a-given-string-pattern) – phuclv Aug 10 '17 at 10:59

4 Answers4

76

The command you quote, ironically enough does exactly what you describe. Test it!

echo "hello" > a
echo "bye" > b
grep -iL BYE a b

Says a only.


I think you may be confusing -L and -l

find . -print | xargs grep -iL "somestring"

is the inverse of

find . -print | xargs grep -il "somestring"

By the way, consider

find . -print0 | xargs -0 grep -iL "somestring"

Or even

grep -IRiL "somestring" .
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 2
    accepting your answer, it's recursive and inverse of find I wanted: grep -IRiL "somestring" . –  Aug 26 '11 at 13:15
12

You can do it with grep alone (without find).

grep -riL "somestring" .

This is the explanation of the parameters used on grep

     -L, --files-without-match
             each file processed.
     -R, -r, --recursive
             Recursively search subdirectories listed.

     -i, --ignore-case
             Perform case insensitive matching.

If you use l lowercase you will get the opposite (files with matches)

     -l, --files-with-matches
             Only the names of files containing selected lines are written
Adrian
  • 9,102
  • 4
  • 40
  • 35
2

Find the markdown file through find and grep to find the mismatch

$ find. -name '* .md' -print0 | xargs -0 grep -iL "title"

Directly use grep's -L to search for files that only contain markdown files and no titles

$ grep -iL "title" -r ./* --include '* .md'
lupguo
  • 1,525
  • 13
  • 13
0

If you use "find" the script do "grep" also in folder:

[root@vps test]# find  | xargs grep -Li 1234
grep: .: Is a directory
.
./test.txt
./test2.txt
[root@vps test]#

Use the "grep" directly:

# grep -Li 1234 /root/test/*
/root/test/test2.txt
/root/test/test.txt
[root@vps test]#

or specify in "find" the options "-type f"...even if you use the find you will put more time (first the list of files and then make the grep).

danilo
  • 139
  • 1
  • 5