[一文一命令]find命令详解
Find命令
名稱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
/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命令來說就是錯誤的。
total 4
-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
--------------------------------------------------------------------
轉載于:https://blog.51cto.com/atong/1343666
總結
以上是生活随笔為你收集整理的[一文一命令]find命令详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript中求根的函数有哪些
- 下一篇: Denyhosts