find命令


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需要搜索的關鍵字

關鍵字可以使用正則表達式來模糊匹配該文件名,記住要用""將文件名模式引起來,不用雙引號的話,需要\轉義
find . -name \*.txt
 

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 $HOME -print
$ find ~ -print
 
$ find . -name "*.log";從當前目錄中查找擴展名為.log的文件。需要說明的是,缺省情況下,find會從指定的目錄搜索,並遞歸的搜索其子目錄
  ./install.log
 
$find . -name "*.log"
$find . -name \*.log;在當前目錄查找 以.log結尾的文件。 ". "代表當前目錄,這兩個命令執行結果一樣
說明:
 
 
 
 
$find . -name "*";查找當前目錄下的所有文件
$find . -name "*qmf*";查找當前目錄下文件名中包含qmf的文件
$find ./ -name "[A-Z]*";想要當前目錄及子目錄中查找文件名以一個大寫字母開頭的文件
$find /etc -name "host*";想要在/etc目錄中查找文件名以host開頭的文件
$find ~ -name "*";想要查找$HOME目錄中的文件
$find . -name "[a-z][a-z][0--9][0--9].txt";如果想在當前目錄查找文件名以兩個小寫字母開頭,跟着是兩個數字,最后是.txt的文件,返回名為ax37.txt的文件
 
$find /etc -name inittab -o -size +2048000       --在etc目錄下查找名稱為inittab或者文件到校大於1000MB的文件
$find /etc -size +163840 -a -size -204800        --在etc目錄下查找大於80MB小於100MB的文件
$find /etc -name inittab -exec ls -l {} \;       --在etc目錄下查找inittab文件並顯示其詳細信息
$find /etc -name init* -a -type f -exec ls -l {} \;   --在etc目錄下查找以init開頭的、文件類型為二進制文件,查找到以后並查看詳細信息;

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

 

-perm :按照文件權限來查找文件。(用的很常見)
按照文件權限模式用-perm選項,按文件權限模式來查找文件的話。最好使用八進制數字權限表示法,但是也可以用符號表示
$find . -type f -perm a=rwx -exec ls -l {} \;
或者
$find . -type f -perm 777 -exec ls -l {} \;   //兩個效果一樣
$find . -type f -perm -ug=rw -exec ls -l {} \;  //查找可以由“other”和組寫入的文件
或者
$find . -type f -perm 220 -exec ls -l {} \;
$find . -perm 755;在當前目錄下查找文件權限為755的文件,即文件屬主可以讀、寫、執行,其他用戶可以讀、執行的文件
$find /opt/soft/test/ -perm 777;查找/opt/soft/test/目錄下 權限為 777的文件
 
/:只匹配一位權限即可
-:文件權限全包含時才顯示

 

find -perm,根據文件的權限來查找文件,有三種形式,是位"與", + /是位"或"

find -perm mode

find -perm -mode

find -perm /mode(+符號的作用與 / 符號相同,但是現在新版 GNU findutils 中不支持使用該符號)

三者區別:

在linux中文件或目錄有三者權限r,w,x,代表的含義分別是讀、寫、可執行。而一個文件或目錄的屬性中又包括所屬用戶u、所屬組g、其他o三個部分的屬性,分別表示所屬用戶、所屬組、其他用戶對這個文件所擁有的權限。 
 所屬用戶   所屬組    其他
   rwx       rwx      rwx
 -perm mode  :查找的檔案屬性『剛好等於』 mode的檔案 
 -perm -mode :查找的檔案屬性『必須要全部囊括 mode 的屬性』的檔案,舉例來說, 
                 我們要查找 -rwxr--r-- ,亦即744 的檔案,使用 -perm -744, 
                 當一個檔案的屬性為 -rwxr-xr-x ,亦即 755時,也會被列出來, 
                 因為 -rwxr-xr-x 的屬性已經囊括了 -rwxr--r-- 的屬性了。 
                 所以:-mode其實就是查找比mode值大的檔案
 -perm +mode :查找檔案屬性『包含任一 mode 的屬性』的檔案,舉例來說,我們查找
                 -rwxr-xr-x ,亦即 -perm +755 時,但一個檔案屬性為 -rw------- 
                 也會被列出來,因為他有 -rw.... 的屬性存在 
[root@test test1]# ll
總用量 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 有一個可寫就匹配
find -perm -222     u g o 全都可寫才匹配
find . -perm 777 查找當前及子目錄下所有權限為777的文件

-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忽略某個目錄, 使用這一選項可以使find命令不在當前指定的目錄中查找,如果同時使用-depth選項,那么-prune將被find命令忽略。( 使用find查找文件的時候怎么避開某個文件目錄)
eg:

如果在查找文件時希望忽略某個目錄,因為你知道那個目錄中沒有你所要查找的文件,那么可以使用-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搭配使用

