如何查找内容包含特定字符串的具有特定名称的所有文件的位置?

dav*_*vid 1 linux command-line find

我想找到所有命名的文件的位置 index.php包含字符串“hello”的。

谢谢。

Kus*_*nda 9

使用grep具有find

find /top-dir -type f -name index.php -exec grep -l 'hello' {} +
Run Code Online (Sandbox Code Playgroud)

在哪里 /top-dir您要搜索的最顶层目录的路径。

使用-type f,我们只查看使用 的常规文件find,使用-name index.php将搜索限制为名为index.php.

-exec grep -l 'hello' {} +grep在找到的文件上运行,并输出与模式 ( 'hello')匹配的所有文件的路径。这是-lgrep导致路径的输出。

随着+末,find将尽可能多的文件成为了可能给你的每次调用grep。将此更改为';'or\;将导致grep一次调用一个文件,如果有很多文件,这可能会很慢。