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



>我正在尝试查找属于特定用户的文件,该文件还包含特定的文本字符串。 例如,我想找到一个属于 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

最新更新