天天看點

Linux指令--which,whereis,locate,find

原文出處:http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html。感謝作者無私分享

  • which

我們經常在linux要查找某個檔案,但不知道放在哪裡了,可以使用下面的一些指令來搜尋: 

       which  檢視可執行檔案的位置。

       whereis 檢視檔案的位置。 

       locate   配合資料庫檢視檔案位置。

       find   實際搜尋硬碟查詢檔案名稱。

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

1.指令格式:

which 可執行檔案名稱 

2.指令功能:

which指令會在PATH變量指定的路徑中,搜尋某個系統指令的位置,并且傳回第一個搜尋結果。

3.指令參數:

-n  指定檔案名長度,指定的長度必須大于或等于所有檔案中最長的檔案名。

-p  與-n參數相同,但此處的包括了檔案的路徑。

-w  指定輸出時欄位的寬度。

-V  顯示版本資訊

4.使用執行個體:

執行個體1:查找檔案、顯示指令路徑

指令:

which lsmod

輸出:

[[email protected] ~]# which pwd

/bin/pwd

[root@localhost ~]#  which adduser

/usr/sbin/adduser

[root@localhost ~]#

說明:

which 是根據使用者所配置的 PATH 變量内的目錄去搜尋可運作檔的!是以,不同的 PATH 配置内容所找到的指令當然不一樣的!

執行個體2:用 which 去找出 which

指令:

  which which

輸出:

[[email protected] ~]# which which

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot  --show-tilde'

     /usr/bin/which

[root@localhost ~]#

說明:

竟然會有兩個 which ,其中一個是 alias 這就是所謂的『指令别名』,意思是輸入 which 會等於後面接的那串指令!

執行個體3:找出 cd 這個指令

指令:

 which cd

輸出:

Linux指令--which,whereis,locate,find

       說明:

cd 這個常用的指令竟然找不到啊!為什麼呢?這是因為 cd 是bash 内建的指令! 但是 which 預設是找 PATH 内所規範的目錄,是以當然一定找不到的!

  • whereis

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

和find相比,whereis查找的速度非常快,這是因為linux系統會将 系統内的所有檔案都記錄在一個資料庫檔案中,當使用whereis和下面即将介紹的locate時,會從資料庫中查找資料,而不是像find指令那樣,通 過周遊硬碟來查找,效率自然會很高。 

但是該資料庫檔案并不是實時更新,預設情況下時一星期更新一次,是以,我們在用whereis和locate 查找檔案時,有時會找到已經被删除的資料,或者剛剛建立檔案,卻無法查找到,原因就是因為資料庫檔案沒有被更新。 

1.指令格式:

whereis [-bmsu] [BMS 目錄名 -f ] 檔案名

2.指令功能:

whereis指令是定位可執行檔案、源代碼檔案、幫助檔案在檔案系統中的位置。這些檔案的屬性應屬于原始代碼,二進制檔案,或是幫助檔案。whereis 程式還具有搜尋源代碼、指定備用搜尋路徑和搜尋不尋常項的能力。

3.指令參數:

-b   定位可執行檔案。

-m   定位幫助檔案。

-s   定位源代碼檔案。

-u   搜尋預設路徑下除可執行檔案、源代碼檔案、幫助檔案以外的其它檔案。

-B   指定搜尋可執行檔案的路徑。

-M   指定搜尋幫助檔案的路徑。

-S   指定搜尋源代碼檔案的路徑。

4.使用執行個體:

執行個體1:将和**檔案相關的檔案都查找出來

指令:

whereis svn

輸出:

[[email protected] ~]# whereis tomcat

tomcat:

[root@localhost ~]# whereis svn

svn: /usr/bin/svn /usr/local/svn /usr/share/man/man1/svn.1.gz

說明:

tomcat沒安裝,找不出來,svn安裝找出了很多相關檔案

執行個體2:隻将二進制檔案 查找出來 

指令:

whereis -b svn

輸出: 

[[email protected] ~]# whereis -b svn

svn: /usr/bin/svn /usr/local/svn

[root@localhost ~]# whereis -m svn

svn: /usr/share/man/man1/svn.1.gz

[root@localhost ~]# whereis -s svn

svn:

[root@localhost ~]#

說明:

whereis -m svn 查出說明文檔路徑,whereis -s svn 找source源檔案。

  • locate

