文件查找
# find查找概述
# 为什么需要查找
- 很多时候我们可能会忘了文件所在的位置;
- 有时候需要通过内容查找到对应的文件;
# 为什么是find
因为find命令可以根据不同的条件来进行查找文件,从而实现精准定位
- 文件名称
- 文件大小
- 文件时间
- 属主属组
- 权限等等
# find命令语法
命令 | 路径 | 选项 | 表达式 | 动作 |
---|---|---|---|---|
find | [path...] | [options] | [expression] | [action] |
查找 | 地区 | 妹纸 | 18-25岁 | ??? |
# find查找示例
# find基于名称查找
# 创建文件
[root@web ~]# touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}
# 查找/etc目录下包含ifcfg-eth1名称的文件
[root@web ~]# find /etc -name "ifcfg-eth1"
# -i忽略大小写
[root@web ~]# find /etc -iname "ifcfg-eth1"
#查找/etc目录下包含ifcfg-eth名称所有文件
[root@web ~]# find /etc -name "ifcfg-eth*"
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# find基于大小查找
# 查找大于5M的文件
[root@web ~]# find /etc -size +5M
# 查找等于5M的文件
[root@web ~]# find /etc -size 5M
# 查找小于5M的文件
[root@web ~]# find /etc -size -5M
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# find基于类型查找
# f 文件
[root@web ~]# find /dev -type f
# d 目录
[root@web ~]# find /dev -type d
# l 链接
[root@web ~]# find /dev -type l
# b 块设备
[root@web ~]# find /dev -type b
# c 字符设备
[root@web ~]# find /dev -type c
# s 套接字
[root@web ~]# find /dev -type s
# p 管道文件
[root@web ~]# find /dev -type p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# find基于时间查找
-mtime
:根据文件的修改时间查找-ctime
:根据文件的改变时间查找-atime
:根据文件的访问时间查找
以mtime
举例
# 创建测试文件
[root@web ~]# for i in {01..10];do date -s 201904$i && touch file-$i;done
# 查找7天以前的文件(不算当天往前数7天)
[root@web ~]# find ./ -iname "file-*" -mtime +7
# 查找最近7天的文件,不建议使用(算当天)
[root@web ~]# find ./ -iname "file-*" -mtime -7
# 查找第7天文件(不算当天往前数第7天)
[root@web ~]# find ./ -iname "file-*" -mtime 7
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# find基于用户查找
# 查找属主是jack
[root@web ~]#find /home -user jack
# 查找属组是admin
[root@web ~]#find /home -group admin
# 查找属主是jack,并且属组是admin
[root@web ~]# find /home -user jack -a -group admin
# 查找属主是jack,或者属组是admin
[root@web ~]# find /home -user jack -o -group admin
# 查找没有属主
[root@web ~]# find /home -nouser
# 查找没有属组
[root@web ~]# find /home -nogroup
# 查找没有属主或属组
[root@web ~]# find /home -nouser -o -nogroup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# find基于权限查找
# 精确
[root@web ~]# find /root -type f -perm 644 -ls
# 包含
[root@web ~]# find /root -type f -perm -644 -ls
# 特殊权限
[root@web ~]# find /usr/bin/ /usr/sbin/ -type f -perm -4000 -ls
[root@web ~]# find /usr/bin/ /usr/sbin/ -type f -perm -2000 -ls
[root@web ~]# find /usr/bin/ /usr/sbin/ -type f -perm -1000 -ls
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# find逻辑运算符
符号 | 作用 |
---|---|
-a | 与 |
-o | 或 |
-not|! | 非 |
# 查找当前目录下,属主不是root的所有文件
[root@web ~]# find . -not -user root
[root@web ~]# find . ! -user root
# 查找当前目录下,属主属于hdfs,并且大小大于1k的文件
[root@web ~]# find . -type f -a -user hdfs -a -size +1k
# 查找当前目录下的属主为root或者以xml结尾的普通文件
[root@web ~]# find . -type f -a \( -user hdfs -o -name '*.xml' \)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# find动作处理
查找到一个文件后,需要对文件进行如何处理,
find
的默认动作是-print
动作 含义 -print 打印查找到的内容(默认) -ls 以长格式显示的方式打印查找到的内容 -delete 删除查找到的文件(仅能删除空目录) -ok 后面跟自定义shell命令(会提示是否操作) -exec 后面跟自定义shell命令(标准写法 -exec \;
)
# find结合exec
# 使用-exec实现文件拷贝和文件删除。
[root@web ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;
[root@web ~]# find /etc -name "ifcfg*" -exec rm -f {} \;
1
2
3
2
3
# find结合xargs
# xargs将前者命令查找到的文件作为一个整体传递后者命令的输入
[root@web ~]# touch file.txt
[root@web ~]# find . -name "file.txt" |xargs rm -f
1
2
3
2
3
# find结合grep
[root@web ~]# find ./ -type f | xargs grep -R "xxx.com"
1
Last Updated: 2021/11/12, 16:51:33