天天看點

Linux下的檔案查找指令

Linux的五個檔案查找指令:find,locate,whereis,which,type

find:查找檔案或目錄所在路徑 

格式:find [路徑] [表達式] 

表達式: 

-name    :查找名為filename的檔案 

-perm    :按執行權限來查找 

-empty   :查找空檔案或空目錄 

-user    :按檔案屬主來查找 

-group   :按組來查找 

-nogroup   :查無有效屬組的檔案,即檔案的屬組在/etc/groups中不存在 

-nouser  :查無有效屬主的檔案,即檔案的屬主在/etc/passwd中不存 

-mtime   :按檔案更改時間來查找檔案 

-atime   :按檔案通路時間來查找檔案 

-ctime   :按檔案建立時間來查找檔案 

-newer     :查更改時間更新的檔案或目錄 

-type    :查是塊裝置b、目錄d、字元裝置c、管道p、符号連結l、普通檔案f 

-size n[c] :查找大小為n塊(512位元組)[或n位元組]的檔案 

-inum    :根據i節點查找 

-depth   :使查找在進入子目錄前先行查找完本目錄 

-fstype  :查位于某一類型檔案系統中的檔案,這些檔案系統類型通常可 在/etc/fstab中找到 

-mount   :查檔案時不跨越檔案系統mount點 

-cpio    :對比對的檔案使用cpio指令,将他們備份到錄音帶裝置中 

-prune   :忽略某個目錄 

-maxdepth  :查詢的目錄深度 

-exec   :查找檔案并執行後面的指令 find ... -exec CMD {} \; 

-ok     :詢問是否要執行後面的指令 find ... -ok CMD {} \;  

-perm mode 表示嚴格比對

-perm -mode         表示mode中轉換成二進制的1必須全部比對(不管0位) 

-perm +mode         表示mode中轉換成二進制的1必須部分比對(不管0位) 

-ctime/atime/mtime/cmin/amin/mmin:按時間查找

以天為機關的 :ctime、atime、mtime

以分鐘為機關的 :cmin、amin、mmin

c--change 表示檔案的屬性被修改過

a--access

m--modify 表示檔案的内容被修改過

+n 表示n天以前

-n 表示n天以内

[root@rhel6 ~]# find /etc/ -name "host*"                 "查詢/etc/目錄(包括子目錄)中以host開頭的檔案或目錄" 

[root@rhel6 ~]# find -type l                         "查詢目前目錄下檔案類型為連結的檔案" 

[root@rhel6 ~]# find -size +10000000c                "查詢目前目錄中>10M的檔案" 

[root@rhel6 ~]# find -size -1K                       "查詢目前目錄中<1K的檔案" 

[root@rhel6 ~]# find /etc -name inittab -o -size +17M    "查詢/etc/目錄中檔案名為inittab或檔案>17M的檔案" 

[root@rhel6 ~]# find /etc -name "*.conf" [-a] -size +20k "查詢/etc/目錄中檔案名為*.conf且檔案<20k的檔案" 

[root@rhel6 ~]# find /etc/* -name "*.conf" -not -name "*http*"  "查詢/etc目錄中檔案名為*.conf但不包含http的檔案"     

[root@rhel6 ~]# find /etc/ -empty                    "查詢/etc/目錄中的空檔案或空目錄" 

[root@rhel6 ~]# find /var -user oracle                   "查詢/var/目錄中屬于使用者oracle的檔案或目錄" 

[root@rhel6 ~]# find /home -group xfcy   

[root@rhel6 ~]# find -inum 1024                  "查詢目前目錄中 i 節點為1024的檔案或目錄" 

[root@rhel6 ~]# find -newer new                  "查詢目前目錄中比檔案new還新的檔案或目錄" 

[root@rhel6 ~]# find /etc/ -nouser -o -nogroup           "查詢/etc/目錄中不屬于本地使用者的檔案或目錄(危險檔案)" 

[root@rhel6 ~]# find /data/ -mmin -10                "查詢/data/目錄中十分鐘内檔案内容被修改過的檔案" 

[root@rhel6 ~]# find /proc/ -type f -maxdepth 1          "查詢/data/目錄中檔案類型為普通檔案的檔案且不查詢子目錄" 

[root@rhel6 ~]# find /data/ -mtime -10 -exec rm {} \;    "查詢/data/目錄中十分鐘内内容被修改過的檔案并将其删除" 

[root@rhel6 ~]# find /data/ -mtime -10 -ok rm {} \;      "查詢/data/目錄中十分鐘内内容被修改過的檔案并詢問是否将其删除(y/n)" 

 =================================================================================

locate:根據檔案資料庫updatedb查找檔案或目錄

locate不搜尋具體目錄,而是搜尋一個資料庫(/var/lib/locatedb),這個資料庫中含有本地所有檔案資訊(預設沒有掃描外接的移動硬碟或者挂載在/media下的其他分區).

Linux系統自動建立這個資料庫,并且每天自動更新一次(crontab),是以使用locate指令查不到最新變動過的檔案.為了避免這種情況,可以在使用locate之前,先使用updatedb指令,手動更新資料庫.

[root@rhel6 ~]# locate /etc/sh                   "查詢/etc目錄下所有以sh開頭的檔案" 

 ====================================================================================

whereis:隻能用于程式名的搜尋,而且隻搜尋二進制檔案(參數-b)、man說明檔案(參數-m)和源代碼檔案(參數-s)。如果省略參數,則傳回所有資訊。

[root@rhel6 ~]# whereis find 

find: /usr/bin/find /usr/share/man/man1/find.1.gz /usr/share/man/man1p/find.1p.gz 

[root@rhel6 ~]# whereis -b find 

find: /usr/bin/find 

====================================================================================

which:在PATH變量指定的路徑中,搜尋某個系統指令的位置,并且傳回第一個搜尋結果。即使用which指令,就可以看到某個系統指令是否存在,以及執行的到底是哪一個位置的指令。

[root@rhel6 ~]# which find 

/usr/bin/find 

type:其實不能算查找指令,它是用來區分某個指令到底是由shell自帶的,還是由shell外部的獨立二進制檔案提供的。如果一個指令是外部指令,那麼使用-p參數,會顯示該指令的路徑,相當于which指令。

[root@rhel6 ~]# type cd 

cd is a shell builtin 

[root@rhel6 ~]# type -p find

/bin/find

本文轉自Vnimos51CTO部落格,原文連結:http://blog.51cto.com/vnimos/1161355,如需轉載請自行聯系原作者