在linux下搜索文件只需要幾行簡(jiǎn)單的命令就搞定了,不過(guò)不同的指令其功能也是不一樣的,使用的時(shí)候要靈活選擇。
-
whereis簡(jiǎn)單快速
whereis <filename>
直接從數(shù)據(jù)庫(kù)查詢,但是只能搜索二進(jìn)制文件bin,man幫助文檔,源代碼文件-s
-
locate快&全
通過(guò)/var/lib/mlocate.db數(shù)據(jù)庫(kù)查找。
不過(guò)這個(gè)數(shù)據(jù)庫(kù)不是實(shí)時(shí)更新的,系統(tǒng)會(huì)使用定時(shí)任務(wù)每天自動(dòng)執(zhí)行updatedb更新一次,所以剛添加的文件要手動(dòng)執(zhí)行一次updatedb才能找得到。
它可以用來(lái)查找指定目錄下的不同文件類型,比如查找/etc下所有sh開(kāi)頭的文件 :
locate /etc/sh
它不只會(huì)在etc目錄下查找并且會(huì)自動(dòng)遞歸子目錄進(jìn)行查找
又比如查找某目錄下所有jpg文件:
locate /usr/share/\*.jpg
-
whichshell內(nèi)建的一個(gè)命令。通常使用which來(lái)確定是否安裝了某個(gè)指定的軟件,因?yàn)樗粡腜ATH路徑去搜索
which man
-
find功能最強(qiáng)大也最復(fù)雜
可以通過(guò)文件類型,文件名,文件屬性(時(shí)間,權(quán)限)進(jìn)行搜索
基本格式:
find <path> <option> <action>
比如和時(shí)間相關(guān)的一些命令:
-atime //last access time
-ctime //created time
-mtime //last edit time
以-mtime 參數(shù)舉例:
-mtime n //edited the day n days ago
-mtime +n //edited n days ago excluding nth day
-mtime -n //edited n days ago including nth day
-newer <file> //edited newer than <file>
e.g.:
find ~ -mtime 0
24小時(shí)內(nèi)有改動(dòng)
find ~ -newer /home/user/code