红联Linux门户
Linux帮助

AIX中find命令和xargs命令介绍

发布时间:2014-08-03 21:46:26来源:linux网站作者:BOBO12082119

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"
注意\表示转义