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

后端 未结 16 1143
半阙折子戏
半阙折子戏 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 17:59
    grep -irnw "filepath" -ve "pattern"
    

    or

    grep -ve "pattern" < file
    

    above command will give us the result as -v finds the inverse of the pattern being searched

    0 讨论(0)
  • 2020-11-28 18:01

    find *20161109* -mtime -2|grep -vwE "(TRIGGER)"

    You can specify the filter under "find" and the exclusion string under "grep -vwE". Use mtime under find if you need to filter on modified time too.

    0 讨论(0)
  • 2020-11-28 18:06

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

    $ grep -L "foo" *
    
    0 讨论(0)
  • 2020-11-28 18:08

    When you use find, you have two basic options: filter results out after find has completed searching or use some built in option that will prevent find from considering those files and dirs matching some given pattern.

    If you use the former approach on a high number of files and dirs. You will be using a lot of CPU and RAM just to pass the result on to a second process which will in turn filter out results by using a lot of resources as well.

    If you use the -not keyword which is a find argument, you will be preventing any path matching the string on the -name or -regex argument behind from being considered, which will be much more efficient.

    find . -not -regex ".*/foo/.*" -regex ".*"
    

    Then, any path that is not filtered out by -not will be captured by the subsequent -regex arguments.

    0 讨论(0)
  • 2020-11-28 18:09

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

    I had good luck with

    grep -H -E -o -c "foo" */*/*.ext | grep ext:0
    

    My attempts with grep -v just gave me all the lines without "foo".

    0 讨论(0)
提交回复
热议问题