find命令用于在指定的目录下,通过指定的查找条件,查找文件,并且能够对查找到的文件执行相应的处理或者与管道命令结合使用。
find命令格式:
find [options] [-D debugopts] [-Olevel] [path] [expression]
[options]:
-P: 不查找符号链接文件,缺省选项
-L:查找符号链接文件
-H:不查找符号链接,如果查找到的文件不作为expression的参数
-D debugopts:
用于打印诊断信息,用于诊断find查不到你想到的结果
debugopts:
tree: Show the expression tree in its original and optimised form.
stat: Print messages as files are examined with the stat and lstat system calls. The find program
tries to minimise such calls.opt: Prints diagnostic information relating to the optimisation of the expression tree; see the -O
option.rates: Prints a summary indicating how often each predicate succeeded or failed.
-Olevel:
开启查询最优化
level的值为0 , 1 , 2 , 3
[path]:
用于指定查找目录,如果不指定,默认为当前目录
[expression]:
-amin [+|-]n: 文件被访问的时间(分钟)
+n: n分钟以前
-n: n分钟以内
n: n分钟时
-cmin [+|-]n: 文件权限修改时间(分钟)
-mmin [+|-]n: 文件修改时间(分钟)
-atime [+|-]n: 文件被访问的时间(天数)
-ctime [+|-]n: 文件权限修改的时间(天数)
-mtime [+|-]n: 文件被修改的时间(天数)
-fstype type: 文件所在的文件系统类型
-user: 文件的属主
-nouser: 无属主的文件
-uid: 文件的uid
-group: 文件的属组
-nogroup: 无属组的文件
-gid: 文件的gid
-name pattern: 文件名
pattern: 文件名通配符
*: 匹配任意字符
?: 匹配单个字符
[]: 匹配指定范围内的任意单个字符
[^]: 匹配指定范围外的任意单个字符
-iname pattern: 文件名,忽略大小写
-lname pattern: 符号链接文件指向的文件
-ilname pattern: 同-lname,忽略大小写
-inum: 指定文件的inode号
-path pattern: 指定要查找的文件路径
-newer file: 比file修改时间更近
-perm [+|-]mode: 指定文件权限
mode: 精确匹配指定的权限
+mode:任意一类用户(u,g,o)的任何一位权限(r,w,x)匹配
-mode: 每类用户的指定要检查的权限位(r,w,x)都匹配
-size n[cwbkMG]: 文件大小
c: 字节
b: for 512-byte bloc
w: two-byte words
k: kilobytes
M: Megabytes
G: Gigabytes
-type file_type: 文件类型
b: 块设备
c: 字符设备
d: 目录
p: 管道文件
f: 普通文件
l: 链接文件
s: socket文件
-delete : 删除查找到的文件
-exec COMMAND: 对查找到的文件执行命令
-ok COMMAND: 执行命令之前提示
-fprint : 列出查到的文件到标准输出
使用举例:
[root@ccdb ccdb]# date
Wed Aug 27 12:47:57 CST 2014[root@ccdb ccdb]# ll -h /ccdb
total 54G -rwxrwSrwx 1 root root 479M Aug 9 01:14 arc_CCDB_20140809_4hpffnkg_1_1_L0 -rwxrwSrwx 1 root root 481M Aug 10 00:03 arc_CCDB_20140810_4kpfi7qj_1_1_L1 -rwxrwSrwx 1 root root 487M Aug 11 00:03 arc_CCDB_20140811_4npfks6i_1_1_L1 -rwxrwSrwx 1 root root 502M Aug 12 00:03 arc_CCDB_20140812_4qpfngis_1_1_L1 -rwxrwSrwx 1 root root 466M Aug 13 00:03 arc_CCDB_20140813_4tpfq4uh_1_1_L1 -rwxrwSrwx 1 root root 531M Aug 14 00:03 arc_CCDB_20140814_50pfspah_1_1_L1 -rwxrwSrwx 1 root root 502M Aug 15 00:03 arc_CCDB_20140815_53pfvdmt_1_1_L1 -rwxrwSrwx 1 root root 547M Aug 16 01:15 arc_CCDB_20140816_56pg268q_1_1_L0 -rwxrwSrwx 1 root root 475M Aug 17 00:03 arc_CCDB_20140817_59pg4mei_1_1_L1 -rwxrwSrwx 1 root root 465M Aug 18 00:03 arc_CCDB_20140818_5cpg7aqh_1_1_L1 -rwxrwSrwx 1 root root 607M Aug 19 00:04 arc_CCDB_20140819_5fpg9v8g_1_1_L1 -rwxrwSrwx 1 root root 562M Aug 20 00:03 arc_CCDB_20140820_5ipgcjij_1_1_L1 -rwxrwSrwx 1 root root 486M Aug 21 00:03 arc_CCDB_20140821_5lpgf7uj_1_1_L1 -rwxrwSrwx 1 root root 571M Aug 22 00:04 arc_CCDB_20140822_5opghsce_1_1_L1 -rwxrwSrwx 1 root root 621M Aug 23 01:15 arc_CCDB_20140823_5rpgkks8_1_1_L0 -rwxrwSrwx 1 root root 457M Aug 24 00:03 arc_CCDB_20140824_5upgn52h_1_1_L1 -rwxrwSrwx 1 root root 484M Aug 25 00:03 arc_CCDB_20140825_61pgppei_1_1_L1 -rwxrwSrwx 1 root root 499M Aug 26 00:03 arc_CCDB_20140826_64pgsdqs_1_1_L1 -rwxrwSrwx 1 root root 513M Aug 27 00:03 arc_CCDB_20140827_67pgv26t_1_1_L1 -rwxrwSrwx 1 root root 7.7M Aug 14 00:03 c-3388356195-20140814-00.ctl -rwxrwSrwx 1 root root 7.7M Aug 15 00:03 c-3388356195-20140815-00.ctl -rwxrwSrwx 1 root root 7.7M Aug 16 01:15 c-3388356195-20140816-00.ctl -rwxrwSrwx 1 root root 7.7M Aug 17 00:03 c-3388356195-20140817-00.ctl -rwxrwSrwx 1 root root 7.7M Aug 18 00:03 c-3388356195-20140818-00.ctl -rwxrwSrwx 1 root root 7.7M Aug 19 00:05 c-3388356195-20140819-00.ctl -rwxrwSrwx 1 root root 7.7M Aug 20 00:03 c-3388356195-20140820-00.ctl -rwxrwSrwx 1 root root 7.7M Aug 21 00:03 c-3388356195-20140821-00.ctl -rwxrwSrwx 1 root root 7.7M Aug 22 00:04 c-3388356195-20140822-00.ctl -rwxrwSrwx 1 root root 7.7M Aug 23 01:15 c-3388356195-20140823-00.ctl -rwxrwSrwx 1 root root 7.7M Aug 24 00:03 c-3388356195-20140824-00.ctl -rwxrwSrwx 1 root root 7.7M Aug 25 00:03 c-3388356195-20140825-00.ctl -rwxrwSrwx 1 root root 7.7M Aug 26 00:03 c-3388356195-20140826-00.ctl -rwxrwSrwx 1 root root 7.7M Aug 27 00:04 c-3388356195-20140827-00.ctl -rwxrwSrwx 1 root root 10G Aug 9 00:51 db_CCDB_20140809_4gpffjc4_1_1_L0 -rwxrwSrwx 1 root root 4.3G Aug 9 01:12 db_CCDB_20140809_4gpffjc4_2_1_L0 -rwxrwSrwx 1 root root 79M Aug 10 00:01 db_CCDB_20140810_4jpfi7o5_1_1_L1 -rwxrwSrwx 1 root root 85M Aug 11 00:01 db_CCDB_20140811_4mpfks44_1_1_L1 -rwxrwSrwx 1 root root 79M Aug 12 00:01 db_CCDB_20140812_4ppfngg4_1_1_L1 -rwxrwSrwx 1 root root 79M Aug 13 00:01 db_CCDB_20140813_4spfq4s4_1_1_L1 -rwxrwSrwx 1 root root 91M Aug 14 00:01 db_CCDB_20140814_4vpfsp84_1_1_L1 -rwxrwSrwx 1 root root 102M Aug 15 00:01 db_CCDB_20140815_52pfvdk4_1_1_L1 -rwxrwSrwx 1 root root 10G Aug 16 00:51 db_CCDB_20140816_55pg2204_1_1_L0 -rwxrwSrwx 1 root root 4.3G Aug 16 01:12 db_CCDB_20140816_55pg2204_2_1_L0 -rwxrwSrwx 1 root root 79M Aug 17 00:01 db_CCDB_20140817_58pg4mc4_1_1_L1 -rwxrwSrwx 1 root root 77M Aug 18 00:01 db_CCDB_20140818_5bpg7ao4_1_1_L1 -rwxrwSrwx 1 root root 235M Aug 19 00:02 db_CCDB_20140819_5epg9v44_1_1_L1 -rwxrwSrwx 1 root root 89M Aug 20 00:01 db_CCDB_20140820_5hpgcjg4_1_1_L1 -rwxrwSrwx 1 root root 83M Aug 21 00:01 db_CCDB_20140821_5kpgf7s5_1_1_L1 -rwxrwSrwx 1 root root 211M Aug 22 00:02 db_CCDB_20140822_5npghs84_1_1_L1 -rwxrwSrwx 1 root root 10G Aug 23 00:51 db_CCDB_20140823_5qpgkgk6_1_1_L0 -rwxrwSrwx 1 root root 4.3G Aug 23 01:12 db_CCDB_20140823_5qpgkgk6_2_1_L0 -rwxrwSrwx 1 root root 79M Aug 24 00:01 db_CCDB_20140824_5tpgn504_1_1_L1 -rwxrwSrwx 1 root root 81M Aug 25 00:01 db_CCDB_20140825_60pgppc4_1_1_L1 -rwxrwSrwx 1 root root 98M Aug 26 00:01 db_CCDB_20140826_63pgsdo4_1_1_L1 -rwxrwSrwx 1 root root 96M Aug 27 00:01 db_CCDB_20140827_66pgv244_1_1_L1
1.查找17天以前没有修改过的文件
[root@ccdb ccdb]# find /ccdb -mtime +17
/ccdb/arc_CCDB_20140809_4hpffnkg_1_1_L0 /ccdb/db_CCDB_20140809_4gpffjc4_1_1_L0 /ccdb/db_CCDB_20140809_4gpffjc4_2_1_L02.查找17天没有修改过的文件
[root@ccdb ccdb]# find /ccdb -mtime 17
/ccdb/arc_CCDB_20140810_4kpfi7qj_1_1_L1 /ccdb/db_CCDB_20140810_4jpfi7o5_1_1_L13.查找3天内没有修改过的文件
[root@ccdb ccdb]# find /ccdb/* -mtime -3
/ccdb/arc_CCDB_20140825_61pgppei_1_1_L1 /ccdb/arc_CCDB_20140826_64pgsdqs_1_1_L1 /ccdb/arc_CCDB_20140827_67pgv26t_1_1_L1 /ccdb/c-3388356195-20140825-00.ctl /ccdb/c-3388356195-20140826-00.ctl /ccdb/c-3388356195-20140827-00.ctl /ccdb/db_CCDB_20140825_60pgppc4_1_1_L1 /ccdb/db_CCDB_20140826_63pgsdo4_1_1_L1 /ccdb/db_CCDB_20140827_66pgv244_1_1_L14. 查找大于4G的文件
[root@ccdb ccdb]# find /ccdb/* -size +4G -exec ls -lh {} \;
-rwxrwSrwx 1 root root 10G Aug 9 00:51 /ccdb/db_CCDB_20140809_4gpffjc4_1_1_L0 -rwxrwSrwx 1 root root 4.3G Aug 9 01:12 /ccdb/db_CCDB_20140809_4gpffjc4_2_1_L0 -rwxrwSrwx 1 root root 10G Aug 16 00:51 /ccdb/db_CCDB_20140816_55pg2204_1_1_L0 -rwxrwSrwx 1 root root 4.3G Aug 16 01:12 /ccdb/db_CCDB_20140816_55pg2204_2_1_L0 -rwxrwSrwx 1 root root 10G Aug 23 00:51 /ccdb/db_CCDB_20140823_5qpgkgk6_1_1_L0 -rwxrwSrwx 1 root root 4.3G Aug 23 01:12 /ccdb/db_CCDB_20140823_5qpgkgk6_2_1_L0
5.查找普通文件
[root@ccdb ccdb]# find /root/ -type f
/root/.bash_profile /root/.bash_logout /root/install.log /root/.cshrc /root/nohup.out /root/.bash_history /root/.lesshst /root/.bashrc /root/.tcshrc /root/install.log.syslog /root/anaconda-ks.cfg
6.查找/etc/sysconfig/中以config结尾的文件
[root@unp sysconfig]# find /etc/sysconfig/ -iname "*config"
/etc/sysconfig/ /etc/sysconfig/ip6tables-config /etc/sysconfig/authconfig /etc/sysconfig/iptables-config
7.查找/tmp/foo/目录下以install开头的文件,并删除
root@unp foo]# find /tmp/foo/ -iname "install*" -exec rm -rf {} \;
[root@unp foo]# find /tmp/foo/ -iname "install*" | xargs rm –rf
8.查找/tmp/foo/目录下以install开头的文件之外的其它文件,并增加后缀名.txt
[root@unp foo]# find /tmp/foo/* ! -iname "install*" -exec mv {} {}.txt \;
[root@unp foo]# ll total 0 -rw-r--r-- 1 root root 0 Aug 27 13:06 aa.txt -rw-r--r-- 1 root root 0 Aug 27 13:06 bb.txt -rw-r--r-- 1 root root 0 Aug 27 13:06 cc.txt -rw-r--r-- 1 root root 0 Aug 27 13:06 dd.txt -rw-r--r-- 1 root root 0 Aug 27 13:06 install -rw-r--r-- 1 root root 0 Aug 27 13:07 install.bz2 -rw-r--r-- 1 root root 0 Aug 27 13:07 install.gz -rw-r--r-- 1 root root 0 Aug 27 13:07 install.xz
9.查找/tmp/foo/目录下nouser的文件
[root@unp foo]# ll
total 0 -rw-r--r-- 1 4008 4008 0 Aug 27 13:06 aa.txt -rw-r--r-- 1 doc doc 0 Aug 27 13:06 bb.txt -rw-r--r-- 1 root root 0 Aug 27 13:06 cc.txt -rw-r--r-- 1 root root 0 Aug 27 13:06 dd.txt -rw-r--r-- 1 4008 4008 0 Aug 27 13:06 install -rw-r--r-- 1 doc doc 0 Aug 27 13:07 install.bz2 -rw-r--r-- 1 root root 0 Aug 27 13:07 install.gz -rw-r--r-- 1 root root 0 Aug 27 13:07 install.xz [root@unp foo]# find /tmp/foo/ -nouser /tmp/foo/install /tmp/foo/aa.txt