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]#
 
————————————————
版权声明:本文为CSDN博主「傲娇天子」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_41116956/article/details/82788018

posted @ 2020-04-30 12:28  老虎逛大街  阅读(2135)  评论(0编辑  收藏  举报
levels of contents