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

后端 未结 16 1323
半阙折子戏
半阙折子戏 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:52

    Open bug report

    As commented by @tukan, there is an open bug report for Ag regarding the -L/--files-without-matches flag:

    • ggreer/the_silver_searcher: #238 - --files-without-matches does not work properly

    As there is little progress to the bug report, the -L option mentioned below should not be relied on, not as long as the bug has not been resolved. Use different approaches presented in this thread instead. Citing a comment for the bug report [emphasis mine]:

    Any updates on this? -L completely ignores matches on the first line of the file. Seems like if this isn't going to be fixed soon, the flag should be removed entirely, as it effectively does not work as advertised at all.


    The Silver Searcher - Ag (intended function - see bug report)

    As a powerful alternative to grep, you could use the The Silver Searcher - Ag:

    A code searching tool similar to ack, with a focus on speed.

    Looking at man ag, we find the -L or --files-without-matches option:

    ...
    
    OPTIONS
        ...
    
        -L --files-without-matches
               Only print the names of files that don´t contain matches.
    

    I.e., to recursively search for files that do not match foo, from current directory:

    ag -L foo
    

    To only search current directory for files that do not match foo, simply specify --depth=0 for the recursion:

    ag -L foo --depth 0
    

提交回复
热议问题