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 ~]#