加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

unix命令行…如何grep并只显示包含字符串的文件名?

发布时间:2020-12-15 22:19:43 所属栏目:安全 来源:网络整理
导读:我知道我可以搜索一个字符串: grep -n -d recurse 'snoopy' * 然后它显示包含该字符串的每个文件名和实例,如: file/name.txt:23 some snoopy herefile/name2.txt:59 another snoopy therefile/name2.txt:343 some more snoopyetc... 问题是,在很多事件中,
我知道我可以搜索一个字符串:

grep -n -d recurse 'snoopy' *

然后它显示包含该字符串的每个文件名和实例,如:

file/name.txt:23    some snoopy here
file/name2.txt:59   another snoopy there
file/name2.txt:343  some more snoopy
etc...

问题是,在很多事件中,列表很大.如何使其仅显示包含字符串的实际文件名,没有重复且没有出现?

只喜欢:

file/name1.txt
file/name52.txt
file/name28293.txt

非常感谢任何帮助:)

解决方法

-l标志(或者,在BSD和GNU grep中,– files-with-matches)做你想要的.

从the POSIX spec开始:

Write only the names of files containing selected lines to standard output. Pathnames shall be written once per file searched. If the standard input is searched,a pathname of “(standard input)” shall be written,in the POSIX locale. In other locales,“standard input” may be replaced by something more appropriate in those locales.

BSD和GNU都明确保证这将更有效. (Older BSD版本说“…… grep只搜索文件,直到找到匹配,使搜索可能更便宜”,newer BSD和GNU说“扫描将在第一场比赛中停止”.)如果你不知道哪个grep你有它和它有哪些选项,只需在shell中键入man grep,你就应该获得联机帮助页.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读