linux 命令——22 find (轉)

來源:互聯網
上載者:User

標籤:

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

[[email protected] test]# find . -name "[a-z]*[4-9].log" -print

./log2014.log

./log2015.log

./test4/log2014.log

[[email protected] 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

[[email protected] 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

[[email protected] 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

[[email protected] 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

[[email protected] 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

[[email protected] 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

[[email protected] 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

[[email protected] 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

[[email protected] 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

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

.

./log2015.log

./log2017.log

./log2016.log

./test3

[[email protected] test]#

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

命令:

find . -newer log2012.log -print

輸出:

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

.

./log2015.log

./log2017.log

./log2016.log

./test3

[[email protected] 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

linux 命令——22 find (轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.