天天一個linux命令(22):find 命令的參數詳解

find一些經常使用參數的一些經常使用實例和一些具體用法和注意事項。shell

1.使用name選項:express

文件名選項是find命令最經常使用的選項,要麼單獨使用該選項,要麼和其餘選項一塊兒使用。  可使用某種文件名模式來匹配文件,記住要用引號將文件名模式引發來。  無論當前路徑是什麼,若是想要在本身的根目錄$HOME中查找文件名符合*.log的文件,使用~做爲 'pathname'參數,波浪號~表明了你的$HOME目錄。apache

find ~ -name "*.log" -print  app

想要在當前目錄及子目錄中查找全部的‘ *.log‘文件,能夠用: io

find . -name "*.log" -print  test

想要的當前目錄及子目錄中查找文件名以一個大寫字母開頭的文件,能夠用:  file

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

想要在/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

輸出:

[root@localhost 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的文件,即文件屬主能夠讀、寫、執行,其餘用戶能夠讀、執行的文件,能夠用:  

[root@localhost 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

輸出:

[root@localhost 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

輸出:

[root@localhost 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

輸出:

[root@localhost 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 

輸出:

[root@localhost 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

輸出:

[root@localhost 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

輸出:

[root@localhost 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

輸出:

[root@localhost 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 

相關文章
相關標籤/搜索