版权声明:作者-傲娇天子 博文主页地址:https://blog.csdn.net/qq_41116956 欢迎转载,转载请在文章页面明显位置给出原文链接,谢谢 https://blog.csdn.net/qq_41116956/article/details/82788018
大家在工作中,是否遇到需要查询某个文件下内容中的关键词是否存在。例如:
需要查询在/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]#