How can I use grep to show just filenames on Linux?
如何使用
我通常使用的是:
1 | find . -iname"*php" -exec grep -H myString {} \; |
如何获取文件名(带路径),但不带匹配项?我必须使用
标准选项
从Unix标准:
1 2 3 4 5 6 | -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. |
在这种情况下,您也不需要
从
1
2
3
4
5 -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.)
对于简单的文件搜索,您可以使用grep的
1 | grep -rl"mystring" |
所有的搜索都是由grep完成的。当然,如果需要在其他参数上选择文件,find是正确的解决方案:
1 | find . -iname"*.php" -execdir grep -l"mystring" {} + |