題外話:find命令之所以難學,是因爲它不僅僅有選項、測試和動作類型的參數,還因爲其中一個參數的處理結果可能會影響到後續參數的處理。
find命令的完整語法格式如下:
find [path] [options] [tests] [actions]
-amin<分鐘>:查找在指定時間曾被存取過的文件或目錄,單位以分鐘計算; -anewer<參考文件或目錄>:查找其存取時間較指定文件或目錄的存取時間更接近現在的文件或目錄; -atime<24小時數>:查找在指定時間曾被存取過的文件或目錄,單位以24小時計算; -cmin<分鐘>:查找在指定時間之時被更改過的文件或目錄; -cnewer<參考文件或目錄>查找其更改時間較指定文件或目錄的更改時間更接近現在的文件或目錄; -ctime<24小時數>:查找在指定時間之時被更改的文件或目錄,單位以24小時計算; -daystart:從本日開始計算時間; -depth:從指定目錄下最深層的子目錄開始查找; -expty:尋找文件大小爲0 Byte的文件,或目錄下沒有任何子目錄或文件的空目錄; -exec<執行指令>:假設find指令的回傳值爲True,就執行該指令; -false:將find指令的回傳值皆設爲False; -fls<列表文件>:此參數的效果和指定“-ls”參數類似,但會把結果保存爲指定的列表文件; -follow:排除符號連接; -fprint<列表文件>:此參數的效果和指定“-print”參數類似,但會把結果保存成指定的列表文件; -fprint0<列表文件>:此參數的效果和指定“-print0”參數類似,但會把結果保存成指定的列表文件; -fprintf<列表文件><輸出格式>:此參數的效果和指定“-printf”參數類似,但會把結果保存成指定的列表文件; -fstype<文件系統類型>:只尋找該文件系統類型下的文件或目錄; -gid<羣組識別碼>:查找符合指定之羣組識別碼的文件或目錄; -group<羣組名稱>:查找符合指定之羣組名稱的文件或目錄; -help或——help:在線幫助; -ilname<範本樣式>:此參數的效果和指定“-lname”參數類似,但忽略字符大小寫的差別; -iname<範本樣式>:此參數的效果和指定“-name”參數類似,但忽略字符大小寫的差別; -inum:查找符合指定的inode編號的文件或目錄; -ipath<範本樣式>:此參數的效果和指定“-path”參數類似,但忽略字符大小寫的差別; -iregex<範本樣式>:此參數的效果和指定“-regexe”參數類似,但忽略字符大小寫的差別; -links<連接數目>:查找符合指定的硬連接數目的文件或目錄; -iname<範本樣式>:指定字符串作爲尋找符號連接的範本樣式; -ls:假設find指令的回傳值爲Ture,就將文件或目錄名稱列出到標準輸出; - maxdepth<目錄層級>:設置最大目錄層級; -mindepth<目錄層級>:設置最小目錄層級; -mmin<分鐘>:查找在指定時間曾被更改過的文件或目錄,單位以分鐘計算; -mount:此參數的效果和指定“-xdev”相同; -mtime<24小時數>:查找在指定時間曾被更改過的文件或目錄,單位以24小時計算; -name<範本樣式>:指定字符串作爲尋找文件或目錄的範本樣式; -newer<參考文件或目錄>:查找其更改時間較指定文件或目錄的更改時間更接近現在的文件或目錄; -nogroup:找出不屬於本地主機羣組識別碼的文件或目錄; -noleaf:不去考慮目錄至少需擁有兩個硬連接存在; -nouser:找出不屬於本地主機用戶識別碼的文件或目錄; -ok<執行指令>:此參數的效果和指定“-exec”類似,但在執行指令之前會先詢問用戶,若回答“y”或“Y”,則放棄執行命令; -path<範本樣式>:指定字符串作爲尋找目錄的範本樣式; -perm<權限數值>:查找符合指定的權限數值的文件或目錄; -print:假設find指令的回傳值爲Ture,就將文件或目錄名稱列出到標準輸出。格式爲每列一個名稱,每個名稱前皆有“./”字符串; -print0:假設find指令的回傳值爲Ture,就將文件或目錄名稱列出到標準輸出。格式爲全部的名稱皆在同一行; -printf<輸出格式>:假設find指令的回傳值爲Ture,就將文件或目錄名稱列出到標準輸出。格式可以自行指定; -prune:不尋找字符串作爲尋找文件或目錄的範本樣式; -regex<範本樣式>:指定字符串作爲尋找文件或目錄的範本樣式; -size<文件大小>:查找符合指定的文件大小的文件; -true:將find指令的回傳值皆設爲True; - typ<文件類型>:只尋找符合指定的文件類型的文件; -uid<用戶識別碼>:查找符合指定的用戶識別碼的文件或目錄; -used<日數>:查找文件或目錄被更改之後在指定時間曾被存取過的文件或目錄,單位以日計算; -user<擁有者名稱>:查找符和指定的擁有者名稱的文件或目錄; -version或——version:顯示版本信息; -xdev:將範圍侷限在先行的文件系統中; -xtype<文件類型>:此參數的效果和指定“-type”參數類似,差別在於它針對符號連接檢查。
下面介紹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 輸出: [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選項來查找這樣的文件。
Linux文件系統中的每一個文件都有三種時間戳:
訪問時間(-atime):用戶最近一次訪問文件的時間。
修改時間(-mtime):文件內容最後一次被修改的時間。
變化時間(-ctime):文件元數據(例如權限或所有權)最後一次改變的時間。
-atime、-mtime、-ctime可作爲find的時間參數,它們可以整數值給出,單位是天。這些整數值還可以帶有-或+,用減號-來限定更改時間在距今n日以內的文件,而用加號+來限定更改時間在距今n日以前的文件。
$ find . type f –atime -7 -print #打印最近7天被訪問過的所有文件 $ find . type f –atime 7 –print #打印恰好在7天前被訪問過的所有文件 $ find . type f –atime +7 –print #打印出訪問時間超過7天的所有文件 $ find / -mtime -5 -print #系統根目錄下查找更改時間在5日以內的文件 $find /var/adm -mtime +3 -print #在/var/adm目錄下查找更改時間在3日以前的文件
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選項:
$ find . –type d –print #列出所有的目錄 $ find . –type f –print #列出普通文件 $ find . –type l –print #列出符號鏈接 $ find . –type c –print #列出字符設備 $ find . –type b –print #列出塊設備 $ find . –type s –print #列出套接字 $ find . –type p –print #列出FIFO
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
下面列出一些find常見用法實例:
find -name april* 在當前目錄下查找以april開始的文件 find -name april* fprint file 在當前目錄下查找以april開始的文件,並把結果輸出到file中 find -name ap* -o -name may* 查找以ap或may開頭的文件 find /mnt -name tom.txt -ftype vfat 在/mnt下查找名稱爲tom.txt且文件系統類型爲vfat的文件 find /mnt -name t.txt ! -ftype vfat 在/mnt下查找名稱爲tom.txt且文件系統類型不爲vfat的文件 find /tmp -name wa* -type l 在/tmp下查找名爲wa開頭且類型爲符號鏈接的文件 find /home -mtime -2 在/home下查最近兩天內改動過的文件 find /home -atime -1 查1天之內被存取過的文件 find /home -mmin +60 在/home下查60分鐘前改動過的文件 find /home -amin +30 查最近30分鐘前被存取過的文件 find /home -newer tmp.txt 在/home下查更新時間比tmp.txt近的文件或目錄 find /home -anewer tmp.txt 在/home下查存取時間比tmp.txt近的文件或目錄 find /home -used -2 列出文件或目錄被改動過之後,在2日內被存取過的文件或目錄 find /home -user cnscn 列出/home目錄內屬於用戶cnscn的文件或目錄 find /home -uid +501 列出/home目錄內用戶的識別碼大於501的文件或目錄 find /home -group cnscn 列出/home內組爲cnscn的文件或目錄 find /home -gid 501 列出/home內組id爲501的文件或目錄 find /home -nouser 列出/home內不屬於本地用戶的文件或目錄 find /home -nogroup 列出/home內不屬於本地組的文件或目錄 find /home -name tmp.txt -maxdepth 4 列出/home內的tmp.txt 查時深度最多爲3層 find /home -name tmp.txt -mindepth 3 從第2層開始查 find /home -empty 查找大小爲0的文件或空目錄 find /home -size +512k 查大於512k的文件 find /home -size -512k 查小於512k的文件 find /home -links +2 查硬連接數大於2的文件或目錄 find /home -perm 0700 查權限爲700的文件或目錄 find /tmp -name tmp.txt -exec cat {} \; find /tmp -name tmp.txt -ok rm {} \; find / -amin -10 # 查找在系統中最後10分鐘訪問的文件 find / -atime -2 # 查找在系統中最後48小時訪問的文件 find / -empty # 查找在系統中爲空的文件或者文件夾 find / -group cat # 查找在系統中屬於 groupcat的文件 find / -mmin -5 # 查找在系統中最後5分鐘裏修改過的文件 find / -mtime -1 # 查找在系統中最後24小時裏修改過的文件 find / -nouser # 查找在系統中屬於作廢用戶的文件 find / -user fred # 查找在系統中屬於FRED這個用戶的文件