grep 命令系列:从文件中搜索并显示文件名
2023-09-11 14:16:12 时间
我怎样从几个文件中搜索(grep),并只显示匹配到的文件的文件名?
当你从不止一个的文件中搜索时,默认它将显示文件名:
grep "word" 文件名 grep root /etc/*
示例输出:
/etc/bash.bashrc: See "man sudo_root" for details. /etc/crontab:17 * * * * root cd / run-parts --report /etc/cron.hourly /etc/crontab:25 6 * * * root test -x /usr/sbin/anacron || ( cd / run-parts --report /etc/cron.daily ) /etc/crontab:47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / run-parts --report /etc/cron.weekly ) /etc/crontab:52 6 1 * * root test -x /usr/sbin/anacron || ( cd / run-parts --report /etc/cron.monthly ) /etc/group:root:x:0: grep: /etc/gshadow: Permission denied /etc/logrotate.conf: create 0664 root utmp /etc/logrotate.conf: create 0660 root utmp
每行开始的第一个部分是文件名(如:/etc/crontab、/etc/group)。使用 -l 选项可以只显示文件名:
grep -l "string" filename grep -l root /etc/*
示例输出:
/etc/aliases /etc/arpwatch.conf grep: /etc/at.deny: Permission denied /etc/bash.bashrc /etc/bash_completion /etc/ca-certificates.conf /etc/crontab /etc/group
你也可以逆转输出;使用 -L 选项来输出那些不匹配的文件的文件名:
grep -L "word" filename grep -L root /etc/*
示例输出:
/etc/apm /etc/apparmor /etc/apparmor.d /etc/apport /etc/apt /etc/avahi /etc/bash_completion.d /etc/bindresvport.blacklist /etc/blkid.conf /etc/bluetooth /etc/bogofilter.cf /etc/bonobo-activation /etc/brlapi.key grep 命令系列:从文件中搜索并显示文件名 本文来自云栖社区合作伙伴“Linux中国” 原文发布时间为:2013-04-02.
相关文章
- BFS(四):搜索状态判重
- android POI搜索,附近搜索,周边搜索定位介绍
- Elasticsearch-数据的存储、搜索(干货)
- 【HDU6037】Expectation Division(动态规划,搜索)
- 稀疏数组搜索
- 微服务轮子项目(11) - 实时搜索系统设计
- C#,数据检索算法之插值搜索(Interpolation Search)的源代码
- 2/19 kmp算法+dfs+记忆化搜索+图的遍历+层次拓扑
- 微信小程序搜索框代码组件
- redis的搜索组件 redis-search4j
- CentOS6.5菜鸟之旅:关于搜索的shell命令
- LeetCode Binary Search Summary 二分搜索法小结
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
- 社区产品搜索