一、命令介绍

  find 命令用来在目录中搜索文件,它可以根据不同的条件来进行查找文件,例如文件名称、文件类型、文件大小、文件修改时间、属主属组、权限等方式。

二、用法

1、按名称

用法:

选项:
 -name      # 根据名称查找 
 -iname     # 根据名称查找忽略大小写

通配符:  
 *      # 所有
 ?      # 匹配前面的字符零次或一次
 []     # 匹配中括号中的任意一个字符 
 [^]    # 排除中括号中的字符

示例:

# 精准查找
[root@cp ~]# find / -name test.txt

# 查找以什么开头的
[root@cp ~]# find ./ -name 'test*'

# 查找以什么结尾的
[root@cp ~]# find ./ -name '*.txt'

# []和[^]
[root@cp ~]# find ./ -name 'test[1-9].txt'
./test1.txt
[root@cp ~]# find ./ -name 'test[^1-9].txt'
./testA.txt

# ?
[root@cp ~]# find ./ -name 'test[1-9]?.txt'
./test11.txt
[root@cp ~]# find ./ -name 'test[1-9]??.txt'
./test111.txt

2、按类型

用法:

选项:
 -type      # 根据类型进行查找 
    f       # 普通文件  
    d       # 目录
    l       # 软链接
    s       # 套接字,如socket文件
    p       # 管道文件
    b       # 块设备,如硬盘、硬盘分区、镜像光盘
    c       # 字符设备 

示例:

# 查找文件
[root@cp ~]# find ./ -type f -name 'test.txt'

# 查找目录
[root@cp ~]# find ./ -type d -name 'other'

# 查找块设备
[root@cp ~]# find /dev -type b

3、按大小

用法:

选项:
 -size      # 根据文件大小查找 
    +n      # 大于
    -n      # 小于
     n      # 精确匹配 

单位:b、k、M、G

示例:

# 查找大于5M的文件
[root@cp ~]# find ./ -size +5M

# 查找等于5M的文件
[root@cp ~]# find ./ -size 5M

# 查找小于5M的文件
[root@cp ~]# find ./ -size -5M 

4、按修改时间

用法:

选项:
 -mtime     # 最后修改时间 
    +n      # 多少天以前 
    -n      # 多少天以内
     n      # 离现在的前第几天,包括当天

示例:

# 测试环境准备
[root@cp ~]# for i in {01..5};do date -s  2020/01/$i && touch file-${i}.txt;done
[root@cp ~]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  1 00:00 file-01.txt
-rw-r--r-- 1 root root 0 Jan  2 00:00 file-02.txt
-rw-r--r-- 1 root root 0 Jan  3 00:00 file-03.txt
-rw-r--r-- 1 root root 0 Jan  4 00:00 file-04.txt
-rw-r--r-- 1 root root 0 Jan  5 00:00 file-05.txt
[root@cp ~]# date
Sun Jan  5 00:00:05 CST 2020

# 查找3天以前的文件,不包含当天
[root@cp ~]# find ./ -iname "file-*" -mtime +3
./file-01.txt

# 查找最近3天的文件,包含当天
[root@cp ~]# find ./ -iname "file-*" -mtime -3
./file-04.txt
./file-05.txt
./file-03.txt

# 查找离现在的前第3天文件,也就是2号
[root@cp ~]# find ./ -name "file-*" -mtime 3
./file-02.txt

5、按权限

用法:

选项:
 -perm      # 根据权限查找 
    644     # 精确匹配     
    -644    # 包含这些权限 
    /644    # 包含其中的某个权限,三个身份中满足一个则匹配

6、按属主属组

用法:

选项:
 -user      # 根据属主
 -group     # 根据属组
 -nouser    # 没有属主
 -nogroup   # 没有属组

7、动作

-print      # 打印查找到的内容,默认动作
-ls         # 把查找出来的文件以长格形式显示出来文件的详细信息  
-delete     # 把查找出来的文件进行删除,仅能删除空目录
-exec       # 后面跟自定义 shell 命令,标准写法 -exec \;
-ok         # 后面跟自定义 shell 命令,会提示是否操作

示例:

# 使用 -ls 打印查找到的文件,以长格式显示
[root@cp ~]# find / -name 1.txt -ls
17281935    0 -rw-r--r--   1 root     root            0 Dec 22 16:52 /root/1.txt

# 使用 -ok 实现文件拷贝,但会提示是否拷贝
[root@cp ~]# find /root -name '*.txt' -ok cp -a {} /tmp \;
< cp ... /root/4.txt > ? 

# 使用 -exec 实现文件拷贝和文件删除。
[root@cp ~]# find /root -name "*.txt" -exec cp -a {} /tmp \;
[root@cp ~]# find /tmp -name "*.txt" -exec rm -vf {}  \;

# 使用 -delete 删除文件,但仅能删除空目录
[root@cp ~]# find /root -name "*.txt" -delete

8、逻辑运算符

用法:

-a      # 与
-o      # 或
-not|!  # 非

示例:

# 查找属主属于cp且大小大于1M的文件
[root@cp ~]# ind ./ -type f -a -user cp -a -size +1M

# 查找属主为cp或者以txt结尾的普通文件
[root@xuliangwei ~]# find ./ -type f -a \( -user hdfs -o -name '*.txt' \)

# 查找当前目录下,属主不是cp的所有文件
[root@cp ~]# find ./ -not -user cp
[root@cp ~]# find ./ ! -user hdfs

8、其它
指定目录层级:

[root@cp ~]# find ./ -maxdepth 1 -type f

排除指定目录:

[root@cp ~]# find ./ -path "./test" -prune -o -name '*.txt' -print

查找比某个文件新或旧的文件:

# 查找更改时间比文件 1.txt 新的文件
[root@cp ~]# find -newer 1.txt 

发表评论

验证码: 20 − 18 =