locate 讓使用者可以很快速的搜尋檔案系統内是否有指定的檔案。其方法是先建立一個包括系統内所有檔案名稱及路徑的資料庫,之後當尋找時就隻需查詢這個資料庫,而不必實際深入檔案系統之中了。在一般的 distribution 之中,資料庫的建立都被放在 crontab 中自動執行。

1.指令格式:

Locate [選擇參數] [樣式]

2.指令功能:

locate指令可以在搜尋資料庫時快速找到檔案,資料庫由updatedb程式來更新,updatedb是由cron daemon周期性建立的,locate指令在搜尋資料庫時比由整個由硬碟資料來搜尋資料來得快,但較差勁的是locate所找到的檔案若是最近才建立或 剛更名的,可能會找不到,在内定值中,updatedb每天會跑一次,可以由修改crontab來更新設定值。(etc/crontab)

locate指定用在搜尋符合條件的檔案,它會去儲存檔案與目錄名稱的資料庫内,尋找合乎範本樣式條件的檔案或目錄錄,可以使用特殊字元(如”*” 或”?”等)來指定範本樣式,如指定範本為kcpa*ner, locate會找出所有起始字串為kcpa且結尾為ner的檔案或目錄,如名稱為kcpartner若目錄錄名稱為kcpa_ner則會列出該目錄下包括 子目錄在内的所有檔案。

locate指令和find找尋檔案的功能類似,但locate是透過update程式将硬碟中的所有檔案和目錄資料先建立一個索引資料庫,在 執行loacte時直接找該索引,查詢速度會較快,索引資料庫一般是由作業系統管理,但也可以直接下達update強迫系統立即修改索引資料庫。

3.指令參數:

-e   将排除在尋找的範圍之外。

-1  如果 是 1.則啟動安全模式。在安全模式下,使用者不會看到權限無法看到 的檔案。這會始速度減慢,因為 locate 必須至實際的檔案系統中取得檔案的 權限資料。

-f   将特定的檔案系統排除在外,例如我們沒有到理要把 proc 檔案系統中的檔案 放在資料庫中。

-q  安靜模式,不會顯示任何錯誤訊息。

-n 至多顯示 n個輸出。

-r 使用正規運算式 做尋找的條件。

-o 指定資料庫存的名稱。

-d 指定資料庫的路徑

-h 顯示輔助訊息

-V 顯示程式的版本訊息

4.使用執行個體:

執行個體1:查找和pwd相關的所有檔案

指令:

locate pwd

輸出:

peida-VirtualBox ~ # locate pwd

/bin/pwd

/etc/.pwd.lock

/sbin/unix_chkpwd

/usr/bin/pwdx

/usr/include/pwd.h

/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.py

/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.pyc

/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.py

/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyc

/usr/lib/syslinux/pwd.c32

/usr/share/help/C/empathy/irc-join-pwd.page

/usr/share/help/ca/empathy/irc-join-pwd.page

/usr/share/help/cs/empathy/irc-join-pwd.page

/usr/share/help/de/empathy/irc-join-pwd.page

/usr/share/help/el/empathy/irc-join-pwd.page

執行個體2: 搜尋etc目錄下所有以sh開頭的檔案 

指令:

locate /etc/sh

輸出:

peida-VirtualBox ~ # locate /etc/sh

/etc/shadow

/etc/shadow-

/etc/shells

peida-VirtualBox ~ #

執行個體3:搜尋etc目錄下,所有以m開頭的檔案

指令:

locate /etc/m

輸出:

peida-VirtualBox ~ # locate /etc/m

/etc/magic

/etc/magic.mime

/etc/mailcap

/etc/mailcap.order

/etc/manpath.config

/etc/mate-settings-daemon

  • find

Linux下find指令在目錄結構中搜尋檔案,并執行指定的操作。Linux下find指令提供了相當多的查找條件,功能很強大。由于find具有強大的功能,是以它的選項也很多,其中大部分選項都值得我們花時間來了解一下。即使系統中含有網絡檔案系統( NFS),find指令在該檔案系統中同樣有效,隻你具有相應的權限。 在運作一個非常消耗資源的find指令時,很多人都傾向于把它放在背景執行,因為周遊一個大的檔案系統可能會花費很長的時間(這裡是指30G位元組以上的檔案系統)。

1.指令格式:

