名稱find - search for files in a directory hierarchy
文法find [-H] [-L] [-P] [path...] [expression]
指令參數:
Pathname find指令所查找的目錄路徑。
-print find将比對的檔案的輸出到标準輸出中-->預設查找到不列印出來
-exec find指令對比對的檔案執行該參數所給出的shell指令。相應的形式為‘command’{} \---》也就是說我們用這個參數能夠對我們用find找出來的檔案進行操作。
-ok 和exec的作用相同隻不過是以一種更為安全的模式來執行該參數所給出的shell指令。在執行每一個指令之前都會給出提示讓使用者來确定是否執行。
指令選項
-name 按照檔案的檔案名來查找檔案
-perm 按照檔案權限來查找檔案
-prune 可以讓find指令不在目前目錄裡面查找。
-user 按照檔案所屬的擁有者來查找
-group 按照檔案所屬的使用者組來查找
-nogroup 查找無有效所屬組的檔案即使用者組不在/etc/groups檔案裡面。
-nouser 查找無有效使用者的檔案
-type 查找某一類型的檔案
d 目錄
f 普通檔案
l 符号連結檔案
c 字元裝置檔案
b 塊裝置檔案
-size n 查找檔案長度為n快的檔案帶c時表示檔案長度以位元組計。
-fstype 查找位于某一類型檔案系統中的檔案。
-mount 在查找檔案時不跨越檔案系統mount點
-follow 如果find指令遇到符号連結檔案就跟蹤至連結所指向的檔案
-cpio 對比對的檔案使用cpio指令将這些檔案備份到錄音帶裝置中。
a:access通路 c:change變更 m:modified改動
-amin n 查找系統中最後N分鐘通路(access)的檔案
-atime n 查找系統中最後n 天前通路的檔案 -n表示在n天内+n表示在n天前其餘類似。
-cmin n 查找系統最後n分鐘被改變(change)檔案狀态的檔案
-ctime n 查找系統中最後n*24小時被改動過檔案狀态的檔案
-mmin n 查找系統中最後n分鐘被改動(modified)過檔案資料的檔案
-mtime n 查找系統中最後n*24小時被改變檔案資料的檔案-n +n 按照檔案所更改的時間來查找檔案-n 表示更改在n天内的+n表示更改在n天前的。
使用執行個體
查找指定時間内的檔案
[root@LiWenTong test]# find -amin -1 à通路時間為1天内的檔案。--》錯了應該是1分鐘吧。寫博文的時候發現的。
./test1
./test1/test2
查找建立時間在最近的。c:change 改變檔案狀态的。
find -ctime -19
根據關鍵字查找
[root@LiWenTong test]# find -name *.log
./2.log
根據權限、類型來查找檔案
[root@LiWenTong test]# find / -perm 777 -name *.log
/root/test/2.log
[root@LiWenTong test]# find / -perm 777 -type d
find: /proc/4435/task/4435/fd/4: No such file ordirectory
find: /proc/4435/task/4435/fdinfo/4: No such fileor directory
find: /proc/4435/fd/4: No such file or directory
find: /proc/4435/fdinfo/4: No such file ordirectory
/root/test/test1
[root@LiWenTong test]# find /root/ -type d
/root/
/root/test
/root/test/test1/test2
[root@LiWenTong test]# find /root/ -type f
/root/.cshrc
/root/2.log
/root/.bash_logout
/root/3.log
/root/1.log
/root/test.txt
/root/2lo
--->查找使用者
find -user root
find -type f檔案 d目錄
------以下是一些執行個體----
$find ~ -name "*.txt" -print #在$HOME中查.txt檔案并顯示
$find . -name "*.txt" -print
$find . -name "[A-Z]*" -print #查以大寫字母開頭的檔案
$find /etc -name "host*" -print #查以host開頭的檔案
$find . -name "[a-z][a-z][0–9][0–9].txt" -print #查以兩個小寫字母和兩個數字開頭的txt檔案
$find . -perm 755 -print
$find . -perm -007 -exec ls -l {} \; #查所有使用者都可讀寫執行的檔案同-perm 777
$find . -type d -print
$find . ! -type d -print
$find . -type l -print
$find . -size +1000000c -print #查長度大于1Mb的檔案
$find . -size 100c -print # 查長度為100c的檔案
$find . -size +10 -print #查長度超過期廢棄10塊的檔案1塊=512位元組
$cd /
$find etc home apps -depth -print | cpio -ivcdC65536 -o /dev/rmt0
$find /etc -name "passwd*" -exec grep "cnscn" {} \; #看是否存在cnscn使用者
$find . -name "yao*" | xargs file
$find . -name "yao*" | xargs echo "" > /tmp/core.log
$find . -name "yao*" | xargs chmod o-w
-----小結-------------------------------------------------------------------------------
find指令能夠找到我們想找的任何檔案,因為我們能夠設定的參數有很多。如最簡單的檔案名-name 使用者 -user 類型 -type f d l 。修改、通路的時間也能夠作為我們查找的參數。
并且我們能對找到的檔案進行的操作,隻要後面加上-exec參數。當然這個我們後面再講将怎麼操作查找到的檔案。現在先說怎麼找到我們想要的檔案。名稱查找是否能夠用正規表達式,還需要驗證下。接下來就來講如何對我們查找到的檔案進行操作。
----------------------------------------------------------------------------------------
Find指令之-exec選項
用法簡述:
使用find指令查找出來的檔案有時候需要做一定的處理。我們通過加上-exec選項,并在後面跟上command指令就可以對我們所查找到的檔案進行操作。格式為:-exec command {} \; {}代表的是find查找出來的檔案名。此處存有疑問--》後經驗證确實是{}代表find找出來的檔案,而不是{} \;代表找到的檔案。
1)查找檔案類型并修改權限
[root@LiWenTong test]# find /root/test -type f -exec ls -l {} \;
--->find /root/test -type f -name *.log -print
---->有時候為了保證-exec執行正确可以通過-print 或ls先把find查找的檔案輸出檢視下是否是我們需要操作的檔案以保誤操作。--->這個就是為了在做操作之前先知道下我們要的檔案是不是我們想要的。
-rwxrwxrwx 1 root root 26 Apr 29 02:13/root/test/2.log
-rwxrwxrwx 1 root root 43 Apr 29 02:12/root/test/test1/1.log
-rwxrwxrwx 1 root root 22 Apr 29 02:14/root/test/test1/test2/3.log
[root@LiWenTong test]# find /root/test -type f -exec chmod 755 {} \;
-------exec選項後接的指令講解------------------
-exec後面的指令怎麼接指令,有些同學可能還不能直覺的了解過來。其實很好了解,我們原本的操作指令格式是什麼樣的現在的格式還是什麼樣的。隻是當你需要用到查找出的檔案的時候,就用{}待入就是了。
比如要删除一個檔案:rm -f filename 這個是rm原來的格式,那現在是我們要删除找出來的檔案,帶入就變成 rm -f {},但要記住最後要用\;結束。如果是mv指令呢?原本 mv old.file /tmp 代入後變 mv {} /tmp \;還是mv {} \; /tmp 呢?
---》以上的問題驗證确實是{}代表find找到的檔案後文有執行個體。也就是應該是:mv old.file {} /tmp \;
-----------------------------------------
2)在目前目錄下查找資料修改1天内的檔案
[root@LiWenTong test]# find -type f -mtime -1 -exec ls -l {} \;
-rwxr-xr-x 1 root root 26 Apr 29 02:13 ./2.log
-rwxr-xr-x 1 root root 43 Apr 29 02:12./test1/1.log
-rwxr-xr-x 1 root root 22 Apr 29 02:14./test1/test2/3.log
[root@LiWenTong test]# find -type f -mtime -1 -exec rm -f {} \;
[root@LiWenTong test]# ll
total 4
drwxrwxrwx 3 root root 4096 Apr 29 04:01 test1
3)使用-ok 選項較為安裝的執行指令
使用-ok選項同-exec一樣隻是在做相應的操作時會有提示。
[root@LiWenTong test]# find /root/test -type d -mtime -1 -ok rm {} \;
< rm ... /root/test > ? y
rm: cannot remove `/root/test': Is a directory
< rm ... /root/test/test1 > ? y
rm: cannot remove `/root/test/test1': Is adirectory
< rm ... /root/test/test1/test2 > ? y
rm: cannot remove `/root/test/test1/test2': Is adirectory
4)-exec使用grep選項
[root@LiWenTong test]# find /etc -name "passwd" -exec grep"root" {} \;
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
說明任何形式的指令都可以在-exec選項中使用。---》這個說明很強勁哦,在系統中能夠對檔案進行操作的指令都能夠在這裡使用的。注意咯!
5)使用find查找檔案并将移動指定的目錄下。
[root@LiWenTong tmp]# ll log_mv
total 0
-rw-r--r-- 1 root root 0 Apr 29 07:08 1.log
-rw-r--r-- 1 root root 0 Apr 29 07:08 2.log
-rw-r--r-- 1 root root 0 Apr 29 07:08 3.log
-rw-r--r-- 1 root root 0 Apr 29 07:08 4.log
[root@LiWenTong tmp]# find /tmp/log_mv -name"*.log" -exec mv {} /root/test/ \;
[root@LiWenTong tmp]# ll /root/test--à需要記住{}是find查找出來的檔案那麼-exec的指令就需要對應的把位置放好。因為之前老是寫成 mv /root/test/ {} \; 如果帶入來看 這個mv指令來說就是錯誤的。
-rw-r--r-- 1 root root 0 Apr 29 07:01 1
-rw-r--r-- 1 root root 0 Apr 29 07:08 1.log
-rw-r--r-- 1 root root 0 Apr 29 07:08 2.log
-rw-r--r-- 1 root root 0 Apr 29 07:08 3.log
-rw-r--r-- 1 root root 0 Apr 29 07:08 4.log
drwxr-xr-x 3 root root 4096 Apr 29 07:05 test1
Find指令之xargs選項
功能簡述:
在使用-exec選項進行檔案處理時,有些系統對傳給-exec的指令長度有限制。如果太長在執行時會溢出錯誤、或出現參數太長的錯誤。-exec是一次性的擷取傳來的參數-xargs是分多次的擷取傳來的參數每次擷取多少還是一次擷取是根據該指令的選項及系統的核心中相應的可調參數決定。并且-exec在處理指令時會發起一個相應的程序可能多個。而xargs則隻有一個程序[U2]。如果通過-exec指令來執行如果出現程序很多的情況那麼有可能會影響到系統的性能。
xargs如何從find指令中擷取結果
我們知道-exec選項是通過{}來代入我們查找到的檔案。那麼我們的xargs又是如何擷取我們查找到的檔案呢?其實格式有一下兩種:
1) find -type f | xargs rm ; 看不到什麼代入的{}而是直接xargs 接上指令。那是因為預設的xargs會将(stdin)标準輸入作為我們後面指令的參數。其實rm指令會接上在管道(|)之前的指令所産生的結果,也就是我們find查找出來的檔案。那麼也就相當于是:rm 查找的檔案
2) find -type f |xargs -i mv {} /tmp;這種方式就有代入{}了,相應的也多了個-i的參數。-i參數就是指定我們要用{}來作為find指令結果的替代。這個就和-exec沒啥差別咯。然後現在來看看以下的一些執行個體就可以了。
1)Find查找普通檔案并通過xargs file指令來判斷檔案的類型
[atong@LiWenTong tmp]$ find -type f | xargs file
find: ./test1/tmp/ssh-YmJglw2954: Permissiondenied
./1.txt.tar.gz: gzip compresseddata, from Unix, last modified: Thu May 2 20:14:25 2013
./2.txt: ASCIItext
./tar.man: UTF-8 UnicodeEnglish text, with overstriking
./rc.sysinit: Bourne-Again shellscript text executable
./oldboy/test/test1/test2/test3/test.txt: empty
2)在系統中查找記憶體資訊轉存檔案core dump然後把結果儲存到/tmp/core.log中。
[atong@LiWenTong tmp]$ find / -name "core" | xargs echo"">>/tmp/core.l
3)在目前目錄下查找所有使用者具有讀、寫和執行權限的檔案并收回相應的寫權限
[atong@LiWenTong tmp]$ find -perm 777 | sudo xargs chmod 744--->這裡應為atong沒有權限是以需要在xargs前面加上個sudo 要不會提示權限限制。^_^
-rwxr--r-- 1 root root 167 May 2 20:16 1.tar.gz
-rwxr--r-- 1 root root 45 May 2 20:16 1.txt
-rwxr--r-- 1 atong atong 152 May 2 20:14 1.txt.tar.gz
drwxr--r-- 2 root root 4096 Apr 29 07:0620130425175009
-rwxr--r-- 1 root root 5 May 2 21:41 2.txt
-rw-r--r-- 1 root root 0 May 2 21:41 3.txt
4)用grep指令在所有的普通檔案中搜尋hostname這個詞
[root@LiWenTong tmp]# find -type f | xargs grep "good"
./1.log:good
5)使用xargs執行mv
[root@LiWenTong test4]# find -name "*.log"| xargs -i mv{} test4---》此處的-i選項是表示預設用{}來代表前面的輸出[U3]。
[root@LiWenTong test4]# ll /tmp/test4
total 8
-rw-r--r-- 1 root root 5 May 3 04:12 1.log
-rw-rw-r-- 1 atong atong 619 May 3 03:28 core.log
[root@LiWenTong test4]# find -name"*.log"| xargs -i-p mv {} test4--->-p選項是表示會提示讓你确認是否執行之後的操作。
[U1]《find之-exec》
Find查找到的結果能夠再給-exec作為輸入再進行相應的處理。-exe的格式為-exec command {} \: 其中{}代表的就是find出來的結果那具體放在command之後的哪個位置需要具體看是什麼command
[U2]《exec和xargs的差別》
Exec會有多個程序在執行指令。而xargs隻會有一個程序這樣減少系統資源開銷。Exec對傳過來的指令長度有限制xargs沒有。
[U3]《xargs如何從find擷取結果》
Exec是通過command後的{}作為find的結果傳輸通道。而xargs若沒有指定就直接是作為stdin來傳。如果加上-i參數那麼一樣是通過{}來作為find的結果傳輸通道。
最後附上幾個可以參考的文章
http://www.cnblogs.com/wanqieddy/archive/2011/06/09/2076785.html【這篇有點雜,内容多】
http://oldboy.blog.51cto.com/2561410/792396 【find+sed的執行個體運用很好很強大】
http://oldboy.blog.51cto.com/2561410/792396/
-----------------------------後續自我小結--------------------------
find首先在檔案查找上基本上我們希望找到的檔案它都能夠幫我們找到的。另外就是-exec和xargs參數能夠讓我們對我們所找到的檔案進行相應的操作。其實了解了-exec和xargs的格式含義也不算很難使用隻要我們對我們的操作指令熟悉就可以了其實就隻是一個代入的動作而已。
也正是因為我們的-exec和xargs兩個參數後面能夠跟上我們系統的任何操作指令也就使得我們find指令能夠跟非常多的指令進行結合隻要是我們能想到的操作。并且文中最後也提到了find和sed指令的相結合。本身sed指令就地内容的編輯上很靈活 選址、輸出、替換、改寫等等。這樣兩個很強的指令一結合就變成了你可以對你系統中任何檔案進行任何操作、對檔案内容進行任何操作。
Any operation of any files, make any edits to the contents of any
--------------------------------------------------------------------
本文出自 “從頭開始” 部落格,請務必保留此出處http://atong.blog.51cto.com/2393905/1343666