AIX中find命令和xargs命令介紹

531968912發表於2016-07-01
find查詢檔案  
命令格式:  
find  pathname  options[-print -exec -ok]  
pathname :目錄路徑  
-print  :匹配的檔案輸出到標準輸出  
-exec    :對匹配的檔案執行該引數所給出的shell命令  
-ok      :與-exec同,在執行命令前,每次都給出提示  
  
find命令選項  
-name  :按照檔名查詢檔案  
    ~ 表示當前使用者的$HOME目錄  
    . 表示當前目錄及子目錄  
    /etc 表示在/etc目錄下查詢檔案  
    / 表示從根目錄開始查詢【謹慎使用】  
    eg.find ~ -name "*.txt" -print  
  
-perm  :按照檔案許可權查詢檔案  
    使用八進位制表示,rwxrwxrwx(777)  
    eg.find . -perm -755 -print  
  
-prune  :不在當前指定的目錄中查詢,若同時使用了-depth選項,則忽略此選項  
    忽略/apps下的bin目錄  
    eg.find /apps -name "/apps/bin" -prun -o -print  
    在/apps目錄下查詢除了/apps/bin目錄下的所有檔案  
  
-user  :按照檔案屬主查詢檔案  
    eg.find ~ -user scott -print  (查詢scott家目錄下的檔案)  
      find /etc -user tom -print  (查詢/etc目錄下屬於tom的檔案)  
  
-nouser  :查詢無有效屬主的檔案,即改檔案的屬主不在/etc/passwd中  
      查詢屬主帳戶已經被刪除的檔案  
    eg.find /home -nouser print  
  
-group  :按照檔案所屬的組查詢檔案  
    eg.find /apps -group grp01 -print  
    查詢/apps目錄下屬於grp01使用者組的檔案  
  
-nogroup  :查詢無有效屬組的檔案,即檔案所在組不在/etc/groups中  
    eg.find / -group -print (從根目錄查詢沒有組別的檔案)  
  
-mtime -n +n :按照檔案的更改時間查詢檔案(-atime,-ctime)  
    -n:檔案更改時間距現在n天內  
    +n:檔案更改時間距現在n天前  
    eg.find / -mtime -5 -print  (查詢根目錄下距今5日內的檔案)  
      find /var/adm -mtime +3 -print  (距今3天前的檔案)  
  
-newer newest_file1 ! oldest_file2 :查詢更改時間比檔案file1新,  
                                    但比file2檔案就的檔案  
    eg.  
  
-type    :查詢某一型別的檔案,  
    b:塊裝置檔案  
    d:目錄  
    c:字元裝置檔案  
    p:管道檔案  
    l:符號連結檔案  
    f:普通檔案  
    eg.find /etc -type d -print (查詢/etc目錄下的所有目錄)  
      find . ! -type d -print (查詢當前目錄下的除了目錄之外的檔案)  
      find /etc -type l -print (查詢/etc目錄下所有的連結檔案)  
  
-size n[c] :查詢檔案長度為n塊的檔案,c表示檔案長度以位元組計算  
    eg.find .-size +1000000c -print (查詢當前目錄下大於1M位元組的檔案)  
      find /home/apache -size 100c -print  
      (查詢/home/apache目錄下恰好為100位元組的檔案)  
      find . -size +10 -print  
      (查詢當前目錄下長度找過10塊的檔案(1=512位元組))  
  
-depth  :查詢檔案時,首先查詢當前目錄中的檔案,然後再在其字目錄中查詢  
    eg.find / -name "CON.FILE" -depth -print  
      (鍵首先查詢匹配所有的檔案然後再進入字目錄中查詢)  
  
-mount  :在查詢檔案時不跨越檔案系統mount點  
    eg.find . -name "*.XC" -mount -print  
      (查詢當前目錄下本檔案系統中XC結尾的檔案)  
  
-cpio    :對匹配的檔案使用cpio命令,將這些檔案備份到磁帶裝置中  
      
  
-fstype  :查詢位於某一型別檔案系統中的檔案,這些檔案系統型別通常,  
          可以在配置檔案/etc/fstab中找到,改配置檔案中包含了本系統中,  
      有關檔案系統的資訊  
  
-follow  :如果find命令遇到符號連結檔案,就跟蹤到連結所指向的檔案  
  
-exec -ok    :執行shell命令  
          可以使用任何形式的命令,如grep "GetStr"  
    eg.find . -type f -exec ls -l {} \;  
      (查詢當前目錄下的普通檔案,找到周,執行ls -l命令)  
      find logs -type f -mtime +5 -exec rm {} \;  
      (查詢logs目錄下5天前的普通檔案,然後刪除之)  
      find . -name "*.LOG" -mtime +5 -ok rm {} \;  
      (查詢當前目錄下LOG結尾的5天前的檔案,並刪除之)  
examples  
1.查詢使用者目錄下的所有檔案  
  find $HOME -print  
  find ~ -print  
2.查詢當前目錄下,所有大小為0的檔案  
[開發]/usr/xxxx/src>find . -type f -size 0 -exec ls -l {} \;  
-rw-r--r--  1 xxxx    group          0 Sep 14 19:11 ./cds/120031/declare  
-rw-r--r--  1 xxxx    group          0 Jul 25  2011 ./testfortest/S11100/123.fe  
-rw-r--r--  1 xxxx    group          0 Jul 27  2011 ./zzs/ZZS403/ZZS403.fe  
-rw-r--r--  1 xxxx    group          0 Jul 27  2011 ./zzs/ZZS403/ZZS404.fe  
-rw-r--r--  1 xxxx    group          0 Jul 27  2011 ./zzs/ZZS403/456.fe  
-rw-r--r--  1 xxxx    group          0 Aug 17 13:46 ./zzs/zzs020  
-rw-r--r--  1 xxxx    group          0 Aug 24 19:06 ./zzs/ZZA212.  
-rw-r--r--  1 xxxx    group          0 Aug 15  2011 ./tmp/123.fe  
-rw-r--r--  1 xxxx    group          0 Aug 15  2011 ./tmp/456.fe  
-rw-r--r--  1 xxxx    group          0 Aug 15  2011 ./tmp/ZZS403.fe  
-rw-r--r--  1 xxxx    group          0 Aug 15  2011 ./tmp/ZZS404.fe  
  
xargs  
使用-exec命令時,find命令會將所有匹配到的檔案一併傳遞給exec執行,  
此命令有引數限制,若命令過長,則會報“引數列太長”或“引數溢位”等錯誤;  
-xargs命令每次獲取一部分檔案,然後執行命令,並不是全部一併獲取。  
[開發]/usr/xxxx/ytcclb>find . -type f -print |xargs file  
./myfile:      commands text  
./first2:      commands text  
./test.sql:    commands text  
  
查詢/apps/audit目錄下,所有使用者具有讀寫和執行許可權的檔案,並回收其他使用者組的寫許可權:  
find /apps/audit -perm -7 -print | xargs chmod o-w  
  
查詢所有的普通檔案,並搜尋"device"這個詞:  
find / -type f -print | xargs grep "device"  
  
查詢當前目錄下所有的普通檔案,並搜尋DBO這個詞:  
find . -name *\-type f -print | xargs grep "DBO"  
注意\表示轉義 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25462274/viewspace-2121359/,如需轉載,請註明出處,否則將追究法律責任。

相關文章