我该如何在Linux上使用grep仅显示文件名? [已关闭]

16 浏览
0 Comments

我该如何在Linux上使用grep仅显示文件名? [已关闭]

已关闭。这个问题不符合Stack Overflow准则。目前不接受答案。


 

这个问题似乎不涉及具体的编程问题、软件算法或主要由程序员使用的软件工具。如果你认为这个问题在其他Stack Exchange网站上应该是一个合适的话题,你可以留下一条评论解释在哪里可以回答这个问题。

社区在20天前已经审查过是否重新开放此问题,但没有。

原关闭原因未被解决


改进此问题

如何在Linux上使用grep只显示文件名(不包含行内匹配)?

我通常使用类似于:

find . -iname "*php" -exec grep -H myString {} \;

我该如何获取文件名(带路径),但不包括匹配内容?我需要使用xargs吗?我在grep手册页上没有看到这样的方法。

admin 更改状态以发布 2023年5月21日
0
0 Comments

来自 grep(1) 的手册页面:

  -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.)

0
0 Comments

标准选项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.

在这种情况下,您也不需要使用-H

0