大家在工作中,是否遇到需要查詢某個檔案下内容中的關鍵詞是否存在。例如:
需要查詢在/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]#