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

Sen*_*mar 494 directory grep file find

如何找出文件中就当前目录中包含单词foo(使用grep)?

gho*_*g74 773

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

$ grep -L "foo" *
Run Code Online (Sandbox Code Playgroud)

  • 或等效使用[ack](http://betterthangrep.com/):`ack -L'foo'` (35认同)
  • @GuruM这可以通过导出变量`GREP_OPTIONS =' - exclude-dir = .svn --exclude-dir = .git'`:^)在GNU grep中完成 (10认同)
  • 或等效使用[ag](https://github.com/ggreer/the_silver_searcher):`ag -L'foo'` (5认同)
  • 像魔术一样工作!提示:使用`-rL`而不是`-L`来匹配子目录 (5认同)

And*_*ter 44

看看ack.它会.svn自动为您排除,为您提供Perl正则表达式,并且是单个Perl程序的简单下载.

相当于你正在寻找的东西,应该是ack:

ack -L foo
Run Code Online (Sandbox Code Playgroud)


Sen*_*mar 16

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

find .  -not  -ipath '.*svn*' -exec  grep  -H -E -o -c  "foo"  {} \; | grep 0
Run Code Online (Sandbox Code Playgroud)

  • @clouseau大多是正确的...但是,`grep'0 $'`也会匹配10行数的文件!你需要`grep':0 $'`来检查行尾的显式':0'.然后,您将只获得零线匹配的文件. (9认同)
  • 您希望将最后的grep 0更改为grep 0 $(否则会在文件名中包含字符0的文件上出现错误匹配). (4认同)

use*_*305 13

以下命令不需要svn使用秒来过滤掉文件夹grep.

grep -rL "foo" ./* | grep -v "\.svn"
Run Code Online (Sandbox Code Playgroud)


Zak*_*Zak 13

如果您使用的是 git,这将搜索所有跟踪的文件:

git grep -L "foo"
Run Code Online (Sandbox Code Playgroud)

如果您打开了 ** 子目录通配符,您可以搜索跟踪文件的子集(shopt -s globstar在 .bashrc 中,请参阅此内容):

git grep -L "foo" -- **/*.cpp
Run Code Online (Sandbox Code Playgroud)


Adr*_*ian 10

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

grep -riL "foo" .
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)


ako*_*nov 10

为了完整性,ripgrep版本:

rg --files-without-match "pattern"
Run Code Online (Sandbox Code Playgroud)

您可以结合文件类型和搜索路径,例如

rg --files-without-match -t ruby "frozen_string_literal: true" app/
Run Code Online (Sandbox Code Playgroud)


小智 9

你真的需要:

find .  -not  -ipath '.*svn*' -exec  grep  -H -E -o -c  "foo"  {} \; | grep :0\$
Run Code Online (Sandbox Code Playgroud)


小智 6

我好运

grep -H -E -o -c "foo" */*/*.ext | grep ext:0
Run Code Online (Sandbox Code Playgroud)

我的尝试grep -v只给了我所有的线条而没有"foo".


Gru*_*ffy 5

问题

我需要重构一个大型项目,该项目使用.phtml文件使用内联 PHP 代码写出 HTML。我想改用Mustache模板。我想找到任何.phtml不包含字符串的 giles,new Mustache因为它们仍然需要重写。

解决方案

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

解释

管道前:

find . 递归查找文件,从这个目录开始

-iname '*.phtml'文件名必须包含.phtmli使其不区分大小写)

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

格雷普

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

-E 将模式解释为扩展的正则表达式(即强制 grep 表现为 egrep)。

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

-c 仅将选定行的计数写入标准输出。


这将为我提供以 结尾的所有文件路径的列表.phtml,并计算字符串new Mustache在每个路径中出现的次数。

$> 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
Run Code Online (Sandbox Code Playgroud)

第一个管道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
Run Code Online (Sandbox Code Playgroud)

第二个管道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
Run Code Online (Sandbox Code Playgroud)