find pathname -options [-print -exec -ok ...]

2.指令功能:

用于在檔案樹種查找檔案,并作出相應的處理 

3.指令參數:

pathname: find指令所查找的目錄路徑。例如用.來表示目前目錄,用/來表示系統根目錄。 

-print: find指令将比對的檔案輸出到标準輸出。 

-exec: find指令對比對的檔案執行該參數所給出的shell指令。相應指令的形式為'command' {  } \;,注意{   }和\;之間的空格。 

-ok: 和-exec的作用相同,隻不過以一種更為安全的模式來執行該參數所給出的shell指令,在執行每一個指令之前,都會給出提示,讓使用者來确定是否執行。

4.指令選項:

-name   按照檔案名查找檔案。

-perm   按照檔案權限來查找檔案。

-prune  使用這一選項可以使find指令不在目前指定的目錄中查找,如果同時使用-depth選項,那麼-prune将被find指令忽略。

-user   按照檔案屬主來查找檔案。

-group  按照檔案所屬的組來查找檔案。

-mtime -n +n  按照檔案的更改時間來查找檔案, - n表示檔案更改時間距現在n天以内,+ n表示檔案更改時間距現在n天以前。find指令還有-atime和-ctime 選項,但它們都和-m time選項。

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

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

-newer file1 ! file2  查找更改時間比檔案file1新但比檔案file2舊的檔案。

-type  查找某一類型的檔案,諸如:

b - 塊裝置檔案。

d - 目錄。

c - 字元裝置檔案。

p - 管道檔案。

l - 符号連結檔案。

f - 普通檔案。

-size n:[c] 查找檔案長度為n塊的檔案,帶有c時表示檔案長度以位元組計。-depth:在查找檔案時,首先查找目前目錄中的檔案,然後再在其子目錄中查找。

-fstype:查找位于某一類型檔案系統中的檔案,這些檔案系統類型通常可以在配置檔案/etc/fstab中找到,該配置檔案中包含了本系統中有關檔案系統的資訊。

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

-follow:如果find指令遇到符号連結檔案,就跟蹤至連結所指向的檔案。

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

另外,下面三個的差別:

-amin n   查找系統中最後N分鐘通路的檔案

-atime n  查找系統中最後n*24小時通路的檔案

-cmin n   查找系統中最後N分鐘被改變檔案狀态的檔案

-ctime n  查找系統中最後n*24小時被改變檔案狀态的檔案

-mmin n   查找系統中最後N分鐘被改變檔案資料的檔案

-mtime n  查找系統中最後n*24小時被改變檔案資料的檔案

5.使用執行個體:

執行個體1:查找指定時間内修改過的檔案 

指令:

           find -atime -2

輸出:

[[email protected] ~]# find -atime -2

.

./logs/monitor

./.bashrc

./.bash_profile

./.bash_history

說明:

超找48小時内修改過的檔案 

執行個體2:根據關鍵字查找 

指令:

find . -name "*.log"

輸出:

[[email protected] test]# find . -name "*.log" 

./log_link.log

./log2014.log

./test4/log3-2.log

./test4/log3-3.log

./test4/log3-1.log

./log2013.log

./log2012.log

./log.log

./test5/log5-2.log

./test5/log5-3.log

./test5/log.log

./test5/log5-1.log

./test5/test3/log3-2.log

./test5/test3/log3-3.log

./test5/test3/log3-1.log

./test3/log3-2.log

./test3/log3-3.log

./test3/log3-1.log

說明:

在目前目錄查找 以.log結尾的檔案。 ". "代表目前目錄 

執行個體3:按照目錄或檔案的權限來查找檔案

指令:

find /opt/soft/test/ -perm 777

輸出:

[[email protected] test]# find /opt/soft/test/ -perm 777

/opt/soft/test/log_link.log

/opt/soft/test/test4

/opt/soft/test/test5/test3

/opt/soft/test/test3

說明: 

查找/opt/soft/test/目錄下 權限為 777的檔案

執行個體4:按類型查找 

指令:

find . -type f -name "*.log"

輸出:

[[email protected] test]# find . -type f -name "*.log"

./log2014.log

./test4/log3-2.log

./test4/log3-3.log

./test4/log3-1.log

./log2013.log

./log2012.log

./log.log

./test5/log5-2.log

./test5/log5-3.log

