如何找出当前目录中不包含单词foo(使用grep)的文件?
当前回答
为了完整,ripgrep版本如下:
rg --files-without-match "pattern"
你可以结合文件类型和搜索路径,例如:
rg --files-without-match -t ruby "frozen_string_literal: true" app/
其他回答
问题
我需要重构一个使用.phtml文件编写HTML使用内联PHP代码的大型项目。我想用胡子模板代替。我想找到任何。phtml贾尔斯不包含字符串新的胡子,因为这些仍然需要重写。
解决方案
找到。- iname”*。phtml' -exec grep -H -E -o -c '新胡子' {}\;| grep:0$ | sed 's/..$//'
解释
在管道之前:
Find
找到。递归地查找文件,从这个目录开始
- iname”*。文件名必须包含.phtml (i使其不区分大小写)
-exec 'grep -H -E -o -c 'new Mustache'{}'在每个匹配的路径上执行grep命令
Grep
始终用输出行打印文件名标题。
将pattern解释为扩展的正则表达式(即force 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
第一个管道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
grep -irnw "filepath" -ve "pattern"
or
grep -ve "pattern" < file
上面的命令会给我们结果,因为-v找到了正在搜索的模式的倒数
这可能会对其他人有所帮助。我有混合文件Go和测试文件。但我只需要。go文件。所以我用了
ls *.go | grep -v "_test.go"
-v,——invert-match选择不匹配的行参见https://stackoverflow.com/a/3548465
也可以使用vscode从终端打开所有文件
code $(ls *.go | grep -v "_test.go")
你实际上需要:
find . -not -ipath '.*svn*' -exec grep -H -E -o -c "foo" {} \; | grep :0\$
当您使用find时,您有两个基本选项:在find完成搜索后过滤结果,或者使用一些内置选项来阻止find考虑那些匹配某些给定模式的文件和dirs。
如果您在大量的文件和dirs上使用前一种方法。您将使用大量的CPU和RAM来将结果传递给第二个进程,而第二个进程反过来也会使用大量的资源来过滤结果。
如果你使用-not关键字作为一个find参数,你将阻止任何与后面的name或-regex参数上的字符串匹配的路径被考虑,这将更有效。
find . -not -regex ".*/foo/.*" -regex ".*"
然后,任何没有被-not过滤掉的路径都将被后续的-regex参数捕获。
推荐文章
- 在Windows中有像GREP这样的模式匹配实用程序吗?
- 如何在Python中获得所有直接子目录
- 即使模板文件存在,Flask也会引发TemplateNotFound错误
- 如何从命令行将每两行合并为一行?
- 如何复制在bash所有目录和文件递归?
- 如何在Ruby中创建文件
- __FILE__宏显示完整路径
- 匹配前后的Grep字符?
- (grep)正则表达式匹配非ascii字符?
- 如何从另一个文件A中删除文件B中出现的行?
- 如何使用查找命令从列表中查找所有具有扩展名的文件?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- 如何删除文件中的特定行?
- 使用Java重命名文件
- PowerShell等价于grep -f