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

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

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

[root@localhost ~]# 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

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

[root@localhost ~]# find /etc/passwd | xargs grep -ri 'yasuo' -l
/etc/passwd

 

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

[root@localhost 1]# echo 456 >2.txt
[root@localhost 1]# echo 123 >1.txt
[root@localhost 1]# tree
.
├── 1.txt
└── 2.txt

0 directories, 2 files

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

[root@localhost 1]# find .| xargs grep -ri '2'
./1.txt:123
./1.txt:123

加上 -l 只显示文件名称

[root@localhost 1]# find .| xargs grep -ri '2' -l
./1.txt
./1.txt

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

[root@localhost 1]# find .| xargs grep -ri '9' -l
[root@localhost 1]# find .| xargs grep -ri '9' 
[root@localhost 1]# 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值