./test5/log.log

./test5/log5-1.log

./test5/test3/log3-2.log

./test5/test3/log3-3.log

./test5/test3/log3-1.log

./test3/log3-2.log

./test3/log3-3.log

./test3/log3-1.log

[root@localhost test]#

說明:

查找當目錄,以.log結尾的普通檔案 

執行個體5:查找目前所有目錄并排序

指令:

find . -type d | sort

輸出:

[[email protected] test]# find . -type d | sort

.

./scf

./scf/bin

./scf/doc

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/info

./scf/service/deploy/product

./test3

./test4

./test5

./test5/test3

[root@localhost test]#

執行個體6:按大小查找檔案

指令:

find . -size +1000c -print

輸出:

[[email protected] test]#  find . -size +1000c -print

.

./test4

./scf

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/product

./scf/service/deploy/info

./scf/doc

./scf/bin

./log2012.log

./test5

./test5/test3

./test3

[root@localhost test]#

說明:

查找目前目錄大于1K的檔案 

find是我們很常用的一個Linux指令,但是我們一般查找出來的并不僅僅是看看而已,還會有進一步的操作,這個時候exec的作用就顯現出來了。 

exec解釋:

-exec  參數後面跟的是command指令,它的終止是以;為結束标志的,是以這句指令後面的分号是不可缺少的,考慮到各個系統中分号會有不同的意義,是以前面加反斜杠。

{}   花括号代表前面find查找出來的檔案名。

使用find時,隻要把想要的操作寫在一個檔案裡,就可以用exec來配合find查找,很友善的。在有些作業系統中隻允許-exec選項執行諸如l s或ls -l這樣的指令。大多數使用者使用這一選項是為了查找舊檔案并删除它們。建議在真正執行rm指令删除檔案之前,最好先用ls指令看一下,确認它們是所要删除的檔案。 exec選項後面跟随着所要執行的指令或腳本,然後是一對兒{ },一個空格和一個\,最後是一個分号。為了使用exec選項,必須要同時使用print選項。如果驗證一下find指令,會發現該指令隻輸出從目前路徑起的相對路徑及檔案名。

執行個體1:ls -l指令放在find指令的-exec選項中 

指令:

find . -type f -exec ls -l {} \;

輸出: 

[[email protected] test]# find . -type f -exec ls -l {} \; 

-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log

-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log

-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log

-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log

-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log

[root@localhost test]#

說明: 

上面的例子中,find指令比對到了目前目錄下的所有普通檔案,并在-exec選項中使用ls -l指令将它們列出。

執行個體2:在目錄中查找更改時間在n日以前的檔案并删除它們

指令:

find . -type f -mtime +14 -exec rm {} \; 

輸出:

[[email protected] test]# ll

總計 328

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     33 10-28 16:54 log2013.log

-rw-r--r-- 1 root root    127 10-28 16:51 log2014.log

lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -> log.log

-rw-r--r-- 1 root root     25 10-28 17:02 log.log

-rw-r--r-- 1 root root     37 10-28 17:07 log.txt

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 10-28 14:47 test3

drwxrwxrwx 2 root root   4096 10-28 14:47 test4

[root@localhost test]# find . -type f -mtime +14 -exec rm {} \;

[[email protected] test]# ll

總計 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -> log.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# 

說明:

在shell中用任何方式删除檔案之前,應當先檢視相應的檔案,一定要小心!當使用諸如mv或rm指令時,可以使用-exec選項的安全模式。它将在對每個比對到的檔案進行操作之前提示你。 

執行個體3:在目錄中查找更改時間在n日以前的檔案并删除它們,在删除之前先給出提示

指令:

find . -name "*.log" -mtime +5 -ok rm {} \;

輸出:

[[email protected] test]# ll

總計 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -> log.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} \;

< rm ... ./log_link.log > ? y

< rm ... ./log2012.log > ? n

[root@localhost test]# ll

總計 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]#

說明:

在上面的例子中, find指令在目前目錄中查找所有檔案名以.log結尾、更改時間在5日以上的檔案,并删除它們,隻不過在删除之前先給出提示。 按y鍵删除檔案,按n鍵不删除。 

執行個體4:-exec中使用grep指令

指令:

find /etc -name "passwd*" -exec grep "root" {} \;

輸出:

