http://blog.chinaunix.net/uid-7340476-id-225283.html
find命令主要用來在硬盤上搜索文件, find命令主要用於文件查找,列出當前目錄及子目錄下所有的文件和文件夾
格式:find path -option "keyword" [-print] [-exec -ok command] {} \;
path: 查找路徑 ;該命令用於在指定路徑中查找符合條件的文件,搜索路徑可以是多個目錄,不同目錄之間用空格分割
-option: 選項
"keyword": 關鍵字
command: 需要執行的命令
-exec command] {} \;將查到的文件執行command操作,注意{}和\;之間有空格
-ok和-exec的作用一樣,只是-ok在執行前會詢問用戶是否執行操作
find . -name "*" | xargs grep -i "passwd"(自己工作中用的最多的實例,-i的作用是不區分大小寫)
find -name "t*" -perm 744 //查找當前目錄下文件名以t開頭的,且文件屬性主具有讀、寫、執行權限的文件。。。。
find還有-exec選項,對匹配文件執行該參數過給出的shell命令。
例如:find /etc/ -type f -name "rc*" -exec ls -l {} \; //注意{}和\之間有空格。。。
由此可見:可以接多個選項參數
目錄路徑:表示以此目錄作為根目錄逐級往下搜索
1.目錄介紹:
如果find不指定目錄,則默認從當前所在的目錄開始搜索
$find
$find .
以上兩個結果一樣
. 一個點表示當前目錄,也可以使用 ./ 來表示;
.. 兩個點表示父目錄,也可以 ../ 來代表。
如果需要從根目錄開始查找:/
find . 遍歷輸出當前目錄下的所有文件(夾)及子文件(夾)
find / 遍歷輸出根目錄下的所有文件(夾)及子文件(夾)
find ./ 遍歷輸出當前目錄的下一級路徑的所有文件(夾)及子文件(夾)
也可以是/opt/qmfsun之類的路徑
find中的目錄可以指定多個搜索目錄
$ind /usr /home /tmp -name "*.java";在/usr /home /tmp三個目錄中查找以.java結尾的文件
如果對某個目錄沒有訪問權限的話,就會報錯,提示: find: /tmp/qmfsun:Permission denide
2. 需要搜索的關鍵字
3.主要選項參數:
注意:
a)每一個選項前面跟隨一個橫杠-
$find /doc -name '*bak' -exec rm -rf {} \; //從 /doc 目錄開始往下找,找到凡是文件名結尾為 bak的文件,把它刪除掉。
注意:-exec 選項是執行的意思,rm -rf是刪除命令,{ } 表示文件名,“\;”是規定的命令結尾。
find命令默認情況下是區分大小寫的,可以通過-iname或者在grep中添加-i參數來忽略大小寫
-name :指定按照文件名查找文件,查找時文件名大小寫敏感。只能搜索到文件名,如果需要搜索文件內容里包含的特定字符串,需要用grep(用的最常見)
-iname: 查找時不區分文件名大小寫
$ find . -iname U*
users users2
#如果執行find . -name U*將不會找到匹配的文件
find -iname "MyCProgram.c";所有不區分大小寫的文件名為“MyCProgram.c”的文件
查找當前用戶主目錄下的所有文件:下面兩種方法都可以使用
$ find . -name "*.log";從當前目錄中查找擴展名為.log的文件。需要說明的是,缺省情況下,find會從指定的目錄搜索,並遞歸的搜索其子目錄
./install.log
grep差多個字符串
ps -e|grep -E ‘grant_server|commsvr|tcpsvr|dainfo’ 查找多個字符串的匹配(grep -E相當於egrep)
使用grep匹配“與”或者“或”模式
grep命令加- E參數,這一擴展允許使用擴展模式匹配。例如,要抽取城市代碼為2 1 9或2 1 6,方法如下:
CODE:
[sam@chenwy sam]$ grep -E '219|216' data.f
219 dec 2CC1999 CAD 23.00 PLV2C 68
216 sept 3ZL1998 USP 86.00 KVM9E 234
find -perm,根據文件的權限來查找文件,有三種形式,是位"與", + /是位"或"
find -perm mode
find -perm -mode
find -perm /mode(+符號的作用與 / 符號相同,但是現在新版 GNU findutils 中不支持使用該符號)
三者區別:
-perm -mode :查找的檔案屬性『必須要全部囊括 mode 的屬性』的檔案,舉例來說,
我們要查找 -rwxr--r-- ,亦即744 的檔案,使用 -perm -744,
當一個檔案的屬性為 -rwxr-xr-x ,亦即 755時,也會被列出來,
因為 -rwxr-xr-x 的屬性已經囊括了 -rwxr--r-- 的屬性了。
-perm +mode :查找檔案屬性『包含任一 mode 的屬性』的檔案,舉例來說,我們查找
-rwxr-xr-x ,亦即 -perm +755 時,但一個檔案屬性為 -rw-------
也會被列出來,因為他有 -rw.... 的屬性存在
總用量 0
-rwxrwxrwx 1 root root 0 6月 9 20:30 1.txt 777
-rwxrwxr-x 1 root root 0 6月 9 18:58 2.txt 775
-rwxr-xrwx 1 root root 0 6月 10 00:10 3.txt 757
-r-xrwxrwx 1 root root 0 6月 10 00:10 4.txt 577
-r-xr-xrwx 1 root root 0 6月 10 00:10 5.txt 557
-r-xr-xr-x 1 root root 0 6月 10 00:10 6.txt 555
[root@test test1]# find . -perm 700 //沒有這個權限的文件
[root@test test1]# find . -perm -700
.
./3.txt
./1.txt
./2.txt
[root@test test1]# find . -perm -712 //2.txt主要是-rwxrwxr-x中的其他用戶沒有w權限
./3.txt
./1.txt
[root@test test1]# find -perm 755 //也沒有這個文件
find -perm +222 //找u或者g或者o三個中,至少一個有可寫權限的
find -perm -222 //找u,g和o三個中都有可寫的權限文件
find -perm -002 //找權限至少o可以寫的
find -perm -222 u g o 全都可寫才匹配
-perm按文件權限查找。例如:-perm -777, -perm –a+x(user, group, other 都具有write屬性)
find plsql -type f -perm -ug=rw -exec ls -l {} \; 2>/dev/null //將查找可由“other”和組寫入的文件
或者
find plsql -type f -perm -220 -exec ls -l {} \; 2>/dev/null
-rw-rw-rw- 1 bluher users 4303 Jun 7 2004 plsql/FORALLSample/doc/otn_new.css
-rw-rw-rw- 1 bluher users 10286 Jan 12 2005 plsql/FORALLSample/doc/readme.html
-rw-rw-rw- 1 bluher users 22647 Jan 12 2005
find plsql -type f -perm /ug=rw -exec ls -l {} \; 2>/dev/null //查找由用戶、組或二者共同寫入的文件:
或者
find plsql -type f -perm /220 -exec ls -l {} \; 2>/dev/null
-rw-r--r-- 1 bluher users 21473 May 3 16:02 plsql/regexpvalidate.zip
-rw-rw-rw- 1 bluher users 4303 Jun 7 2004 plsql/FORALLSample/doc/otn_new.css
-rw-rw-rw- 1 bluher users 10286 Jan 12 2005 plsql/FORALLSample/doc/readme.html
-rw-rw-rw- 1 bluher users 22647 Jan 12 2005 plsql/FORALLSample/src/config.sql
find /etc -perm 640 精確匹配,其權限必須是640
find /etc -perm /640三組權限中有任意一組匹配都行
find /etc -perm -640含有該權限的都得匹配
-perm -222 可查找出666,只要含有222權限的都可以
-perm -400只要屬主有讀權限即可,其他任意權限
-perm /400屬主有讀權限,其他沒有任何權限;符合這三組都可
find . -perm 700 是說恰好為 700, rwx------
find . -perm -700 是說第一組滿足 7 就可以了,后兩組無所謂,因此 rwx------ 和 rwxrwxrwx 都會入選
find . -perm +700 是說第一組每一位有一個滿足就可以了,因此 r-x------ 也會入選,范圍又擴大很多
# find . -perm 111 -print //表示在當前目錄下搜尋所有者、同組成員及其他成員均為可執行權限的文件及文件夾
# find . -perm -111 -print //表示在當前目錄下搜尋所有者、同組成員及其他成員均含有可執行權限的文件及文件夾
#find . -perm /111 -print //表示在當前目錄下搜尋所有者、同組成員及其他成員中至少一個角色含有可執行權限的文件及文件夾
+ 針對 三個權限位中的任意 一位
- 針對 三個權限位中的全部 三位
MODE 也是針對三位的
[root@localhost ~]# ls -lh test
total 0
-r--r--r-- 1 root root 0 Oct 10 20:50 test1
-rwxr--r-- 1 root root 0 Oct 10 20:50 test2
-rw-r--r-- 1 root root 0 Oct 10 20:50 test3
-rw-r--r-- 1 root root 0 Oct 10 20:50 test4
[root@localhost ~]# find test -perm 644 | xargs ls -hld
-rw-r--r-- 1 root root 0 Oct 10 20:50 test/test3
-rw-r--r-- 1 root root 0 Oct 10 20:50 test/test4
[root@localhost ~]# find test -perm +644 |xargs ls -lhd
drwxr-xr-x 2 root root 1.0K Oct 10 20:50 test //找到這個,是因為find中沒有指明查找的類型,即沒有-type f的原因
-r--r--r-- 1 root root 0 Oct 10 20:50 test/test1
-rwxr--r-- 1 root root 0 Oct 10 20:50 test/test2
-rw-r--r-- 1 root root 0 Oct 10 20:50 test/test3
-rw-r--r-- 1 root root 0 Oct 10 20:50 test/test4
[root@localhost ~]# find test -perm -644 | xargs ls -lhd
drwxr-xr-x 2 root root 1.0K Oct 10 20:50 test
-rwxr--r-- 1 root root 0 Oct 10 20:50 test/test2
-rw-r--r-- 1 root root 0 Oct 10 20:50 test/test3
-rw-r--r-- 1 root root 0 Oct 10 20:50 test/test4
可以看到 a ab abc 的權限分別為600 640 666
find -perm 640 是做精確匹配,只會匹配到640即 ab
find -perm -640 做比640更充足的匹配,當然666是滿足的,即 ab abc
find -perm /640 是要任意的一組權限中1的位置上有一個符合即可,因此a ab abc 都會匹配出來

