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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM