find命令使用

本文详细介绍了Linux中find命令的使用方法,包括按名称、类型、权限、大小、访问时间、创建时间和更改时间查找文件和目录。通过示例展示了如何查找特定条件的文件,如空文件、特定大小的文件以及按用户或组查找。还讲解了如何结合使用逻辑操作符(-and, -or)以及-exec和-ok选项来执行命令。这是一篇全面的find命令教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

find命令的应用方法

使用范围

查找符合条件的文件或者目录,默认查找指定路径所有文件或者目录

语法

find path options argument

选项使用

-name

按名称查找,或按名称中的关键字

[root@haha ~]# find . -name haha
./haha
[root@haha ~]# find . -name "*.txt"
./haha.txt
./hehe.txt

-type

按类型查找

[root@haha ~]# find . -name "ha*"
./haha
./haha.txt
[root@haha ~]# find . -name "ha*" -type d
./haha
[root@haha ~]# find . -name "ha*" -type f
./haha.txt

-perm

按权限查找

[root@haha ~]# find . -perm 644
./haha.txt
./hehe.txt

-size

按大小查找
-小于
+大于
可以同时使用,查找一个大小范围

[root@haha ~]# find . -size -1c
./haha.txt
./hehe.txt
[root@haha ~]# find . -size +1c -size -100c
./haha

-atime

按访问时间进行查找,单位天
-n 小于
+n 大于
n 无前缀1天~n天

[root@haha ~]# find . -atime -1
./haha
./haha.txt
./hehe.txt
[root@haha ~]# find . -atime +1
./fun.sh
./mysql-install.sh
[root@haha ~]# find . -atime +1 -atime -20
./fun.sh
[root@haha ~]# find . -atime 30
./mysql-install.sh

-ctime

按创建时间进行查找,单位天
-n 小于
+n 大于
n 无前缀1天~n天

[root@haha ~]# find . -ctime -1
./haha
./haha.txt
./hehe.txt
[root@haha ~]# find . -ctime +1
./fun.sh
./mysql-install.sh
[root@haha ~]# find . -ctime +1 -ctime -20
./fun.sh
[root@haha ~]# find . -ctime 30
./mysql-install.sh

-mtime

按更改时间进行查找,单位天
-n 小于
+n 大于
n 无前缀1天~n天

[root@haha ~]# find . -mtime -1
./haha
./haha.txt
./hehe.txt
[root@haha ~]# find . -mtime +1
./fun.sh
./mysql-install.sh
[root@haha ~]# find . -mtime +1 -mtime -20
./fun.sh
[root@haha ~]# find . -mtime 30
./mysql-install.sh

-cmin

按创建时间进行查找,单位分钟
-n 小于
+n 大于
n 无前缀1分钟~n分钟

[root@haha ~]# find . -cmin -40
.
./.mysql_history
./haha
./haha.txt
./hehe.txt
[root@haha ~]# find . -cmin +60
./fun.sh
./mysql-install.sh
[root@haha ~]# find . -cmin 35
./haha
./haha.txt
./hehe.txt
[root@haha ~]# find . -cmin +60 -cmin -80
./haha.sql

-amin

按访问时间进行查找,单位分钟
-n 小于
+n 大于
n 无前缀1分钟~n分钟

[root@haha ~]# find . -amin -40
.
./.mysql_history
./haha
./haha.txt
./hehe.txt
[root@haha ~]# find . -amin +60
./fun.sh
./mysql-install.sh
[root@haha ~]# find . -amin 35
./haha
./haha.txt
./hehe.txt
[root@haha ~]# find . -amin +60 -amin -80
./haha.sql

-mmin

按更改时间进行查找,单位分钟
-n 小于
+n 大于
n 无前缀1分钟~n分钟

[root@haha ~]# find . -mmin -40
.
./.mysql_history
./haha
./haha.txt
./hehe.txt
[root@haha ~]# find . -mmin +60
./fun.sh
./mysql-install.sh
[root@haha ~]# find . -mmin 35
./haha
./haha.txt
./hehe.txt
[root@haha ~]# find . -mmin +60 -mmin -80
./haha.sql

-user

按所属用户进行查找

[root@haha ~]# find  /home -user haha
/home/haha/sudo.log

-group

按所属组进行查找

[root@haha ~]# find /home -group hehe
/home/hehe/username.txt

-empty

查找空文件

[root@haha ~]# find . -empty
./haha

-maxdepth

[root@haha ~]# find ./test -maxdepth 2 -name  "*.php"
./test/heihei.php
./test/hehe/test1.php          ##只查询到第二层目录

-mindepth

[root@haha ~]# find ./test -maxdepth 1 -name  "*.php"
./test/hehe/test1.php         
./test/hehe/haha/xixi.php           ##到第二层才开始查

!取反

等同于 -not

[root@haha ~]#  find ./test ! -name "*.php" -type f
./test/haha.log
./test/hehe/test.txt

and & or

[root@haha ~]# find ./test -name "ha*" -a -type f
./test/haha.log
[root@haha ~]# find test/ -name "ha*" -o -name "he*" -type f
test/hehe/haha
test/heihei.php
test/haha.log

exec & ok

exec 执行命令 {} 引用find的结果 ;结束符号
ok与exec功能相同,只不过是会询问

[root@haha ~]# find test/ -empty -ok rm -r {} \;
< rm ... test/heihei.php > ? n
< rm ... test/haha.log > ? n
[root@haha ~]# find test/ -empty -exec rm -r {} \;
[root@haha ~]# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值