如果在查找文件時希望忽略某個目錄,因為你知道那個目錄中沒有你所要查找的文件,那么可以使用-prune選項來指出需要忽略的目錄。在使用-prune選項時要當心,因為如果你同時使用了-depth選項,那么-prune選項就會被find命令忽略。
find /apps -path "/apps/bin" -prune -o -print;希望在/apps目錄下查找文件,但不希望在/apps/bin目錄下查找
find /usr/sam -path "/usr/sam/dir1" -prune -o -print;比如要在/usr/sam目錄下查找不在dir1子目錄之內的所有文件
find命令和and(多個參數選項連接符),or搭配使用
b)這些選項可以多個一起用
$find -name "t*" -perm 744;查找當前目錄下文件名以t開頭的,且文件屬性主具有讀、寫、執行權限的文件。。。。
$find /etc/ -type f -name "rc*" -exec ls -l {} \; (find還有-exec選項,對匹配文件執行該參數過給出的shell命令。)
$find /doc -user jacky -name 'j*' //從 /doc 目錄開始往下找,找屬主為jacky 的、文件名開頭是 j的文件。
多條件查找:條件間的邏輯關系
並關系:-a
或關系:-o
非關系:!或者-not
例如:find /tmp -name "passwd" -user root(默認並關系)
-a
-o
!
避開多個文件夾
\ 表示轉義字符,即指示 shell 不對后面的字符作特殊解釋,轉義字符。
查找某一確定文件,-name等選項加在-o 之后
在linux find 進行查找的時候,有時候需要忽略某些目錄不查找,可以使用 -prune 參數來進行過濾,但必須要注意要忽略的路徑參數必須緊跟着搜索的路徑之后,否則該參數無法起作用。
以下是指定搜索/home/carryf目錄下的所有文件,但是會忽略/home/carryf/astetc的路徑:
find /home/carryf -path "/home/carryf/astetc" -prune -o -type f -print
如果按照文件名來搜索則為:
find /home/carryf -path "/home/carryf/astetc" -prune -o -type f -name "cdr_*.conf" -print
如果要忽略兩個以上的路徑如何處理?
find /home/carryf /( -path "/home/carryf/astetc" -o -path "/home/carryf/etc" /) -prune -o -type f -print
find /home/carryf /( -path "/home/carryf/astetc" -o -path "/home/carryf/etc" /) -prune -o -type f -name "cdr_*.conf" -print
注意/( 和/) 前后都有空格。
查找某個文件包含內容,下面這個語句可以解決目錄帶空格的問題:
find ./ -name "mysql*" -print0 |xargs -0 grep "SELECT lead_id FROM vicidial_list where vendor_lead_code"
如果目錄不帶空格,那么可以如下面的形式執行:
find ./ -name "mysql*" |xargs grep "SELECT lead_id FROM vicidial_list where vendor_lead_code"
-amin n 查找系統中最后N分鍾訪問的文件
-atime n 查找系統中最后n*24小時訪問的文件
-cmin n 查找系統中最后N分鍾被改變文件狀態的文件
-ctime n 查找系統中最后n*24小時被改變文件狀態的文件
-mmin n 查找系統中最后N分鍾被改變文件數據的文件
-mtime n 查找系統中最后n*24小時被改變文件數據的文件
解釋什么是atime ctime mtime
atime (access time):最后一次訪問文件的時間
mtime(medify time):最后一次修改文件的時間
ctime(change time):最后一次改變文件(改變的是原數據即屬性)的時間
如:記錄該文件的inode節點被修改的時間。touch 命令除了-d -t選項外都會改變改時間,而且chmod,chown等命令也能改變該值
三者之間的關系
當修改mtime時,ctime必須隨着改變,因為文件大小等屬性;有人說atime 也一定會改變,要想修改文件必須先訪問;其實是不對的,不必訪問文件就能修改內容:如#echo "change it" >> /etc/inittab ,inittab文件內容會改變,但並沒有訪問文件,所以atime沒有改變
查看三者的命令
stat filename 可以查看三者的時間值
ls -l filename 查看文件修改時間
ls -lc filename 查看文件狀態改動時間
ls -lu filename 查看文件訪問時間

