How do I find files that do not contain a given string pattern?

335,191

Solution 1

The following command gives me all the files that do not contain the pattern foo:

find .  -not  -ipath '.*svn*' -exec  grep  -H -E -o -c  "foo"  {} \; | grep 0

Solution 2

If your grep has the -L (or --files-without-match) option:

$ grep -L "foo" *

Solution 3

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

grep -riL "foo" .

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 (lowercased) you will get the opposite (files with matches)

     -l, --files-with-matches
             Only the names of files containing selected lines are written

Solution 4

Take a look at ack. It does the .svn exclusion for you automatically, gives you Perl regular expressions, and is a simple download of a single Perl program.

The equivalent of what you're looking for should be, in ack:

ack -L foo

Solution 5

If you are using git, this searches all of the tracked files:

git grep -L "foo"

and you can search in a subset of tracked files if you have ** subdirectory globbing turned on (shopt -s globstar in .bashrc, see this):

git grep -L "foo" -- **/*.cpp
Share:
335,191
Senthil Kumar
Author by

Senthil Kumar

Software Engineer

Updated on July 21, 2022

Comments

  • Senthil Kumar
    Senthil Kumar almost 2 years

    How do I find out the files in the current directory which do not contain the word foo (using grep)?