• 根据文件名或者正则表达式进行搜索
    选项 - name
    选项- iname,忽略大小写

    [root@vm2 find-test]# find /opt/find-test -name "new.sh"
    /opt/find-test/new.sh
    /opt/find-test/n/new.sh
    # 忽略大小写
    [root@vm2 find-test]# find /opt/find-test -iname "new.sh"
    /opt/find-test/new.sh
    /opt/find-test/n/new.sh
    /opt/find-test/n/new.SH
    /opt/find-test/new.SH

    匹配多个条件中的一个,可以使用or条件

    [root@vm2 find-test]# find /opt/find-test \( -name "new.sh" -o -name "new.SH" \)
    /opt/find-test/new.sh
    /opt/find-test/n/new.sh
    /opt/find-test/n/new.SH
    /opt/find-test/new.SH

    选项-path,可以使用通配符匹配文件路径
    选项-ipath,忽略大小写

    [root@vm2 find-test]# find /opt/find-test -path  "*new.sh"
    /opt/find-test/new.sh
    /opt/find-test/n/new.sh
    # 忽略大小写
    [root@vm2 find-test]# find /opt/find-test -ipath  "*new.sh"
    /opt/find-test/new.sh
    /opt/find-test/n/new.sh
    /opt/find-test/n/new.SH
    /opt/find-test/new.SH

    选项-regex,正则匹配
    选项-iregex,忽略大小写

    [root@vm2 find-test]# find . -regex ".*\(\.py\|\.sh\)$"
    ./new.sh
    ./new.py
    ./n/old.sh
    ./n/new.sh
    ./n/new.py
    # 忽略大小写
    [root@vm2 find-test]# find . -iregex ".*\(\.py\|\.sh\)$"
    ./new.sh
    ./new.py
    ./n/old.sh
    ./n/new.sh
    ./n/new.py
    ./n/new.PY
    ./n/new.SH
    ./new.PY
    ./new.SH
  • 否定参数
    !表示否定

    [root@vm2 find-test]# find . ! -name new.txt
  • 基于目录深度的搜索
    选项-maxdepth指定最大深度
    选项-mindepth指定最小深度

    # 默认查找
    [root@vm2 find-test]# find . -name new.sh 
    ./new.sh
    ./n/new.sh
    ./n/n2/new.sh
    # 最大深度
    [root@vm2 find-test]# find . -maxdepth 1 -name new.sh 
    ./new.sh
    [root@vm2 find-test]# find . -maxdepth 2 -name new.sh 
    ./new.sh
    ./n/new.sh
    [root@vm2 find-test]# find . -maxdepth 3 -name new.sh 
    ./new.sh
    ./n/new.sh
    ./n/n2/new.sh
    # 最小深度
    [root@vm2 find-test]# find . -mindepth 1 -name new.sh 
    ./new.sh
    ./n/new.sh
    ./n/n2/new.sh
    [root@vm2 find-test]# find . -mindepth 2 -name new.sh 
    ./n/new.sh
    ./n/n2/new.sh
    [root@vm2 find-test]# find . -mindepth 3 -name new.sh 
    ./n/n2/new.sh
  • 根据文件类型搜索
    选项-type,对文件搜索进行过滤

    # d:目录
    [root@vm2 find-test]# find . -type d 
    .
    ./n
    ./n/n2

    f:普通文件
    l:符号链接
    d:目录
    c:字符设备
    b:块设备
    s:套接字
    p:FIFP

  • 根据文件时间进行搜索
    访问时间-atime:用户最近一次访问文件的时间;
    修改时间-mtime:文件内容最后一次被修改的时间;
    变化时间-ctime:文件元数据(如权限或所有者)最后一次被改变的时间
    (以上单位为:天)
    -atime/-mtime/-ctime可作为find的时间选项。可以用整数值指定,单位是天。还可以带-/+:-表示小于,+表示大于

    # 打印出最近7天内被访问过的所有文件
    [root@vm2 opt]# find . -maxdepth 1 -type f -atime -7
    ./install_mysql.sh
    ./conn.sh
    ./mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz
    ./start.sh
    # 打印出恰好2天前被访问过的所有文件
    [root@vm2 opt]# find . -maxdepth 1 -type f -atime 2
    ./install_mysql.sh
    ./mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz
    # 打印出访问时间超过7天的所有文件
    [root@vm2 opt]# find . -maxdepth 1 -type f -atime +7
    ./clear.sh
    ./install.sh
    ./kill-mongo.sh

    访问时间-amin:用户最近一次访问文件的时间;
    修改时间-mmin:文件内容最后一次被修改的时间;
    变化时间-cmin:文件元数据(如权限或所有者)最后一次被改变的时间
    (以上单位为:分钟)

    # 访问时间超过7分钟的所有文件
    [root@vm2 find-test]# find . -maxdepth 1 -type f -amin +7 
    ./n.txt
    ./new.txt
    ./new.sh
    ./new.py
    ./new.PY
    ./new.SH

    参数-newer,指定一个用于比较时间戳的参考文件,然后找到比参考文件更新(更近的修改时间)的所有文件

    [root@vm2 opt]# find . -maxdepth 1 -type f -newer conn.sh 
    ./install_mysql.sh
    [root@vm2 opt]# stat conn.sh 
    File: `conn.sh'
    Size: 71            Blocks: 8          IO Block: 4096   regular file
    Device: 802h/2050d    Inode: 1053057     Links: 1
    Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2016-05-27 17:57:16.852377034 +0800
    Modify: 2016-04-29 05:45:25.880432420 +0800
    Change: 2016-04-29 05:45:25.884432437 +0800  
    [root@vm2 opt]# stat install_mysql.sh 
    File: `install_mysql.sh'
    Size: 3265          Blocks: 8          IO Block: 4096   regular file
    Device: 802h/2050d    Inode: 1053056     Links: 1
    Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2016-05-25 17:09:46.046655516 +0800
    Modify: 2016-05-25 17:09:40.577657518 +0800
    Change: 2016-05-25 17:09:40.587656924 +0800
  • 基于文件大小的搜索

    # 大于2KB的文件
    [root@vm2 opt]# find . -maxdepth 1 -type f -size +2k
    ./install_mysql.sh
    ./mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz
    # 小于2KB的文件
    [root@vm2 opt]# find . -maxdepth 1 -type f -size -2k
    ./clear.sh
    ./conn.sh
    ./install.sh
    ./kill-mongo.sh
    ./start.sh

    b:块(512字节)
    c:字节
    w:字(2字节)
    k:1024字节
    M:1024K字节
    G:1024M字节

  • 删除匹配的文件
    选项-delete,删除find匹配到的文件

    [root@vm2 find-test]# ls
    n  new.py  new.PY  new.sh  new.SH  new.txt  n.txt
    [root@vm2 find-test]# find . -maxdepth 1 -name new.sh
    ./new.sh
    [root@vm2 find-test]# find . -maxdepth 1 -type f -name  new.sh -delete
    [root@vm2 find-test]# find . -maxdepth 1 -name new.sh
  • 基于文件权限和所有权限匹配
    选项-perm,指定find匹配的权限

    # 找出权限为700的文件
    [root@vm2 find-test]# find . -maxdepth 1 -type f -perm 700
    ./n.txt
    # 找出权限为644的文件
    [root@vm2 find-test]# find . -maxdepth 1 -type f -perm 644
    ./new.txt
    ./new.py
    ./new.PY
    ./new.SH
    # 找出权限不是644的文件
    [root@vm2 find-test]# find . -maxdepth 1 -type f ! -perm 644
    ./n.txt
  • 利用find执行命令行或动作
    选项-exec,与其他命令进行结合。
    {}是一个与-exec选项搭配使用的特殊字符串。对于每个匹配的文件,{}会被替换成相应的文件名。

    [root@vm2 find-test]# find . -maxdepth 1 -type f -user mysql 
    ./new.txt
    [root@vm2 find-test]# find . -maxdepth 1 -type f -user mysql -exec chown zabbix. {} \;
    # find命令找到文件new.txt的所有者为mysql,那么find会执行:chown zabbix. {},然后被解析为:chown zabbix. new.txt
    [root@vm2 find-test]# ll new.txt 
    -rw-r--r-- 1 zabbix zabbix 0 May 27 17:14 new.txt

    有时候并不希望对每个文件都执行一次命令。更希望使用文件列表作为命令行参数,这样可以少用几次命令。可以在exec中使用+来代替