-empty: 查找空文件。
-size +2M大於2M的文件
-size -1k小於1k的
-size 2M介於2M正負1M范圍內的文件
$ find
/ -
type
f -name *.zip -size +100M -
exec
rm
-i {} \;(刪除大於100M的*.zip文件)
-exec 操作允許 find 在它遇到的文件上執行任何 shell 命令。大括號允許移動每個空文件。
- -print: find命令將匹配的文件輸出到標准輸出。
- -exec: find命令對匹配的文件執行該參數所給出的shell命令。相應命令的形式為"command { } \; ",注意"{ }"和“\;”之間的空格。
- -ok: 和-exec的作用相同,只不過以一種更為安全的模式來執行該參數所給出的shell命令,在執行每一個命令之前,都會給出提示,讓用戶來確定是否執行。
作用:用戶使用這一選項是為了查找到舊文件並查看,刪除它或者做其他的操作
exec和ok:
選項后面跟隨着所要執行的命令或腳本,然后是一對兒{ },一個空格和一個\,最后是一個分號。如果驗證一下find命令,會發現該命令只輸出從當前路徑起的相對路徑及文件名。任何形式的命令都可以在-exec選項中使用。
eg:
-prune剔除某個文件或者文件夾
$find . –path “/DSF” –prune –o –print
$find . \( -path “./DSF” –o “./file1” \) –prune –o –perm -444 -print
注意:-o 是或者,以此類推也可以用-a,-a是並且的意思。可根據相應的情況用-o 或者-a
find命令把匹配到的文件傳遞給xargs命令,而xargs命令每次只獲取一部分文件而不是全部,不像-exec選項那樣。這樣它可以先處理最先獲取的一部分文件,然后是下一批,並如此繼續下去。
在有些系統中,使用-exec選項會為處理每一個匹配到的文件而發起一個相應的進程,並非將匹配到的文件全部作為參數一次執行;這樣在有些情況下就會出現進程過多,系統性能下降的問題,因而效率不高;
而使用xargs命令則只有一個進程。另外,在使用xargs命令時,究竟是一次獲取所有的參數,還是分批取得參數,以及每一次獲取參數的數目都會根據該命令的選項及系統內核中相應的可調參數來確定。
來看看xargs命令是如何同find命令一起使用的,並給出一些例子。
下面的例子查找系統中的每一個普通文件,然后使用xargs命令來測試它們分別屬於哪類文件
#find . -name "file*" -print | xargs echo "" > /temp/core.log
# cat /temp/core.log
./file6
# ls -l
drwxrwxrwx 2 sam adm 4096 10月 30 20:14 file6
-rwxrwxrwx 2 sam adm 0 10月 31 01:01 http3.conf
-rwxrwxrwx 2 sam adm 0 10月 31 01:01 httpd.conf
# find . -perm 777 -print | xargs chmod o-w
# ls -l
drwxrwxr-x 2 sam adm 4096 10月 30 20:14 file6
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 http3.conf
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 httpd.conf
$ find . -type f | xargs grep "hostname"
./httpd1.conf:# different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your
用grep命令在當前目錄下的所有普通文件中搜索hostnames這個詞:
# find . -name "*" | xargs grep "hostnames"
./httpd1.conf:# different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
find命令配合使用exec和xargs可以使用戶對所匹配到的文件執行幾乎所有的命令。
實例1:ls -l命令放在find命令的-exec選項中
命令:
find . -type f -exec ls -l {} \;
輸出:
[root@localhost 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 {} \;
輸出:
[root@localhost 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 {} \;
[root@localhost 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 {} \;
輸出:
[root@localhost 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" {} \;
輸出:
[root@localhost 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 {} .. \;
輸出:
[root@localhost 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/
[root@localhost 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 {} .. \;
[root@localhost test3]# ll
總計 0[root@localhost test3]# cd ..
[root@localhost 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 \;
輸出:
[root@localhost test3]# ll
總計 0[root@localhost test3]# cd ..
[root@localhost 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
[root@localhost 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]#
實例1: 查找系統中的每一個普通文件,然后使用xargs命令來測試它們分別屬於哪類文件
命令:
find . -type f -print | xargs file
輸出:
[root@localhost 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
輸出:
[root@localhost test]# find / -name "core" -print | xargs echo "" >/tmp/core.log
[root@localhost test]# cd /tmp
[root@localhost 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
輸出:
[root@localhost 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
[root@localhost 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"
輸出:
[root@localhost 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"
輸出:
[root@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
輸出:
[root@localhost 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/
[root@localhost test4]# ll
總計 0[root@localhost test4]# cd ..
[root@localhost test]# find . -name "*.log" | xargs -i mv {} test4
[root@localhost 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/
[root@localhost 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
輸出:
[root@pd test4]# find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f
rm -f
[root@pdtest4]#
說明:
-l1是一次處理一個;-t是處理之前打印出命令
實例8:使用-i參數默認的前面輸出用{}代替,-I參數可以指定其他代替字符,如例子中的[]
命令:
輸出:
[root@localhost 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
[root@localhost test4]# find . -name "file" | xargs -I [] cp [] ..
[root@localhost 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 ..
[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
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 {} ..
輸出:
[root@localhost test3]# ll
總計 0
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
[root@localhost test3]# cd ..
[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
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
[root@localhost test3]# find . -name "*.log" | xargs -p -i mv {} ..
mv ./log2015.log .. ?...y
[root@localhost test3]# ll
總計 0[root@localhost test3]# cd ..
[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命令查找實例:
查找2004-11-30 16:36:37時更改過的文件
# A=`find ./ -name "*php"` | ls -l --full-time $A 2>/dev/null | grep "2004-11-30 16:36:37"
將find出來的東西拷到另一個地方
find *.c -exec cp '{}' /tmp ;
比如要查找磁盤中大於3M的文件:
find . -size +3000k -exec ls -ld {} ;
在/tmp中查找所有的*.h,並在這些文件中查找“SYSCALL_VECTOR",最后打印出所有包含"SYSCALL_VECTOR"的文件名
A) find /tmp -name "*.h" | xargs -n50 grep SYSCALL_VECTOR
B) grep SYSCALL_VECTOR /tmp/*.h | cut -d':' -f1| uniq > filename
C) find /tmp -name "*.h" -exec grep "SYSCALL_VECTOR" {} \; -print
find /tmp -name tmp.txt -exec cat {} \;
$find . -name "yao*" | xargs file
$find . -name "yao*" | xargs echo "" > /tmp/core.log
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這個用戶的文件
$find /etc -name "passwd*" -exec grep "cnscn" {} \; #看是否存在cnscn用戶
Linux下find一次查找多個指定類型文件,指定文件或者排除某類文件,在 GREP 中匹配多個關鍵
|
(1)Linux下find一次查找多個指定文件:
查找a.html和b.html
- find . -name "a.html" -name "b.html"
find . -regex '.*\.txt\|.*\.doc\|.*\.mp3'
- find . -regex '.*\.txt\|.*\.doc\|.*\.mp3'
- ./a.txt
- ./a.doc
- ./a.mp3
(2)排除某些文件類型:
排除目錄下所有以html結尾的文件:
- find . -type f ! -name "*.html"
- find . -type f ! -name "*.html"
- ./ge.bak.02.09
- ./ge.html.changed.by.jack
- ./a.txt
- ./a.doc
- ./a.mp3
(3)排除多種文件類型的示例:
- find . -type f ! -name "*.html" -type f ! -name "*.php" -type f ! -name "*.svn-base" -type f ! -name "*.js" -type f ! -name "*.gif" -type f ! -name "*.png" -type f ! -name "*.cpp" -type f ! -name "*.h" -type f ! -name "*.o" -type f ! -name "*.jpg" -type f ! -name "*.so" -type f ! -name "*.bak" -type f ! -name "*.log"
(3)在 GREP 中匹配多個關鍵字的方法:
grep查找多個數字的文件:
-r 遞歸,-E:正則 -l:只顯示文件名
- root@116.255.139.240:~/a# grep -r -E '0341028|100081|10086|10001' *
- a.txt:100081
- b.txt:10086
- c/cc.txt:0341028
- c/cc.txt:100081
- c/cc.txt:10086
- c/cc.txt:10001
- c.txt:10001
- d.txt:0341028
- grep -r -E -l '0341028|100081|10086|10001' *
- a.txt
- b.txt
- c/cc.txt
- c.txt
- d.txt
多種類型文件示例:
- find . -name "*.html" -o -name "*.js"|xargs grep -r "BusiTree"
用Awk:
- find . -name "*.php"|awk '{print "cat " $0 " |grep -H dbsys.mxxxx.justwinit.cn"}'|sh
find命令的復雜查找
#find . -size -10c –print | ls –l //在當前目錄下查找100~200塊長的文件並顯示文件的實際塊數。
$find ./ -perm -002 -exec mv {} {}.old \; //將查找到文件的名字加上.old(相當於重命名)
1)在/tmp中查找所有的*.h,並在這些文件中查找“SYSCALL_VECTOR",最后打印出所有包含"SYSCALL_VECTOR"的文件名
A)find /tmp -name "*.h" | xargs n50 grep SYSCALL_VECTOR
B) grep SYSCALL_VECTOR /tmp/*.h | cut -d':' -f1| uniq > filename
C) find /tmp -name "*.h" -exec grep "SYSCALL_VECTOR" {} \;
# A='find ./ -name "*php"' | ls -l --full-time $A 2>/dev/null | grep "2004-11-30 16:36:37 //查找2004-11-30 16:36:37時更改過的文件
$find / -name access_log 2 >/dev/null //無錯誤查找,所有的錯誤信息都被輸入到/dev/null 目錄
$ find . –print
-print指明打印出匹配文件的文件名(路徑),’\n’作為分割符。使用-print0可指明使用’\0’作為分割符。
我們可根據文件名進行搜索,如搜索以.txt結尾的文件名:
$ find . –name “*.txt” –print
另外還有一個-iname選項,與-name選項的區別僅在於-iname選項忽略字母大小寫。
如果想匹配多個條件中的一個,可以采用OR條件操作:
$ find . \( –name “*.txt” –o –name “*.pdf” \) –print
./new.txt
./new.pdf
上面的代碼打印出所有的.txt和.pdf文件。\(以及\)用於將它們之中的內容視為一個整體。
選項-path(同樣也有-ipath)將文件路徑作為一個整體進行匹配,如:
$ find . –path “*new*” –print
./new.txt
./new.pdf
./new.py
選項-regex的參數和-path的類似,只不過-regex(同樣也有-iregex)是基於正則表達式來匹配文件路徑的。
$ find . –regex “.*\(\.txt\|\.pdf\)$” –print
./new.txt
./new.pdf
find也可以用”!”否定參數的含義,如:
$ find . ! –name “*.txt” –print
.
./new.pdf
./new.py
find命令在使用時會遍歷所有的子目錄。但可以使用-maxdepth和-mindepth來限制find命令遍歷的深度。如:
$ find . maxdepth 1 –print
$ find . mindepth 2 –print
-maxdepth和-mindepth應該作為find的第3個參數出現。如果作為第4個或之后的參數,就可能會影響到find的效率,因為它不得不進行一些不必要的檢查。
Linux中文件具有不同的類型,例如普通文件、目錄、字符設備、塊設備、符號鏈接、硬鏈接、套接字以及FIFO等。Find命令中的-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
Linux文件系統中的每一個文件都有三種時間戳:
l 訪問時間(-atime):用戶最近一次訪問文件的時間。
l 修改時間(-mtime):文件內容最后一次被修改的時間。
l 變化時間(-ctime):文件元數據(例如權限或所有權)最后一次改變的時間。
-atime、-mtime、-ctime可作為find的時間參數,它們可以整數值給出,單位是天。這些整數值還可以帶有-或+,如:
$ find . type f –atime -7 -print #打印最近7天被訪問過的所有文件
$ find . type f –atime 7 –print #打印恰好在7天前被訪問過的所有文件
$ find . type f –atime +7 –print #打印出訪問時間超過7天的所有文件
另外,還有以分鍾作為計量單位的,包括:
l -amin(訪問時間)
l -mmin(修改時間)
l -cmin(變化時間)
find命令還有一個-newer參數,使用-newer,可以指定一個用於比較時間戳的參考文件,如:
$ find . –type f –newer new.txt –print #打印比file.txt修改時間更新的所有文件
find還可以根據文件大小搜索:
$ find . –type f –size +2k #大於2KB的文件
$ find . –type f –size -2k #小於2KB的文件
$ find . –type f –size 2k #大小等於2KB的文件
除了k之外,還有:
l b——塊(512字節)。
l C——字節。
l w——字(2字節)。
l k——千字節。
文件匹配還可以根據文件權限進行,如:
$ find . –type f –perm 644 –print #打印出權限為644的文件
還可以用該方法找出那些沒有設置好權限執行權限的PHP文件:
$ find . type f –name “*.php” ! –perm 644 –print
也可以根據文件的所有權進行搜索,如:
$ find . –type f –user baojie –print #打印用戶baojie擁有的所有文件
find命令可以借助選項-exec與其他命令進行結合,如將所有.py添加可執行權限:
$ find . –name “*.py” –exec chmod +x {} \;
{}是一個特殊的字符串,與-exec選項結合使用。對於每一個匹配的文件,{}會被替換成響應的文件名。
無法在-exec參數中直接使用多個命令,但可以把多個命令寫到一個shell腳本中,然后再-exec中使用這個腳本。
-exec能夠同printf結合起來生成有用的輸出信息。例如:
$ find . –name “*.txt” –exec printf “Text file: %s\n” {} \;
find命令在執行搜索時還可以跳過一些子目錄,如下示例:
$ find . \( -name “old” –prune \) –o \( -type f –print \)
以上命令打印出不包括在old目錄中的所有文件的名稱。
使用find命令在linux系統中查找文件時,有時需要忽略某些目錄,可以使用 -prune 參數來進行過濾。 不過必須注意:要忽略的路徑參數要緊跟着搜索的路徑之后,否則該參數無法起作用。
例如:指定搜索/home/zth目錄下的所有文件,但是會忽略/home/zth/astetc的路徑:
按照文件名來搜索則為:
要忽略兩個以上的路徑如何處理?
注意:/( 和/) 前后都有空格。
查找某個文件包含內容,以下語句可以解決目錄帶空格的問題:
如果目錄不帶空格,可以這樣:
通過以上的例子,大家應該可以掌握find命令查找文件時,忽略相關目錄的方法了。
如果想同時刪除A和B文件則可以用-o 連接條件
find -name "*" -o -name "A" -o -name "B" -newer A ! -newer B -exec rm -f {} \;