持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第29天,点击查看活动详情
1. find 命令
1.1 find 介绍
Linux 的 find 命令用来在指定目录下查找文件,允许根据不同的标准来查找指定文件和目录,任何位于参数之前的字符串都将被视为查找的目录名。
默认命令
使用 find 命令时,如果后面不跟任何参数,仅使用 find 命令,代表查询当前文件夹下所有文件和子文件夹。
[root@VM-0-5-centos schedules]# find
.
./select_day.py
./log.out
1.2 find 语法
find 命令使用语法: find path -option [-print] [-exec -ok command] {}
-
path,代表路径,即在哪个路径下查询指定文件
-
/
代表根目录 -
.
代表当前目录 -
缺省,代表当前目录
-
-
-option,操作选项,用来指定查询条件应用的文件的属性信息,
-
-name,根据文件名称
-
-type,根据文件类型
-
-size,根据文件大小
-
-
-print,打印文件的完整信息,默认使用,可以省略
-
-exec -ok command,可以与其他命令结合使用,如 ls 、 sort 等命令,实现查询同时执行其他命令操作
2. find 命令常用操作
find 常用命令格式: find [path] [-options] 搜索内容
2.1 按照文件名搜索
在 -option 操作选项中,按照名称的方式有三种
-
-name: 按照文件名搜索
-
-iname: 按照文件名搜索,不区分文件名大小
-
-inum: 按照 inode 号搜索
# 在根目录下查询名为 nginx 的文件(包括目录)
[root@VM-0-5-centos schedules]# find / -name nginx
/var/lib/yum/repos/x86_64/7/nginx
/var/log/nginx
/var/cache/yum/x86_64/7/nginx
/var/cache/nginx
/usr/libexec/initscripts/legacy-actions/nginx
/usr/share/nginx
/usr/sbin/nginx
/usr/lib64/nginx
/etc/nginx
/etc/logrotate.d/nginx
# 在当前目录下查询名称为 Nginx.conf 的文件,注意不区分大小写,所有满足的均会被查询到
[root@VM-0-5-centos /]# pwd
/
[root@VM-0-5-centos /]# find -iname Nginx.conf
./etc/nginx/nginx.conf
在使用 -name 操作选项时需要注意,find 命令是基于名称完全匹配的,即文件名称必须和搜索关键字一模一样才会作为结果展示出来。
此时可以使用正则中的 *
来匹配所有,这样就可以使用部分名称查询文件。
# 名称没有完全匹配,则不会展示
[root@VM-0-5-centos /]# find / -name nginx.con
[root@VM-0-5-centos /]#
# 使用 * 代表匹配任意字符
[root@VM-0-5-centos /]# find -name nginx.co*
./etc/nginx/nginx.conf
[root@VM-0-5-centos /]# find -name *inx.conf
./etc/nginx/nginx.conf
[root@VM-0-5-centos /]# find -name *inx.co*
./etc/nginx/nginx.conf
2.2 按照文件大小搜索
如果想要按照文件的大小来查找文件时,可以使用 -size 作为操作选项,-size 的使用如下:
-
-size [+-]数值
,按照指定数值大小来搜索文件-
+
,代表搜索比指定数值更大的文件 -
-
,代表搜索比指定数值更小的文件
-
find 使用 -size 选项时,后跟的数值可以指定大小单位
-
如果不指定,或者指定为 b,则单位按照 512Byte 查询,即
数值*512Byte
-
c,按照字节搜索,即 1B
-
w,按照双字节大小搜索,即 2B
-
k,按照 KB 单位搜索
-
M,按照 MB 单位搜索
-
G,按照 GB 单位搜索
[root@VM-0-5-centos schedules]# ll -h
total 52K
-rw-r--r-- 1 root root 37K Jun 19 08:53 log.out
-rw-r--r-- 1 root root 4.3K Jun 14 23:36 select_seat_every_day.py
# 搜索小于 50k 的文件
[root@VM-0-5-centos schedules]# find . -size -50k
.
./select_day.py
./log.out
# 搜索大于 10 * 521B = 5.21k 的文件
[root@VM-0-5-centos schedules]# find . -size -10
.
./select_seat_every_day.py
2.3 按照时间搜索
Linux 中的文件有访问时间(atime)、数据修改时间(mtime)、状态修改时间(ctime)这三个时间,我们也可以按照时间来搜索文件。
-
-atime [+-]时间,按照文件访问时间搜索
-
-mtime [+-]时间,按照文件时间搜索
-
-ctime [+-]时间,按照文件修改时间搜索
-
-5:代表搜索 5 天内修改的文件
-
5:代表搜索前第 5 天当天修改的文件
-
+5:代表搜索 6 天前修改的文件
-
# 当前文件夹文件信息
[root@VM-0-5-centos schedules]# ll -lh
total 52K
-rw-r--r-- 1 root root 37K Jun 19 08:53 log.out
-rw-r--r-- 1 root root 4.3K Jun 14 23:36 select_seat_every_day.py
# 搜索 5 天前的文件
[root@VM-0-5-centos schedules]# find . -mtime +5
.
./select_day.py
./log.out
# 搜索 10 天内修改的文件
[root@VM-0-5-centos schedules]# find . -ctime -10
./log.out
2.4 按照类型搜索
在一些特殊情况下,需要按照文件类型查询文件信息,如将普通文件和目录文件区分开,就可以指定文件类型来查询文件信息
-
-type d:查找目录文件
-
-type f:查找普通文件
-
-type l:查找软链接文件
# 搜索当前目录下的目录文件
[root@VM-0-5-centos schedules]# find -type d
.
# 搜索当前目录下的普通文件
[root@VM-0-5-centos schedules]# find -type f
./select_day.py
./log.out
3. find 配合其他命令使用
根据 find 命令语法 find path -option [-print] [-exec -ok command] {}
可以看到 find 命令在使用时,可以与其他命令配合使用,如 ls 查看文件信息命令和 sort 文件排序命令等。
3.1 find & ls
find & ls 两种命令同时作用于搜索到的文件,可以实现在搜索文件的同时定义展示文件的信息
# 查询普通文件类型,并展示文件的详细信息
[root@VM-0-5-centos schedules]# find . -type f | ls -lh
total 52K
-rw-r--r-- 1 root root 37K Jun 19 08:53 log.out
-rw-r--r-- 1 root root 4.3K Jun 14 23:36 selecty_day.py
3.2 find & ls & sort
find 命令中可以同时使用多种其他命令,如 find & ls & sort 实现搜索文件并展示自定义信息,以及对符合条件的文件列表进行自定义排序
# 查询当前目录下普通文件信息,展示完整信息,并根据展示信息的第 5 列 [大小] 正序排列
[root@VM-0-5-centos schedules]# find . -type f | ls -lh | sort -k5,5 -h
total 52K
-rw-r--r-- 1 root root 4.3K Jun 14 23:36 select_day.py
-rw-r--r-- 1 root root 37K Jun 19 08:53 log.out
除此之外,find 还可以配合其他的命令如:
-
-delete
:删除查找到的文件 -
-exec
:查找到的文件传递给任何 Linux 命令 -
-ok
:与 exec 功能相同,但是需要用户确认每次的操作