[[email protected] test]# find /etc -name "passwd*" -exec grep "root" {} \;

root:x:0:0:root:/root:/bin/bash

root:x:0:0:root:/root:/bin/bash

[root@localhost test]#

說明:

任何形式的指令都可以在-exec選項中使用。  在上面的例子中我們使用grep指令。find指令首先比對所有檔案名為“ passwd*”的檔案,例如passwd、passwd.old、passwd.bak,然後執行grep指令看看在這些檔案中是否存在一個root使用者。

執行個體5:查找檔案移動到指定目錄  

指令:

find . -name "*.log" -exec mv {} .. \;

輸出:

[[email protected] test]# ll

總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxr-x 2 root root 4096 11-12 22:49 test3

drwxrwxr-x 2 root root 4096 11-12 19:32 test4

[root@localhost test]# cd test3/

[[email protected] test3]# ll

總計 304

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

[root@localhost test3]# find . -name "*.log" -exec mv {} .. \;

[[email protected] test3]# ll

總計 0[root@localhost test3]# cd ..

[[email protected] test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 22:50 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]#

執行個體6:用exec選項執行cp指令  

指令:

find . -name "*.log" -exec cp {} test3 \;

輸出:

[[email protected] test3]# ll

總計 0[root@localhost test3]# cd ..

[[email protected] test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 22:50 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -name "*.log" -exec cp {} test3 \;

cp: “./test3/log2014.log” 及 “test3/log2014.log” 為同一檔案

cp: “./test3/log2013.log” 及 “test3/log2013.log” 為同一檔案

cp: “./test3/log2012.log” 及 “test3/log2012.log” 為同一檔案

[root@localhost test]# cd test3

[[email protected] test3]# ll

總計 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

[root@localhost test3]#

在使用 find指令的-exec選項處理比對到的檔案時, find指令将所有比對到的檔案一起傳遞給exec執行。但有些系統對能夠傳遞給exec的指令長度有限制,這樣在find指令運作幾分鐘之後,就會出現溢出錯誤。錯誤資訊通常是“參數列太長”或“參數列溢出”。這就是xargs指令的用處所在,特别是與find指令一起使用。  

find指令把比對到的檔案傳遞給xargs指令,而xargs指令每次隻擷取一部分檔案而不是全部,不像-exec選項那樣。這樣它可以先處理最先擷取的一部分檔案,然後是下一批,并如此繼續下去。  

在有些系統中,使用-exec選項會為處理每一個比對到的檔案而發起一個相應的程序,并非将比對到的檔案全部作為參數一次執行;這樣在有些情況下就會出現程序過多,系統性能下降的問題,因而效率不高; 而使用xargs指令則隻有一個程序。另外,在使用xargs指令時,究竟是一次擷取所有的參數,還是分批取得參數,以及每一次擷取參數的數目都會根據該指令的選項及系統核心中相應的可調參數來确定。

使用執行個體:

執行個體1: 查找系統中的每一個普通檔案,然後使用xargs指令來測試它們分别屬于哪類檔案 

指令:

find . -type f -print | xargs file

輸出:

[[email protected] test]# ll

總計 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -type f -print | xargs file

./log2014.log: empty

./log2013.log: empty

./log2012.log: ASCII text

[root@localhost test]#

執行個體2:在整個系統中查找記憶體資訊轉儲檔案(core dump) ,然後把結果儲存到/tmp/core.log 檔案中

指令:

 find / -name "core" -print | xargs echo "" >/tmp/core.log

輸出:

[[email protected] test]# find / -name "core" -print | xargs echo "" >/tmp/core.log

[[email protected] test]# cd /tmp

[[email protected] tmp]# ll

總計 16

-rw-r--r-- 1 root root 1524 11-12 22:29 core.log

drwx------ 2 root root 4096 11-12 22:24 ssh-TzcZDx1766

drwx------ 2 root root 4096 11-12 22:28 ssh-ykiRPk1815

drwx------ 2 root root 4096 11-03 07:11 vmware-root

執行個體3:在目前目錄下查找所有使用者具有讀、寫和執行權限的檔案,并收回相應的寫權限

指令:

find . -perm -7 -print | xargs chmod o-w

輸出:

[[email protected] test]# ll

總計 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -perm -7 -print | xargs chmod o-w

[[email protected] test]# ll

總計 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 19:32 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]#

