歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

AIX中find命令和xargs命令介紹

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" 
注意\表示轉義 
Copyright © Linux教程網 All Rights Reserved