搜索查找類
find指令
find指令將從指定目錄下遞歸地遍歷其各個(gè)子目錄,將滿足條件的文件或者目錄顯示在終端冲簿。
- 基本語(yǔ)法
find [搜索范圍] [選項(xiàng)] - 選項(xiàng)說(shuō)明
選項(xiàng) | 功能 |
---|---|
-name<查詢方式> | 按照指定的文件名查找模式查找文件 |
-user<用戶名> | 查找屬于指定用戶名所有文件 |
-size<文件大小> | 按照指定的文件大小查找文件 |
- 應(yīng)用實(shí)例
案例1:按文件名:根據(jù)名稱查找/home目錄下的hello.txt文件
[root@localhost ~]# find /home -name hello.txt
/home/hello.txt
案例2:按擁有者:查找/opt目錄下,用戶名稱為nobody的文件
[root@localhost ~]# find /opt -user root
案例3:查找整個(gè)linux系統(tǒng)下大于20m的文件(+n 大于 -n 小于 n 等于)
[root@localhost ~]# find / -size +20M
locate指令
locate指令可以快速實(shí)現(xiàn)文件路徑捐寥。locate指令利用事先建立的系統(tǒng)中所有文件路徑名稱及路徑的locate數(shù)據(jù)庫(kù)實(shí)現(xiàn)快速定位給定的文件淆院。locate指令無(wú)需遍歷整個(gè)文件系統(tǒng),查詢速度較快帆精。為了保證查詢結(jié)構(gòu)的準(zhǔn)確度,管理員必須定期更新locate時(shí)刻贡珊。
- 基本語(yǔ)法
locate 搜索文件 - 特別說(shuō)明
由于locate指令基于數(shù)據(jù)庫(kù)進(jìn)行查詢,所以第一次運(yùn)行前,必須使用updatedb指令創(chuàng)建locate數(shù)據(jù)庫(kù) - 應(yīng)用實(shí)例
案例:請(qǐng)使用locate指令快速定位hello.txt文件所在目錄
[root@localhost ~]# locate hello.txt
/home/hello.txt
grep指令和管道符號(hào)|
grep 過(guò)濾查找,管道符"|",表示將前一個(gè)命令的處理結(jié)果輸出傳遞給后面的命令處理忙厌。
- 基本語(yǔ)法
grep [選項(xiàng)] 查找內(nèi)容 源文件 - 常用選項(xiàng)
選項(xiàng) | 功能 |
---|---|
-n | 顯示匹配行及行號(hào) |
-i | 忽略字母大小寫 |
- 應(yīng)用實(shí)例
案例:請(qǐng)?jiān)趆ello.txt文件中,查找"yes"所在行,并且顯示行號(hào)
[root@localhost home]# cat hello.txt
hello
hello200
hello300
yes
jjj
yyyy
yes
helloabc
[root@localhost home]# cat hello.txt | grep -n yes
4:yes
7:yes
[root@localhost home]# cat hello.txt | grep -ni YES
4:yes
7:yes