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
[[email protected] 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
[[email protected] 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
[[email protected] test]#
說明:
查找目前目錄大于1K的檔案
find指令之exec
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
[[email protected] 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
[[email protected] 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
[[email protected] 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
[[email protected] test]# find . -name "*.log" -mtime +5 -ok rm {} ;
< rm ... ./log_link.log > ? y
< rm ... ./log2012.log > ? n
[[email protected] 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
[[email protected] 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
[[email protected] 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
[[email protected] 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
[[email protected] test3]# find . -name "*.log" -exec mv {} .. ;
[[email protected] test3]# ll
總計 0[[email protected] 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
[[email protected] test]#
執行個體6:用exec選項執行cp指令
指令:
find . -name "*.log" -exec cp {} test3 ;
輸出:
[[email protected] test3]# ll
總計 0[[email protected] 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
[[email protected] 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” 為同一檔案
[[email protected] 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
[[email protected] test3]#
find指令之xargs
在使用 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
[[email protected] test]# find . -type f -print | xargs file
./log2014.log: empty
./log2013.log: empty
./log2012.log: ASCII text
[[email protected] 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
[[email protected] 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
[[email protected] 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[[email protected] test]#
執行個體5:用grep指令在目前目錄下的所有普通檔案中搜尋hostnames這個詞
指令:
find . -name * -type f -print | xargs grep "hostnames"
輸出:
[[email protected] test]# find . -name * -type f -print | xargs grep "hostnames"
./log2013.log:hostnamesina=sina.com
./log2013.log:hostnames=true[[email protected] 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
[[email protected] test]# cd test4/
[[email protected] test4]# ll
總計 0[[email protected] 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
[[email protected] 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
[[email protected] test4]#
執行個體7:find後執行xargs提示xargs: argument line too long解決方法:
指令:
find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f
輸出:
[[email protected] test4]# find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f
rm -f
[[email protected]]#
說明:
-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
[[email protected] 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
[[email protected] 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
[[email protected] 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
[[email protected] 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
[[email protected] test]# cd test3
[[email protected] test3]# find . -name "*.log" | xargs -p -i mv {} ..
mv ./log2015.log .. ?...y
[[email protected] test3]# ll
總計 0[[email protected] 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
[[email protected] test]#
說明:
-p參數會提示讓你确認是否執行後面的指令,y執行,n不執行。
find 指令的參數詳解
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
執行個體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