linux查找命令詳解find、locate、whereis、which、type

我們經常需要在系統中查找一個文件,那麼在Linux系統中我們如何準確高效的確定一個文件在系統中的具體位置呢?一下我總結了在linux系統中用於查找文件的幾個命令。

1、find命令

find是最常用也是最強大的查找命令,它可以查找任何類型的文件。

find命令的一般格式爲:find <指定目錄><指定條件><指定動作>,即find pathname -options [-print -exec -ok]

參數解釋:

pathname:pathname爲搜索的目錄及其子目錄,默認情況下爲當前目錄

常用的option選項:

-name:按文件名來查找文件

-user:按照文件的屬主來查找文件

-group:按照文件所屬的組來查找文件

-perm:按照文件權限來查找文件

-prune:不在當前指定目錄中查找

例如:已知在/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
 
[root@Gin scripts]# cp /etc/passwd ./
find / \( -path /usr/bin -o -path /etc \) -prune -o -name "passwd" -print  ##爲在當前目錄及其子目錄(除了/usr/bin目錄及其子目錄;/etc目錄及其子目錄)下查找名爲passwd的文件。由於命令行不能直接識別圓括號,因此要用轉義字符\,在和前後都要有空格。
/gin/scripts/passwd

注意:find命令不加任何參數時,表示搜索路徑爲當前目錄及其子目錄,默認的動作爲-print,即不過濾任何結果,也就是說輸出所有的文件。

-mtime -n +n:按照文件修改時間來查找文件,-n表示文件修改時間距現在n天以內,+n表示文件修改時間據現在n天以前

-type:查找某一類型的文件(b:塊設備文件;d:目錄文件;c:字符設備文件;p:管道文件;l:鏈接文件;f:普通文件)

-nogroup:查找無有效所屬組的文件,即文件所屬的組在/etc/group中不存在

-nouser;查找無有效所屬主的文件,即文件的所屬主在/etc/passwd中不存在

2、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照樣能搜索到該文件的信息,,儘管此時該文件已經不存在了。

3、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:
4、which命令

 

4、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

 

5、type命令

type命令主要用於區分一個命令到底是shell自帶的還是外部獨立的二進制文件提供的。如果是shell自帶的則會提示此命令爲shell buildin,否則會列出命令的位置。例如:cd爲shell自帶的命令,當用which查找時,which會按照PATH變量設置的路徑進行搜索,結果顯示no cd in...;用type cd則顯示cd爲shell buildin命令。ssh不是shell自帶命令,用type時會顯示ssh的路徑。

 

[root@Gin scripts]# which cd
/usr/bin/which: no cd in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@Gin scripts]# type cd
cd is a shell builtin
[root@Gin scripts]# type ssh
ssh is /usr/bin/ssh

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章