我們經常需要在系統中查找一個文件,那么在Linux系統中我們如何准確高效的確定一個文件在系統中的具體位置呢?下面我總結了在Linux系統中用於查找文件的幾個命令:
一、find命令
find是最常用也是最強大的查找命令,它可以查找任何類型的文件。find命令的一般格式為:find <指定目錄><指定條件><指定動作>,即find pathname -options [-print -exec -ok]
參數解釋:
pathname:pathname為搜索的目錄及其子目錄,默認情況下為當前目錄
常用的option選項:
-name:按文件名來查找文件
-user:按照文件的所屬用戶來查找文件
-group:按照文件所屬的組來查找文件
-perm:按照文件權限來查找文件
-prune:不在當前指定目錄中查找
-mtime -n +n:按照文件修改時間來查找文件,-n表示文件修改時間距現在n天以內,+n表示文件修改時間據現在n天以前
-type:查找某一類型的文件(b:塊設備文件;d:目錄文件;c:字符設備文件;p:管道文件;l:鏈接文件;f:普通文件)
-nogroup:查找無有效所屬組的文件,即文件所屬的組在/etc/group中不存在
-nouser:查找無有效所屬用戶的文件,即文件的所屬用戶在/etc/passwd中不存在
例如:已知在/etc,/etc/pam.d以及/user/bin目錄下都有一個名為passwd的文件,我們看一下-prune的作用
[root@Gin /]# find -name "passwd" #在當前目錄及其子目錄下查找名為passwd的文件 ./usr/bin/passwd ./etc/passwd ./etc/pam.d/passwd [root@Gin /]# find . -path ./etc -prune -o -name "passwd" -print #在當前目錄及其子目錄(除了/etc目錄及其子目錄)下查找名為passwd的文件。 ./usr/bin/passwd
[root@Gin /]# find . -path ./etc -prune -o -name "passwd" #區別帶-print參數與不帶-print參數 ./usr/bin/passwd ./etc [root@Gin /]# find . -path ./usr -prune -o -name "passwd" -print #在當前目錄及其子目錄(除了/usr目錄及其子目錄)下查找名為passwd的文件。 ./etc/passwd ./etc/pam.d/passwd
注意:find命令不加任何參數時,表示搜索路徑為當前目錄及其子目錄,默認的動作為-print,即不過濾任何結果,也就是說輸出所有的文件。
二、locate命令
locate命令實際是"find -name"的另一種寫法,但是查找方式跟find不同,它比find快得多。因為它不搜索具體目錄,而是在一個數據庫(/var/lib/locatedb)中搜索指定的文件。此數據庫含有本地文件的所有信息,此數據庫是Linux系統自動創建的,數據庫由updatedb程序來自動更新,updatedb是由cron daemon周期性建立的,默認情況下為每天更新一次,所以用locate命令你搜索不到最新更新的文件,除非你在用locate命令查找文件之前手動的用updatedb命令更新數據庫。
[root@Gin scripts]# echo "I love linux" >locatetest [root@Gin scripts]# locate locatetest ##更新數據庫前 [root@Gin scripts]# find -name "locatetest" ./locatetest [root@Gin scripts]# updatedb ##更新數據庫后 [root@Gin scripts]# locate locatetest /gin/scripts/locatetest [root@Gin scripts]# rm -f locatetest ##執行刪除文件后 [root@Gin scripts]# find -name "locatetest" [root@Gin scripts]# locate locatetest /gin/scripts/locatetest [root@Gin scripts]# locate locatetest [root@Gin scripts]#
注意:每次有新文件更新和刪除之后,在updatedb之前數據庫中保存的文件信息不會改變,即新添加一個文件之后,updatedb之前用locate搜索不到指定的文件。同樣再刪除一個文件信息已經在數據庫中的文件時,updatedb之前用locate照樣能搜索到該文件的信息,盡管此時該文件已經不存在了。
三、whereis命令
whereis命令用的不多,因為它只能用來查找二進制文件(-b)、源代碼文件(-s)、說明文件(-m)。如果省略參數則返回所有的信息。
[root@Gin scripts]# whereis --help whereis [ -sbmu ] [ -SBM dir ... -f ] name... [root@Gin scripts]# whereis find find: /bin/find /usr/bin/find /usr/share/man/man1/find.1.gz /usr/share/man/man1p/find.1p.gz [root@Gin scripts]# whereis -b find find: /bin/find /usr/bin/find [root@Gin scripts]# whereis -m find find: /usr/share/man/man1/find.1.gz /usr/share/man/man1p/find.1p.gz [root@Gin scripts]# whereis -s find find:
四、which命令
which命令是在PATH變量指定的路徑中搜索指定的系統命令的位置。用echo $PATH可顯示當前PATH變量的值。
[root@Gin scripts]# echo $PATH /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@Gin scripts]# which find /bin/find
五、type命令
type命令主要用於區分一個命令到底是shell自帶的還是外部獨立的二進制文件提供的。如果是shell自帶的則會提示此命令為shell buildin,否則會列出命令的位置。例如:cd為shell自帶的命令,當用which查找時,which會按照PATH變量設置的路徑進行搜索,結果顯示no cd in...;用type cd則顯示cd為shell buildin命令。ssh不是shell自帶命令,用type時會顯示ssh的路徑。
➜ / type cd cd is a shell builtin
➜ / type ssh ssh is /usr/bin/ssh