連接符
-a and 邏輯與 ;多參數條件查找
-o or 邏輯或;
$find /doc \( -name 'ja*' -o- -name 'ma*' \)       //從 /doc 目錄開始往下找,找尋文件名是 ja 開頭或者 ma開頭的文件。
$find . "(" -name "*.m" -or -name "*.mm" -or -name "*.cpp" -or -name "*.h" -or -name "*.rss" ")" -print | xargs wc -l  就可以得到項目的總代碼行數。
  組合測試需要用括號
$find / \( -name "test*" -or -newer afile \) -type f -print
$find . -name *.c  -or -name *.cpp
$find /etc -name init* -a -type f/l/d 二進制文件/軟鏈接文件/目錄; -a是默認的,一般不寫 $find /etc -name inittab -o -size +204800

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

!

 

避開多個文件夾

find /usr/sam \( -path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -print;圓括號表示表達式的結合。

 \ 表示轉義字符,即指示 shell 不對后面的字符作特殊解釋,轉義字符

find /usr/sam \(-path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -name "temp" -print

 查找某一確定文件,-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"

 

 

 

 

 

-user 按照文件屬主來查找文件。
-nouser 查找無有效屬主的文件,即該文件的屬主在/etc/passwd中不存在
eg:
$find ~ -user sam;在$HOME目錄中查找文件屬主為sam的文件
$find / -type f -user qmfsun -exec ls -l {} \;     //在系統根目錄下查找文件屬主為qmfsun的文件
$find /etc -user uucp;在/etc目錄下查找文件屬主為uucp的文件
 
為了查找屬主帳戶已經被刪除的文件,可以使用-nouser選項。這樣就能夠找到那些屬主在/etc/passwd文件中沒有有效帳戶的文件。在使用-nouser選項時,不必給出用戶名; find命令能夠為你完成相應的工作。
 例如,希望在/home目錄下查找所有的這類文件,可以用:
 find /home -nouser;
 

 

-group 按照文件所屬的組來查找文件。
-nogroup 查找無有效所屬組的文件,即該文件所屬的組在/etc/groups中不存在。
eg: 
$ find /apps -group gem -print;在/apps目錄下查找屬於gem用戶組的文件
 
要查找沒有有效所屬用戶組的所有文件,可以使用nogroup選項。下面的find命令從文件系統的根目錄處查找這樣的文件
 $ find / -nogroup-print
 

 

-mtime -n +n 按照文件的更改時間來查找文件,注意: 單位都是以天計算的,可以使用mtime,atime或ctime選項
-mtime:文件內容修改時間
-atime:文件被讀取或者訪問時間
-ctime:文件狀態變化時間(權限等,文件的移動也是由這個時間決定)

-amin n  查找系統中最后N分鍾訪問的文件

-atime n  查找系統中最后n*24小時訪問的文件

-cmin n  查找系統中最后N分鍾被改變文件狀態的文件

-ctime n  查找系統中最后n*24小時被改變文件狀態的文件

-mmin n  查找系統中最后N分鍾被改變文件數據的文件

-mtime n  查找系統中最后n*24小時被改變文件數據的文件

以上的這些時間都需要與一個值n結合使用,只需指定+n、n、-n
- n表示文件更改時間距現在n天以內,
+ n表示文件更改時間距現在n天以前。
n表示文件更改時間距現在恰好n天。一般不會查到結果,因為它要求完全吻合
find命令還有-atime和-ctime 選項,但它們都和-m time選項。如果希望按照更改時間來查找文件。用減號-來限定更改時間在距今n日以內的文件,而用加號+來限定更改時間在距今n日以前的文件。
$ find / -mtime -5;在系統根目錄下查找更改時間在5日以內的文件
$ find /var/adm -mtime +3;在/var/adm目錄下查找更改時間在3日以前的文件
$ find  ./  -mtime  -1  -type f  -exec  ls -l  {} \;(查詢當天修改過的文件)
$find / -amin -10;查找在系統中最后10分鍾訪問的文件
$find / -atime -2;查找在系統中最后48小時訪問的文件
$find / -empty;查找在系統中為空的文件或者文件夾
$find / -mmin -5;查找在系統中最后5分鍾里修改過的文件
$find / -mtime -1;查找在系統中最后24小時里修改過的文件
 
 
 
   -amin n 查找系統中最后N分鍾訪問的文件
 
 -atime n 查找系統中最后n*24小時訪問的文件
$ find -atime -2;超找48小時內修改過的文件
 
 -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   查看文件訪問時間

 

 

 

-type 查找某一類型的文件,諸如:b - 塊設備文件。d - 目錄。c - 字符設備文件。p - 管道文件。l - 符號鏈接文件。f - 普通文件。
諸如: b - 塊設備文件。d - 目錄。c - 字符設備文件。p - 管道文件。l - 符號鏈接文件。f - 普通文件
 
$ find /etc -type d;在/etc目錄下查找所有的目錄 
$ find . ! -type d;在當前目錄下查找除目錄以外的所有類型的文件
$ find /etc -type l;在/etc目錄下查找所有的符號鏈接文件
$ find . -type f -name "*.log";查找當目錄,以.log結尾的普通文件
 

 

-depth:在查找文件時,首先查找當前目錄中的文件,然后再在其子目錄中查找。
在使用find命令時,可能希望先匹配所有的文件,再在子目錄中查找。使用depth選項就可以使find命令這樣做
 $ find / -name "CON.FILE" -depth;
find命令從文件系統的根目錄開始,查找一個名為CON.FILE的文件,它將首先匹配所有的文件然后再進入子目錄中查找
 
 
 
 
 
 
 
 
 
 
size n :查找滿足指定大小的文件
[c] 查找文件長度為n塊的文件, 帶有c時表示文件長度以字節計,帶k表示千字節
可以按照文件長度來查找文件,這里所指的文件長度既可以用塊(block)來計量,也可以用字節來計量。 以字節計量文件長度的表達形式為N c;以塊計量文件長度只用數字表示即可。
  - size [+/-]100[c/k/M/G]: 表示文件的長度為等於[大於/小於]100塊[字節/k/M/G]的文件。
    -empty: 查找空文件。
-size 2(K|M|G)

-size +2M大於2M的文件

-size -1k小於1k的

-size 2M介於2M正負1M范圍內的文件

 
在按照文件長度查找文件時,一般使用這種以字節表示的文件長度,在查看文件系統的大小,因為這時使用塊來計量更容易轉換。
在當前目錄下查找文件長度大於1 M字節的文件:
 
$ find . -size +1000000c;在當前目錄下查找文件長度大於1 M字節的文件
 
$ find . -size +1000c;查找當前目錄大於1K的文件
 
$ find /home/apache -size 100c;在/home/apache目錄下查找文件長度恰好為100字節的文件
 
$ find . -size +10;在當前目錄下查找長度超過10塊的文件( 一塊等於512字節)
$ find / -type f -name *.zip -size +100M -exec rm -i {} \;(刪除大於100M的*.zip文件)
 
find test -type f -size 0 -exec mv {} /tmp/zerobyte \;     //搜索所有零字節文件並將它們移至 /tmp/zerobyte 文件夾

-exec 操作允許 find 在它遇到的文件上執行任何 shell 命令。大括號允許移動每個空文件

 
4.find命令配合【-print -exec -ok】使用,完成更復雜的搜索
這種情況在工作中用的最多
  • -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 . -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 34928 2003-02-25 ./conf/httpd.conf
-rw-r--r-- 1 root root 12959 2003-02-25 ./conf/magic
-rw-r--r-- 1 root root 180 2003-02-25 ./conf.d/README
 上面的例子中,find命令匹配到了當前目錄下的所有普通文件,並在-exec選項中使用ls -l命令將它們列出。
 
$find . -type f -size 0 -exec mv {} /tmp/qmfsun \;        //將當前目錄下所有0字節的文件都移動到/tmp/qmfsun目錄下
$find . -name "*.log" -exec mv {} .. \;
 
$ find logs -type f -mtime +5 -exec rm { } \;(在/logs目錄中查找更改時間在5日以前的文件並刪除它們)
  記住:在shell中用任何方式刪除文件之前,應當先查看相應的文件,一定要小心!當使用諸如mv或rm命令時,可以使用-exec選項的安全模式。它將在對每個匹配到的文件進行操作之前提示你。
 
在下面的例子中, find命令在當前目錄中查找所有文件名以.LOG結尾、更改時間在5日以上的文件,並刪除它們,只不過在刪除之前先給出提示。
$ find . -name "*.conf" -mtime +5 -ok rm {} \;
< rm ... ./conf/httpd.conf > ? n
 按y鍵刪除文件,按n鍵不刪除。
 
在下面的例子中我們使用grep命令。find命令首先匹配所有文件名為“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后執行grep命令看看在這些文件中是否存在一個sam用戶。
 $ find /etc -name "passwd*" -exec grep "sam" {} \;
sam:x:501:501::/usr/sam:/bin/bash
 
為了查找系統中所有文件長度為0的普通文件,並列出它們的完整路徑
$ find / -type f -size 0 -exec ls -l {} \;
 
查找/var/logs目錄中更改時間在7日以前的普通文件,並在刪除之前詢問它們;
$ find /var/logs -type f -mtime +7 -ok rm { } \;
 
為了查找系統中所有屬於root組的文件;
$find . -group root -exec ls -l { } \;
-rw-r--r-- 1 root root 595 10月 31 01:09 ./fie1
 
find命令將刪除當目錄中訪問時間在7日以來、含有數字后綴的admin.log文件。
該命令只檢查三位數字,所以相應文件的后綴不要超過999。先建幾個admin.log*的文件 ,才能使用下面這個命令
$ find . -name "admin.log[0-9][0-9][0-9]" -atime -7 -ok rm { } \;
< rm ... ./admin.log001 > ? n
< rm ... ./admin.log002 > ? n
< rm ... ./admin.log042 > ? n
< rm ... ./admin.log942 > ? n
 
5、 find命令配合xargs:非常常用
注意:xarg后面也是接需要執行的命令
在使用 find命令的-exec選項處理匹配到的文件時, find命令將所有匹配到的文件一起傳遞給exec執行。但有些系統對能夠傳遞給exec的命令長度有限制,這樣在find命令運行幾分鍾之后,就會出現溢出錯誤。錯誤信息通常是“參數列太長”或“參數列溢出”。這就是xargs命令的用處所在,特別是與find命令一起使用
find命令把匹配到的文件傳遞給xargs命令,而xargs命令每次只獲取一部分文件而不是全部,不像-exec選項那樣。這樣它可以先處理最先獲取的一部分文件,然后是下一批,並如此繼續下去。
在有些系統中,使用-exec選項會為處理每一個匹配到的文件而發起一個相應的進程,並非將匹配到的文件全部作為參數一次執行;這樣在有些情況下就會出現進程過多,系統性能下降的問題,因而效率不高;
而使用xargs命令則只有一個進程。另外,在使用xargs命令時,究竟是一次獲取所有的參數,還是分批取得參數,以及每一次獲取參數的數目都會根據該命令的選項及系統內核中相應的可調參數來確定。
來看看xargs命令是如何同find命令一起使用的,並給出一些例子。
下面的例子查找系統中的每一個普通文件,然后使用xargs命令來測試它們分別屬於哪類文件
eg:
在當前目錄中查找以file打頭的文件 ,然后把結果保存到/tmp/core.log 文件中:
#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
 
用grep命令在所有的普通文件中搜索hostname這個詞:
$ 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 中匹配多個關鍵

 

 

 
Linux下find一次查找多個指定文件或者排除某類文件,在 grep 中匹配多個關鍵字的方法
(1)Linux下find一次查找多個指定文件:
查找a.html和b.html
  1. find . -name "a.html"  -name "b.html"  

find . -regex '.*\.txt\|.*\.doc\|.*\.mp3'
  1. find . -regex '.*\.txt\|.*\.doc\|.*\.mp3'  
  2. ./a.txt  
  3. ./a.doc  
  4. ./a.mp3  

(2)排除某些文件類型:
排除目錄下所有以html結尾的文件:
  1. find . -type f ! -name "*.html"    

  1. find . -type f ! -name "*.html"       
  2. ./ge.bak.02.09  
  3. ./ge.html.changed.by.jack  
  4. ./a.txt  
  5. ./a.doc  
  6. ./a.mp3  

(3)排除多種文件類型的示例:
  1. 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:只顯示文件名
  1. root@116.255.139.240:~/a# grep -r -E '0341028|100081|10086|10001' *  
  2. a.txt:100081  
  3. b.txt:10086  
  4. c/cc.txt:0341028  
  5. c/cc.txt:100081  
  6. c/cc.txt:10086  
  7. c/cc.txt:10001  
  8. c.txt:10001  
  9. d.txt:0341028  

  1. grep -r  -E -l '0341028|100081|10086|10001' *     
  2. a.txt  
  3. b.txt  
  4. c/cc.txt  
  5. c.txt  
  6. d.txt  

多種類型文件示例:
 
  1. find . -name "*.html" -o -name "*.js"|xargs grep -r "BusiTree"   


用Awk:
 
  1. 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 /home/zth -path "/home/zth/astetc" -prune -o -type f -print
 

按照文件名來搜索則為:  

find /home/zth -path "/home/zth/astetc" -prune -o -type f -name "cdr_*.conf" -print
 

要忽略兩個以上的路徑如何處理?  

find /home/zth /( -path "/home/zth/astetc" -o -path "/home/zth/etc" /) -prune -o -type f  -print find /home/zth /( -path "/home/zth/astetc" -o -path "/home/zth/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"

通過以上的例子,大家應該可以掌握find命令查找文件時,忽略相關目錄的方法了。

 

如果想同時刪除A和B文件則可以用-o   連接條件  
find   -name   "*"   -o   -name   "A"   -o   -name   "B"   -newer   A   !   -newer   B   -exec   rm   -f   {}   \;  

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM