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

后端 未结 16 1144
半阙折子戏
半阙折子戏 2020-11-28 17:42

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

相关标签:
16条回答
  • 2020-11-28 18:12

    You will actually need:

    find .  -not  -ipath '.*svn*' -exec  grep  -H -E -o -c  "foo"  {} \; | grep :0\$
    
    0 讨论(0)
  • 2020-11-28 18:13

    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
    
    0 讨论(0)
  • 2020-11-28 18:13

    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
    
    0 讨论(0)
  • 2020-11-28 18:13

    another alternative when grep doesn't have the -L option (IBM AIX for example), with nothing but grep and the shell :

    for file in * ; do grep -q 'my_pattern' $file || echo $file ; done
    
    0 讨论(0)
提交回复
热议问题