說明:

執行指令後,檔案夾scf、test3和test4的權限都發生改變

執行個體4:用grep指令在所有的普通檔案中搜尋hostname這個詞

指令:

find . -type f -print | xargs grep "hostname"

輸出:

[[email protected] test]# find . -type f -print | xargs grep "hostname"

./log2013.log:hostnamebaidu=baidu.com

./log2013.log:hostnamesina=sina.com

./log2013.log:hostnames=true[root@localhost test]#

執行個體5:用grep指令在目前目錄下的所有普通檔案中搜尋hostnames這個詞

指令:

find . -name \* -type f -print | xargs grep "hostnames"

輸出:

[[email protected]peida test]# find . -name \* -type f -print | xargs grep "hostnames"

./log2013.log:hostnamesina=sina.com

./log2013.log:hostnames=true[root@localhost test]#

說明:

注意,在上面的例子中, \用來取消find指令中的*在shell中的特殊含義。  

執行個體6:使用xargs執行mv 

指令:

find . -name "*.log" | xargs -i mv {} test4

輸出:

[[email protected] test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 22:54 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]# cd test4/

[[email protected] test4]# ll

總計 0[root@localhost test4]# cd ..

[[email protected] test]# find . -name "*.log" | xargs -i mv {} test4

[[email protected] test]# ll

總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxr-x 2 root root 4096 11-13 05:50 test3

drwxrwxr-x 2 root root 4096 11-13 05:50 test4

[root@localhost test]# cd test4/

[[email protected] test4]# ll

總計 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

[root@localhost test4]#

執行個體7:find後執行xargs提示xargs: argument line too long解決方法:

指令:

find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

輸出:

[[email protected]pd test4]#  find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

rm -f 

[root@pdtest4]#

說明:

-l1是一次處理一個;-t是處理之前列印出指令

執行個體8:使用-i參數預設的前面輸出用{}代替,-I參數可以指定其他代替字元,如例子中的[] 

指令:

輸出:

[[email protected] test]# ll

總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxr-x 2 root root 4096 11-13 05:50 test3

drwxrwxr-x 2 root root 4096 11-13 05:50 test4

[root@localhost test]# cd test4

[[email protected] test4]# find . -name "file" | xargs -I [] cp [] ..

[[email protected] test4]# ll

總計 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

[root@localhost test4]# cd ..

[[email protected] test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 05:50 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]#

說明:

使用-i參數預設的前面輸出用{}代替,-I參數可以指定其他代替字元,如例子中的[] 

執行個體9:xargs的-p參數的使用 

指令:

find . -name "*.log" | xargs -p -i mv {} ..

輸出:

[[email protected] test3]# ll

總計 0

-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log

[root@localhost test3]# cd ..

[[email protected] test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:06 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]# cd test3

[[email protected] test3]#  find . -name "*.log" | xargs -p -i mv {} ..

mv ./log2015.log .. ?...y

[root@localhost test3]# ll

總計 0[root@localhost test3]# cd ..

[[email protected] test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:08 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]#

說明:

-p參數會提示讓你确認是否執行後面的指令,y執行,n不執行。

find一些常用參數的一些常用執行個體和一些具體用法和注意事項。

1.使用name選項:

檔案名選項是find指令最常用的選項,要麼單獨使用該選項,要麼和其他選項一起使用。  可以使用某種檔案名模式來比對檔案,記住要用引号将檔案名模式引起來。  不管目前路徑是什麼,如果想要在自己的根目錄$HOME中查找檔案名符合*.log的檔案,使用~作為 'pathname'參數,波浪号~代表了你的$HOME目錄。

find ~ -name "*.log" -print  

想要在目前目錄及子目錄中查找所有的‘ *.log‘檔案,可以用: 

find . -name "*.log" -print  

想要的目前目錄及子目錄中查找檔案名以一個大寫字母開頭的檔案,可以用:  

find . -name "[A-Z]*" -print  

想要在/etc目錄中查找檔案名以host開頭的檔案,可以用:  

find /etc -name "host*" -print  

想要查找$HOME目錄中的檔案,可以用:  

find ~ -name "*" -print 或find . -print  

要想讓系統高負荷運作,就從根目錄開始查找所有的檔案。  

find / -name "*" -print  

如果想在目前目錄查找檔案名以一個個小寫字母開頭,最後是4到9加上.log結束的檔案:  

