1.find命令简介

find是Linux下一个功能十分强大的文件查找命令,我们可以通过指定其选项,或组合多种条件,来完成我们想要的文件查找工作;find是一种实时查找工具,准确率高,但查询速度比较慢,尤其是在一个大的文件系统进行查找操作的时候,你就会体验到,所以在实际的生产环境中,我们通常会把它放在后台执行;find命令也支持文件名通配。

2.find命令语法

find  [查找路径] [选项] [查找条件] [处理动作]

温馨提示:

   查找路径:默认为当前目录

   查找条件:默认为查找指定路径下的所有文件

   处理动作:默认为打印到标准输出,即屏幕

3.功能

在指定文件系统中查找指定类型的文件,并执行相应的操作

4:选项

-name 文件名: 根据文件名查找文件(文件名默认区分大小写,且支globbing);
          常用的文件名通配符:
           *: 匹配任意长度的任意字符;
           ?:匹配任意单个字符;
          []: 匹配指定字符范围内的任意单个字符;
          [^]: 匹配指定字符范围外的任意单个字符;
-iname 文件名:根据文件名查找文件,且不区分文件名大小写; -i,ignore;
-size [+|-]unit: 根据文件大小查找文件;
          例如:-size +2M
           常用单位:k, M, G
           #: (#-1)<x<=#
-user 用户名:根据文件属主来查找文件;
-group 组名:根据文件所属的基本组来查找文件;
-nouser 用户名: 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在;
-nogroup 组名查: 找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在
-uid uid号: 根据文件属主的UID号,查找文件;
-gid gid号: 根据文件属主的UID号,查找文件;
-type 文件类型:根据文件类型查找文件;
         常用符号及对应文件类型如下:
          f: 普通文件
          d: 目录
          b: 块设备
          c: 字符设备
          l: 符号链接文件
          p: 命名管道
          s: 套接字
-perm  [+|-]MODE
          MODE:精确匹配;
          +MODE: 任何一类用户的任何一位权限匹配;常用于查找某类用户的某特定权限是否存在;
          -MODE: 每类用户的指定要检查的权限位都匹配;
