简体   繁体   English

查找特定文件夹,然后在其中搜索特定文件以查找单词

[英]Find specific folders then search specific files inside them for a word

I am trying to combine find and grep in a way to find folder names that start with k0 and search a specific file "test.log" for a word ERROR. 我正在尝试将find和grep结合起来,以查找以k0开头的文件夹名称,并在特定文件“ test.log”中搜索错误字。

Something like: 就像是:

find . -type d -name "k0*" -print | xargs grep ERROR test.log

unfortunately this command doesnt work as intended. 不幸的是,此命令无法按预期运行。

try this, I am assuming you have multiple files named test.log in the folders whose names start with k0 here: 尝试此操作,我假设您在名称以k0开头的文件夹中有多个名为test.log的文件:

for file in $(find ./k0* -name 'test.log'); do 
   grep -w 'ERROR' $file

done

You can make this into a one-liner command like this: 您可以将其变成单线命令,如下所示:

for file in $(find ./k0* -name 'test.log'); do grep -w 'ERROR' $file; done

It's executable on terminal if you just post it. 如果您将其发布,则可以在终端上执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM