linux find查找多个关键字,Linux小技巧:find . | xargs grep -ri "关键字" -l —— 查询、匹配文件内容关键词...

大家在工作中,是否遇到需要查询某个文件下内容中的关键词是否存在。例如:

需要查询在/etc/passwd下是否存在“yasuo”关键字:

[[email protected] ~]# find /etc/passwd | xargs grep -ri ‘yasuo‘

yasuo:x:1001:1001::/home/yasuo:/bin/bash

yasuo_1:x:1234:1010::/home/yasuo_1:/sbin/nologin

yasuo_2:x:1011:1011::/media/yasuo_2:/bin/bash

如果只需要知道文件名称:

[[email protected] ~]# find /etc/passwd | xargs grep -ri ‘yasuo‘ -l

/etc/passwd

我们在1.txt文件中放入‘123’,2.txt中放入‘456’

[[email protected] 1]# echo 456 >2.txt

[[email protected] 1]# echo 123 >1.txt

[[email protected] 1]# tree

.

├── 1.txt

└── 2.txt

0 directories, 2 files

使用命令查询文件中存在关键字2的文件:

[[email protected] 1]# find .| xargs grep -ri ‘2‘

./1.txt:123

./1.txt:123

加上 -l 只显示文件名称

[[email protected] 1]# find .| xargs grep -ri ‘2‘ -l

./1.txt

./1.txt

如果没有则不显示任何信息

[[email protected] 1]# find .| xargs grep -ri ‘9‘ -l

[[email protected] 1]# find .| xargs grep -ri ‘9‘

[[email protected] 1]#

————————————————

版权声明:本文为CSDN博主「傲娇天子」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_41116956/article/details/82788018

原文:https://www.cnblogs.com/sandshell/p/12808122.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值