指令:

find . -name "[a-z]*[4-9].log" -print

輸出:

[[email protected] test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:08 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]# find . -name "[a-z]*[4-9].log" -print

./log2014.log

./log2015.log

./test4/log2014.log

[root@localhost test]#

2.用perm選項:

按照檔案權限模式用-perm選項,按檔案權限模式來查找檔案的話。最好使用八進制的權限表示法。  

如在目前目錄下查找檔案權限位為755的檔案,即檔案屬主可以讀、寫、執行,其他使用者可以讀、執行的檔案,可以用:  

[[email protected] test]# find . -perm 755 -print

.

./scf

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/product

./scf/service/deploy/info

./scf/doc

./scf/bin

[root@localhost test]#

還有一種表達方法:在八進制數字前面要加一個橫杠-,表示都比對,如-007就相當于777,-005相當于555,

指令:

find . -perm -005

輸出:

[[email protected] test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:08 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]# find . -perm -005

.

./test4

./scf

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/product

./scf/service/deploy/info

./scf/doc

./scf/bin

./test3

[root@localhost test]#

3.忽略某個目錄:

如果在查找檔案時希望忽略某個目錄,因為你知道那個目錄中沒有你所要查找的檔案,那麼可以使用-prune選項來指出需要忽略的目錄。在使用-prune選項時要當心,因為如果你同時使用了-depth選項,那麼-prune選項就會被find指令忽略。如果希望在test目錄下查找檔案,但不希望在test/test3目錄下查找,可以用:  

指令:

find test -path "test/test3" -prune -o -print

輸出:

[[email protected] soft]# find test -path "test/test3" -prune -o -print

test

test/log2014.log

test/log2015.log

test/test4

test/test4/log2014.log

test/test4/log2013.log

test/test4/log2012.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

[root@localhost soft]#

4.使用find查找檔案的時候怎麼避開某個檔案目錄: 

執行個體1:在test 目錄下查找不在test4子目錄之内的所有檔案

指令:

find test -path "test/test4" -prune -o -print

輸出:

[[email protected] soft]# find test

test

test/log2014.log

test/log2015.log

test/test4

test/test4/log2014.log

test/test4/log2013.log

test/test4/log2012.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

test/test3

[root@localhost soft]# find test -path "test/test4" -prune -o -print

test

test/log2014.log

test/log2015.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

test/test3

[root@localhost soft]#

說明:

find [-path ..] [expression] 

在路徑清單的後面的是表達式 

-path "test" -prune -o -print 是 -path "test" -a -prune -o -print 的簡寫表達式按順序求值, -a 和 -o 都是短路求值,與 shell 的 && 和 || 類似如果 

-path "test" 為真,則求值 -prune , -prune 傳回真,與邏輯表達式為真;否則不求值 -prune,與邏輯表達式為假。如果 -path "test" -a -prune 為假,則求值 -print ,-print傳回真,或邏輯表達式為真;否則不求值 -print,或邏輯表達式為真。 

這個表達式組合特例可以用僞碼寫為:

if -path "test" then  

-prune  

else  

-print  

執行個體2:避開多個檔案夾:

指令:

find test \( -path test/test4 -o -path test/test3 \) -prune -o -print 

輸出:

[[email protected] soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -print

test

test/log2014.log

test/log2015.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

[root@localhost soft]#

說明:

圓括号表示表達式的結合。  \ 表示引用,即訓示 shell 不對後面的字元作特殊解釋,而留給 find 指令去解釋其意義。  

執行個體3:查找某一确定檔案,-name等選項加在-o 之後

指令:

find test \(-path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print

輸出:

[[email protected] soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print

test/log2014.log

test/log2015.log

test/log2013.log

test/log2012.log

[root@localhost soft]#

5.使用user和nouser選項:

按檔案屬主查找檔案:

執行個體1:在$HOME目錄中查找檔案屬主為peida的檔案 

指令:

find ~ -user peida -print  

執行個體2:在/etc目錄下查找檔案屬主為peida的檔案: 

指令:

find /etc -user peida -print  

說明:

執行個體3:為了查找屬主帳戶已經被删除的檔案,可以使用-nouser選項。在/home目錄下查找所有的這類檔案

指令:

find /home -nouser -print

說明:

這樣就能夠找到那些屬主在/etc/passwd檔案中沒有有效帳戶的檔案。在使用-nouser選項時,不必給出使用者名; find指令能夠為你完成相應的工作。

6.使用group和nogroup選項:

就像user和nouser選項一樣,針對檔案所屬于的使用者組, find指令也具有同樣的選項,為了在/apps目錄下查找屬于gem使用者組的檔案,可以用:  

find /apps -group gem -print  

要查找沒有有效所屬使用者組的所有檔案,可以使用nogroup選項。下面的find指令從檔案系統的根目錄處查找這樣的檔案:

find / -nogroup-print

7.按照更改時間或通路時間等查找檔案:

如果希望按照更改時間來查找檔案,可以使用mtime,atime或ctime選項。如果系統突然沒有可用空間了,很有可能某一個檔案的長度在此期間增長迅速,這時就可以用mtime選項來查找這樣的檔案。  

用減号-來限定更改時間在距今n日以内的檔案,而用加号+來限定更改時間在距今n日以前的檔案。  

希望在系統根目錄下查找更改時間在5日以内的檔案,可以用:

find / -mtime -5 -print

為了在/var/adm目錄下查找更改時間在3日以前的檔案,可以用:

find /var/adm -mtime +3 -print

8.查找比某個檔案新或舊的檔案:

如果希望查找更改時間比某個檔案新但比另一個檔案舊的所有檔案,可以使用-newer選項。

它的一般形式為:  

newest_file_name ! oldest_file_name  

其中,!是邏輯非符号。  

執行個體1:查找更改時間比檔案log2012.log新但比檔案log2017.log舊的檔案

指令:

find -newer log2012.log ! -newer log2017.log

輸出:

[[email protected] test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

-rw-r--r-- 1 root root      0 11-16 14:41 log2016.log

-rw-r--r-- 1 root root      0 11-16 14:43 log2017.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:08 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]# find -newer log2012.log ! -newer log2017.log

.

./log2015.log

./log2017.log

./log2016.log

./test3

[root@localhost test]#

執行個體2:查找更改時間在比log2012.log檔案新的檔案  

指令:

find . -newer log2012.log -print

輸出:

[[email protected] test]# find -newer log2012.log

.

./log2015.log

./log2017.log

./log2016.log

./test3

[root@localhost test]#

9.使用type選項:

執行個體1:在/etc目錄下查找所有的目錄  

指令:

find /etc -type d -print  

執行個體2:在目前目錄下查找除目錄以外的所有類型的檔案  

指令:

find . ! -type d -print  

執行個體3:在/etc目錄下查找所有的符号連結檔案

指令:

find /etc -type l -print

10.使用size選項:

可以按照檔案長度來查找檔案,這裡所指的檔案長度既可以用塊(block)來計量,也可以用位元組來計量。以位元組計量檔案長度的表達形式為N c;以塊計量檔案長度隻用數字表示即可。  

在按照檔案長度查找檔案時,一般使用這種以位元組表示的檔案長度,在檢視檔案系統的大小,因為這時使用塊來計量更容易轉換。  

執行個體1:在目前目錄下查找檔案長度大于1 M位元組的檔案  

指令:

find . -size +1000000c -print

執行個體2:在/home/apache目錄下查找檔案長度恰好為100位元組的檔案:  

指令:

find /home/apache -size 100c -print  

執行個體3:在目前目錄下查找長度超過10塊的檔案(一塊等于512位元組) 

指令:

find . -size +10 -print

11.使用depth選項:

在使用find指令時,可能希望先比對所有的檔案,再在子目錄中查找。使用depth選項就可以使find指令這樣做。這樣做的一個原因就是,當在使用find指令向錄音帶上備份檔案系統時,希望首先備份所有的檔案,其次再備份子目錄中的檔案。  

執行個體1:find指令從檔案系統的根目錄開始,查找一個名為CON.FILE的檔案。   

指令:

find / -name "CON.FILE" -depth -print

說明:

它将首先比對所有的檔案然後再進入子目錄中查找

12.使用mount選項: 

  在目前的檔案系統中查找檔案(不進入其他檔案系統),可以使用find指令的mount選項。

執行個體1:從目前目錄開始查找位于本檔案系統中檔案名以XC結尾的檔案  

指令:

find . -name "*.XC" -mount -print 

繼續閱讀