linux下find命令詳解

來源:互聯網
上載者:User
Linux下find命令詳解Linux下find命令在目錄結構中搜尋檔案,並執行指定的操作。Linux下find命令提供了相當多的尋找條件,功能很強大。

Linux中find常見用法樣本·

find    path    -option    [    -print ]    [ -exec    -ok    command ]    {} /;

#-print 將尋找到的檔案輸出到標準輸出
#-exec    command    {} /;       -----將查到的檔案執行command操作,{} 和 /;之間有空格
#-ok 和-exec相同,只不過在操作前要詢使用者

-name    filename               #尋找名為filename的檔案
-perm                         #按執行許可權來尋找
-user     username              #按檔案屬主來尋找
-group groupname              #按組來尋找
-mtime    -n +n                 #按檔案更改時間來尋找檔案,-n指n天以內,+n指n天以前
-atime     -n +n                #按檔案訪問時間來查GIN: 0px">-perm                          #按執行許可權來尋找
-user     username              #按檔案屬主來尋找
-group groupname              #按組來尋找
-mtime    -n +n                 #按檔案更改時間來尋找檔案,-n指n天以內,+n指n天以前
-atime     -n +n                #按檔案訪問時間來尋找檔案,-n指n天以內,+n指n天以前 
-ctime     -n +n                #按檔案建立時間來尋找檔案,-n指n天以內,+n指n天以前 
-nogroup                      #查無有效屬組的檔案,即檔案的屬組在/etc/groups中不存在
-nouser                       #查無有效屬主的檔案,即檔案的屬主在/etc/passwd中不存
-newer    f1 !f2                找檔案,-n指n天以內,+n指n天以前 
-ctime     -n +n                #按檔案建立時間來尋找檔案,-n指n天以內,+n指n天以前 
-nogroup                      #查無有效屬組的檔案,即檔案的屬組在/etc/groups中不存在
-nouser                       #查無有效屬主的檔案,即檔案的屬主在/etc/passwd中不存
-newer    f1 !f2                #查更改時間比f1新但比f2舊的檔案
-type      b/d/c/p/l/f          #查是塊裝置、目錄、字元裝置、管道、符號連結、普通檔案
-size       n[c]                #查長度為n塊[或n位元組]的檔案
-depth                        #使尋找在進入子目錄前先行尋找完本目錄
-fstype                       #查更改時間比f1新但比f2舊的檔案
-mount                        #查檔案時不跨越檔案系統mount點
-follow                       #如果遇到符號連結檔案,就跟蹤連結所指的檔案
-cpio                         #對匹配的檔案使用cpio命令,將他們備份到磁帶裝置中
-prune                        #忽略某個目錄

更多資訊可以查看:《Linux下find命令詳解》http://www.phpq.net/linux/linux-find.html、《Linux下find命令執行個體》http://www.phpq.net/linux/linux-find-demo.html。

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"    {}    /; -print

2)find / -name filename -exec rm -rf {} /;
     find / -name filename -ok rm -rf {} /;

3)比如要尋找磁碟中大於3M的檔案:
find . -size +3000k -exec ls -ld {} ;

4)將find出來的東西拷到另一個地方
find *.c -exec cp '{}' /tmp ';'
如果有特殊檔案,可以用cpio,也可以用這樣的文法:
find dir -name filename -print | cpio -pdv newdir

6)尋找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
二、linux下find命令的用法1. 基本用法:
      find / -name 檔案名稱
     find ver1.d ver2.d -name '*.c' -print    尋找ver1.d,ver2.d *.c檔案並列印
     find . -type d -print 從目前的目錄尋找,僅尋找目錄,找到後,列印路徑名。可用於列印目錄結構。
2. 無錯誤尋找:
      find / -name access_log 2 >/dev/null
3. 按尺寸尋找:
      find / -size 1500c (尋找1,500位元組大小的檔案,c表示位元組)
      find / -size +1500c (尋找大於1,500位元組大小的檔案,+表示大於)    
      find / -size +1500c (尋找小於1,500位元組大小的檔案,-表示小於)    
4. 按時間:
      find / -amin n 最後n分鐘 
      find / -atime n 最後n天
      find / -cmin n 最後n分鐘改變狀態
      find / -ctime n 最後n天改變狀態
5. 其它:
      find / -empty 空白檔案、空白檔案夾、沒有子目錄的檔案夾
      find / -false 尋找系統中總是錯誤的檔案
      find / -fstype type 找存在於指定檔案系統的檔案,如type為ext2
      find / -gid n 組id為n的檔案
      find / -group gname 組名為gname的檔案
      find / -depth n 在某層指定目錄中優先尋找檔案內容
      find / -maxdepth levels 在某個層次目錄中按遞減方式尋找
6. 邏輯
      -and 條件與 -or 條件或
7. 尋找字串
      find . -name '*.html' -exec grep 'mailto:'{}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.