在linux命令行中使用find和grep来搜索具有特定用户和文本内容的文件?

我试图找到属于特定用户文件,该文件还包含特定的文本字符串.
例如,我想找到一个属于root用户文件,并包含“hello there”文本.

我知道我可以使用grep搜索文件中的文本,例如:

grep -irl "hello there" /directory1

而且,我知道我可以搜索具有特定扩展名的用户拥有的文件

find -user root -name "*.txt"

有没有办法可以将这两个命令结合起来?

解决方法:

使用所有匹配文件调用grep一次:

find -user root -name "*.txt" -exec grep -il "hello there" {} +

或者为每个文件调用grep -q一次并打印匹配的文件.

find -user root -name "*.txt" -exec grep -iq "hello there" {} \; -print

(-exec可以像-user或-name一样用作测试.如果命令成功,则-exec测试通过并调用-print.)

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...