>我正在尝试查找属于特定用户的文件,该文件还包含特定的文本字符串。 例如,我想找到一个属于 root 用户的文件,其中包含文本"你好那里"。
我知道我可以使用 grep 搜索文件中的文本,例如:
grep -irl "hello there" /directory1
而且,我知道我可以搜索具有特定扩展名的用户拥有的文件:
find -user root -name "*.txt"
有没有办法将这两个命令结合起来?
使用所有匹配的文件调用grep
一次:
find -user root -name "*.txt" -exec grep -il "hello there" {} +
或者为每个文件调用grep -q
一次,并-print
匹配的文件。
find -user root -name "*.txt" -exec grep -iq "hello there" {} ; -print
(-exec
可以像-user
或-name
一样用作测试。如果命令成功,则-exec
测试通过并调用-print
。