find命令

http://blog.chinaunix.net/uid-7340476-id-225283.htmlphp

find命令主要用來在硬盤上搜索文件, find命令主要用於文件查找,列出當前目錄及子目錄下全部的文件和文件夾css

格式:find path -option  "keyword" [-print] [-exec -ok command] {} \;html

path: 查找路徑 ;該命令用於在指定路徑中查找符合條件的文件,搜索路徑可以是多個目錄,不一樣目錄之間用空格分割java

-option: 選項 node

"keyword": 關鍵字 mysql

command: 須要執行的命令linux

-exec command] {} \;將查到的文件執行command操做,注意{}和\;之間有空格web

-ok和-exec的做用同樣,只是-ok在執行前會詢問用戶是否執行操做正則表達式

 

find . -name "*" | xargs grep -i "passwd"(本身工做中用的最多的實例,-i的做用是不區分大小寫sql

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
 
五、 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   {}   \;  

相關文章
相關標籤/搜索