AIX中find命令和xargs命令介紹 [plain] www.2cto.com 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" 注意\表示轉義