如何grep在Linux上使用仅显示文件名(无内联匹配)?
我通常使用类似的东西:
find . -iname "*php" -exec grep -H myString {} \;
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得文件名(带路径),但没有匹配?我必须使用xargs吗?在我的grep手册页上,我没有看到这样做的方法.
Ran*_*832 1480
标准选项grep -l(即小写L)可以做到这一点.
从Unix标准:
-l
(The letter ell.) Write only the names of files containing selected
lines to standard output. Pathnames are written once per file searched.
If the standard input is searched, a pathname of (standard input) will
be written, in the POSIX locale. In other locales, standard input may be
replaced by something more appropriate in those locales.
Run Code Online (Sandbox Code Playgroud)
-H在这种情况下你也不需要.
Ign*_*ams 134
从grep(1)手册页:
Run Code Online (Sandbox Code Playgroud)-l, --files-with-matches Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. (-l is specified by POSIX.)
小智 31
对于简单的文件搜索,您可以使用grep -l和-r选项:
grep -rl "mystring"
Run Code Online (Sandbox Code Playgroud)
所有的搜索都是由grep完成的.当然,如果您需要在其他参数上选择文件,find是正确的解决方案:
find . -iname "*.php" -execdir grep -l "mystring" {} +
Run Code Online (Sandbox Code Playgroud)
该execdir选项为每个目录构建每个grep命令,并将文件名连接到一个命令(+).