-atime [+|-]n: 表访问时间,以天为单位
           +: 表示(#+1)天之外被访问过;
           -: 表示#天之内被访问过;
           无符号:表示短于(#+1)> x >=#天的时间段被访问过;
-ctime [+|-]n:表创建时间,以天为单位
-mtime [+|-]n:表修改时间,以天为单位
-amin  [+|-]n:表访问时间,以分钟为单位
-cmin  [+|-]n:表创建时间,以分钟为单位
-mmin  [+|-]n:表修改时间,以分钟为单位
-o:或,或者,组合条件判断的常用选项之一
-a:与,并且,组合条件判断的常用选项之一
-mount:在查找文件时不跨越文件系统mount点;
-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件;
-cpio:对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。
默认处理动作是显示到标准输出(屏幕)
-exec commands {} \;: 对查找到的文件执行指定的命令
-ok commands {} \; : 交互式的-exec;
| xargs commands:作用和-exec类似,不过它可以接收更多的参数

xargs的意义:

   find命令将所有匹配到的文件一起传递给-exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。  

   find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。


5.实例:

(1).查找/root/Test目录下,所有后缀以txt结尾的文件。

[root@station70 ~]# find ./Test/ -name "*.txt"
./Test/test6.txt
./Test/test7.txt
./Test/test3.txt
./Test/Test4.txt

(2).根据文件大小查找文件;验证选项部分的理论

创建测试文件

dd if=/dev/zero of=test1.txt bs=10000 count=210
dd if=/dev/zero of=test2.txt bs=10000 count=120
dd if=/dev/zero of=test3.txt bs=1000 count=98
dd if=/dev/zero of=test5.txt bs=1000 count=1000
dd if=/dev/zero of=test6.txt bs=1000 count=1100
dd if=/dev/zero of=test7.txt bs=1000 count=5800
dd if=/dev/zero of=test8.txt bs=1 count=0
dd if=/dev/zero of=test9.txt bs=1000 count=2900

操作:

[root@station70 testdir]# find . -size 1M   #显示的是大小大于0K,但小于等于1M的文件
.
./test4.txt
./test5.txt
./test3.txt
[root@station70 testdir]# find . -size -1M  #显示的是大小小于等于0M的
./test8.txt
[root@station70 testdir]# find . -size 2M  #查找的文件大小大于1M,但小于2M的
./test2.txt
./test6.txt
[root@station70 testdir]# find . -size -2M #查找的文件大小小于等于1M
.
./test4.txt
./test8.txt
./test5.txt
./test3.txt
[root@station70 testdir]# find . -size 3M  #查找的文件大小大于2M,但小于3M的
./test1.txt
[root@station70 testdir]# find . -size -3M  #查找的文件大小小于等于2M
.
./test4.txt
./test2.txt
./test8.txt
./test6.txt
./test5.txt
./test3.txt

(3)查找/root/Test目录下在1天之内修改过的文件

[root@station70 Test]# find . -mtime -1
.
./test6.txt
./test7.txt

(4)查找/root/Test目录下在1天中修改过的文件

[root@station70 Test]# find . -mtime 1

(5)查找/root/Test目录下在1天之外修改过的文件

[root@station70 Test]# find . -mtime +1
./test3.txt

(6)查找/var/目录属主为root且属组为mail的所有文件;

[root@station70 Test]# find /var/ -user root -group mail
/var/spool/mail
/var/spool/mail/root

(7)查找/usr目录下不属于root、bin或hadoop的所用文件;

查看当前系统上是否有这三个用户,若没有,则自行添加
# egrep --color=auto '\<^root|^bin|^hadoop\>' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
hadoop:x:514:514::/home/hadoop:/bin/bash
法1:
# find /usr/ -not -user root  -not -user bin -not  -user hadoop
/usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
法2:
# find /usr/ -not \( -user root -o -user bin -o  -user hadoop \)
/usr/libexec/abrt-action-install-debuginfo-to-abrt-cache

(8)查找/etc/目录下最近一周内其内容修改过的,且不属于root或hadoop的文件;

# find /etc/ -mtime -7 -not -user root -not -user hadoop
/etc/1.txt
# find /etc/ -mtime -7 -not \( -user root -o -user hadoop \)
/etc/1.txt

(9)查找当前系统上没有属主或属组,且最近1个月内曾被访问过的文件;

[root@station70 Test]# find / \( -nouser -o -nogroup \) -atime -30
....由于内容过多,所以只展示部分符合条件的行
/root/axel-2.4/conf.h
/root/axel-2.4/ftp.c
/root/axel-2.4/conn.h

(10)查找/etc/目录下大于1M且类型为普通文件的所有文件;

# find /etc/ -type f -size +1M -exec ls -lh {} \;
-rw-r--r--. 1 root root 2.0M Feb 12 16:50 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml
-rw-r--r--. 1 root root 6.9M Feb 12 16:47 /etc/selinux/targeted/modules/active/policy.kern
-rw-r--r--. 1 root root 6.9M Feb 12 16:47 /etc/selinux/targeted/policy/policy.24

(11)查找/etc/目录所有用户都没有写权限的文件;

[root@station70 ~]# find /etc/ -not -perm +222
/etc/dbus-1/system.d/cups.conf
/etc/shadow-
/etc/sudoers
/etc/pam.d/cups
/etc/gshadow
/etc/shadow
/etc/ld.so.conf.d/kernel-2.6.32-358.el6.x86_64.conf
/etc/rc.d/init.d/lvm2-monitor
/etc/rc.d/init.d/blk-availability
/etc/rc.d/init.d/lvm2-lvmetad

(12)查找/etc/目录下至少有一类用户没有写权限;

# find /etc/ -not -perm -222
....内容过多,在这仅展示部分内容
/etc/sasl2
/etc/sasl2/smtpd.conf
/etc/logrotate.conf

(13)查找/etc/init.d/目录下,所有用户都有执行权限且其它用户有写权限的文件;

[root@station70 Test]# find /etc/init.d/  -perm -113
无任何输出表示没有找到符合条件的文件

(14)借助find为某类型的文件改名

[root@station70 Test]# ls
2  t3  test3.txt  Test4.txt  test6.txt  test7.txt
[root@station70 Test]# find ./* -iname "*.txt" -exec mv {} {}doc \;
[root@station70 Test]# ls
2  t3  test3.txtdoc  Test4.txtdoc  test6.txtdoc  test7.txtdoc

注:{}的作用相当于占位符,它可以把find命令的查找结果转交给-exec,并做commands的参数。

   find [路径] [选项] -exec commands {} \;