代码之家  ›  专栏  ›  技术社区  ›  Senthil Kumar

如何查找不包含给定字符串模式的文件?

  •  437
  • Senthil Kumar  · 技术社区  · 16 年前

    文件夹 包含单词 foo (使用 grep )?

    11 回复  |  直到 8 年前
        1
  •  914
  •   t0r0X    11 年前

    如果你的grep有 -L --files-without-match )选项:

    $ grep -L "foo" *
    
        2
  •  50
  •   Adrian    6 年前

    你可以单独使用grep(无需查找)。

    grep -riL "foo" .
    

    这是对上使用的参数的说明 grep

         -L, --files-without-match
                 each file processed.
         -R, -r, --recursive
                 Recursively search subdirectories listed.
    
         -i, --ignore-case
                 Perform case insensitive matching.
    

    如果你使用 l (小写)您将得到相反的结果(带有匹配项的文件)

         -l, --files-with-matches
                 Only the names of files containing selected lines are written
    
        3
  •  48
  •   nbro kai    8 年前

    ack . 它起作用 .svn 自动排除,为您提供Perl正则表达式,是单个Perl程序的简单下载。

    与您正在寻找的内容对应的应该是 阿克 :

    ack -L foo
    
        4
  •  22
  •   nbro kai    8 年前

    下面的命令为我提供了所有不包含该模式的文件 foo :

    find .  -not  -ipath '.*svn*' -exec  grep  -H -E -o -c  "foo"  {} \; | grep 0
    
        5
  •  14
  •   nbro kai    8 年前

    svn 通过使用第二个 grep .

    grep -rL "foo" ./* | grep -v "\.svn"
    
        6
  •  13
  •   Zak    5 年前

    git grep -L "foo"
    

    如果启用了**子目录全局搜索,则可以搜索跟踪文件的子集( shopt -s globstar in.bashrc,请参见 this ):

    git grep -L "foo" -- **/*.cpp
    
        7
  •  9
  •   nbro kai    8 年前

    您实际上需要:

    find .  -not  -ipath '.*svn*' -exec  grep  -H -E -o -c  "foo"  {} \; | grep :0\$
    
        8
  •  6
  •   nbro kai    8 年前

    我和他交了好运

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

    我的尝试 grep -v

        9
  •  5
  •   Gruffy    6 年前

    问题

    .phtml 使用内联PHP代码编写HTML的文件。我想用 Mustache 取而代之的是模板。我想找一些 .phtml 不含细绳的镀金 new Mustache

    解决方案

    find . -iname '*.phtml' -exec grep -H -E -o -c 'new Mustache' {} \; | grep :0$ | sed 's/..$//'

    解释

    管道安装前:

    发现

    find . 从该目录开始递归查找文件

    -iname '*.phtml' .phtml (修订) i

    -exec 'grep -H -E -o -c 'new Mustache' {}' 运行 grep 每个匹配路径上的命令

    格雷普

    -H 始终使用输出行打印文件名标题。

    -E 将模式解释为扩展正则表达式(即force grep 表现得像白鹭)。

    -o 仅打印行的匹配部分。

    -c


    .phtml ,具有字符串的次数计数 新胡子 发生在每种情况下。

    $> find . -iname '*.phtml$' -exec 'grep -H -E -o -c 'new Mustache' {}'\;
    
    ./app/MyApp/Customer/View/Account/quickcodemanagestore.phtml:0
    ./app/MyApp/Customer/View/Account/studio.phtml:0
    ./app/MyApp/Customer/View/Account/orders.phtml:1
    ./app/MyApp/Customer/View/Account/banking.phtml:1
    ./app/MyApp/Customer/View/Account/applycomplete.phtml:1
    ./app/MyApp/Customer/View/Account/catalogue.phtml:1
    ./app/MyApp/Customer/View/Account/classadd.phtml:0
    ./app/MyApp/Customer/View/Account/orders-trade.phtml:0
    

    第一管 grep :0$ 筛选此列表以仅包括以结尾的行 :0 :

    $> find . -iname '*.phtml' -exec grep -H -E -o -c 'new Mustache' {} \; | grep :0$
    
    ./app/MyApp/Customer/View/Account/quickcodemanagestore.phtml:0
    ./app/MyApp/Customer/View/Account/studio.phtml:0
    ./app/MyApp/Customer/View/Account/classadd.phtml:0
    ./app/MyApp/Customer/View/Account/orders-trade.phtml:0
    

    第二根管子 sed 's/..$//' 去掉每行的最后两个字符,只留下文件路径。

    $> find . -iname '*.phtml' -exec grep -H -E -o -c 'new Mustache' {} \; | grep :0$ | sed 's/..$//'
    
    ./app/MyApp/Customer/View/Account/quickcodemanagestore.phtml
    ./app/MyApp/Customer/View/Account/studio.phtml
    ./app/MyApp/Customer/View/Account/classadd.phtml
    ./app/MyApp/Customer/View/Account/orders-trade.phtml
    
        10
  •  1
  •   user6305682    9 年前

    我的grep没有任何-L选项。我确实找到了实现这一目标的方法。

    这些想法是:

    1. 将包含该字符串的所有文件名转储到txt1.txt。
    2. 将目录中的所有文件名转储到txt2.txt。
    3. 使用diff命令对2转储文件进行区分。

      grep 'foo' *.log | cut -c1-14 | uniq > txt1.txt
      grep * *.log | cut -c1-14 | uniq > txt2.txt
      diff txt1.txt txt2.txt | grep ">"
      
        11
  •  1
  •   zandeep    9 年前

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

    您可以在“find”下指定过滤器,在“grep-vwE”下指定排除字符串。如果还需要根据修改后的时间进行筛选,请使用“查找”下的“mtime”。

        12
  •  1
  •   dfrib    7 年前

    打开错误报告

    正如@tukan所评论的,Ag有一个关于 -L / --files-without-matches 旗帜:

    -L 下面提到的选项

    这方面有更新吗? 该标志应完全移除,因为它实际上根本不起广告宣传的作用


    银色搜索器-Ag(预期功能-参见错误报告)

    作为一个强有力的替代品 grep ,你可以使用 The Silver Searcher - Ag

    类似于ack的代码搜索工具,关注速度。

    看着 man ag -L --没有匹配项的文件 选项:

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

    搜索不匹配的文件 foo

    ag -L foo
    

    只搜索 现在的 不匹配文件的目录 ,只需指定 --depth=0 对于递归:

    ag -L foo --depth 0
    
        13
  •  1
  •   JMD    6 年前

    另一种选择是,当grep没有-L选项(例如IBM AIX)时,只有grep和shell:

    for file in * ; do grep -q 'my_pattern' $file || echo $file ; done
    
        14
  •  1
  •   Daniel J.    5 年前

    使用“查找”时,有两个基本选项:在“查找”完成搜索后过滤结果,或者使用一些内置选项,以防止“查找”考虑与某些给定模式匹配的文件和目录。

    如果使用-not关键字作为find参数,则会阻止考虑与后面的-name或-regex参数上的字符串匹配的任何路径,这将更加有效。

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

    然后,任何未被-not过滤掉的路径都将被后续的-regex参数捕获。

        15
  •  0
  •   ARIF MAHMUD RANA    4 年前

    这可能会帮助其他人。我有很多文件 Go test 文件夹。但我只需要 .go

    ls *.go | grep -v "_test.go"
    

    -v、 --反转匹配选择非匹配线请参见 https://stackoverflow.com/a/3548465

    还可以将其与vscode一起使用,以打开终端中的所有文件

    code $(ls *.go | grep -v "_test.go")
    
        16
  •  -4
  •   Zong    11 年前
    grep -irnw "filepath" -ve "pattern"
    

    grep -ve "pattern" < file
    

    上面的命令将给我们一个结果,因为-v找到了正在搜索的模式的倒数

        17
  •  -6
  •   nbro kai    8 年前

    下面的命令可以帮助您过滤包含子字符串“foo”的行。

    cat file | grep -v "foo"