[toc]
find命令
命令说明
说明
find命令用来在指定目录下查找文件
任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示
命令格式
find 查找范围 选项 操作
查找范围:默认当前目录
操作:默认输出到终端
命令选项
-name 按照文件名查找
$ pwd
/root
$ find -name *.log
./install.log
-size 按照文件大小查找
符号
| 符号 | 说明 |
|---|---|
+ | 大于 |
- | 小于 |
无 | 不加符号是等于 |
文件大小单元
| 字符 | 说明 |
|---|---|
b | 块(512字节) |
c | 字节 |
w | 字(2字节) |
k | 千字节 |
M | 兆字节 |
G | G字节 |
查找当前目录大小为110K的文件
$ ll -h
total 144K
-rw-r--r-- 1 root root 11K Aug 9 13:35 1.1
-rw-r--r-- 1 root root 15K Aug 9 13:35 2.2
-rw-r--r-- 1 root root 110K Aug 9 13:35 3.3
-rw-r--r-- 1 root root 580 Aug 9 13:39 ip_date.txt
$ find . -size 110k
./3.3
查找当前目录小于100K的文件
$ ll -h
total 144K
-rw-r--r-- 1 root root 11K Aug 9 13:35 1.1
-rw-r--r-- 1 root root 15K Aug 9 13:35 2.2
-rw-r--r-- 1 root root 110K Aug 9 13:35 3.3
-rw-r--r-- 1 root root 812 Aug 9 13:41 ip_date.txt
$ find . -size -100k
.
./1.1
./ip_date.txt
./2.2
查找当前目录大于100K的文件
$ ll -h
total 144K
-rw-r--r-- 1 root root 11K Aug 9 13:35 1.1
-rw-r--r-- 1 root root 15K Aug 9 13:35 2.2
-rw-r--r-- 1 root root 110K Aug 9 13:35 3.3
-rw-r--r-- 1 root root 928 Aug 9 13:42 ip_date.txt
$ find . -size +100k
./3.3
查找当前目录大于10k小于100k的文件
$ ll -h
total 144K
-rw-r--r-- 1 root root 11K Aug 9 13:35 1.1
-rw-r--r-- 1 root root 15K Aug 9 13:35 2.2
-rw-r--r-- 1 root root 110K Aug 9 13:35 3.3
-rw-r--r-- 1 root root 1.1K Aug 9 13:43 ip_date.txt
$ find . -size +10k -size -100k
./1.1
./2.2
-user 按照文件所有者查找
说明
查找文件的时候会有报错 下面的例子中,5507就是运行find命令时,find命令的PID,只在运行期间出现,等find命令运行完成之后,就会消失,这并不是一个实际错误,解决方法是将错误输出重定向
$ find / -user gun 2>/dev/null
/var/spool/mail/gun
/home/gun
/home/gun/.bash_logout
/home/gun/.bash_profile
/home/gun/.bashrc
查找文件所有者为gun的文件
$ find / -user gun
find: `/proc/5507/task/5507/fd/5': No such file or directory
find: `/proc/5507/task/5507/fdinfo/5': No such file or directory
find: `/proc/5507/fd/5': No such file or directory
find: `/proc/5507/fdinfo/5': No such file or directory
/var/spool/mail/gun
/home/gun
/home/gun/.bash_logout
/home/gun/.bash_profile
/home/gun/.bashrc
-perm 按照文件权限查找
mode 表示精确匹配
查找权限为755的文件或目录
$ ll
total 12
drwxr-xr-x 2 root root 4096 Aug 9 15:22 dir1
drwxrwxrwx 2 root root 4096 Aug 9 15:22 dir2
drwxr-xrw- 2 root root 4096 Aug 9 15:23 dir3
-rw-r--r-- 1 root root 0 Aug 9 15:38 file
$ find . -perm 755
.
./dir1
-mode 表示权限每一位至少匹配
说明
find . -perm -111 表示所有者,所属组,其他人都至少有执行权限
$ ll
total 12
drwxr-xr-x 2 root root 4096 Aug 9 2018 dir1
drwxrwxrwx 2 root root 4096 Aug 9 2018 dir2
drwxr-xrw- 2 root root 4096 Aug 9 2018 dir3
-rwxr--r-- 1 root root 0 Aug 9 2018 file
$ find . -perm -111
.
./dir1
./dir2
+mode 表示权限只要有一位匹配即可
说明
find . -perm +111 表示所有者,所属组,其他人任意一个有执行权限就可以
$ ll
total 12
drwxr-xr-x 2 root root 4096 Aug 9 2018 dir1
drwxrwxrwx 2 root root 4096 Aug 9 2018 dir2
drwxr-xrw- 2 root root 4096 Aug 9 2018 dir3
-rwxr--r-- 1 root root 0 Aug 9 2018 file
$ find . -perm +111
.
./dir3
./dir1
./file
./dir2
-type 按照文件类型查找
文件类型
| 字符 | 说明 |
|---|---|
f | 普通文件 |
d | 目录 |
l | 链接文件 |
c | 字符设备 |
b | 块设备 |
s | 套接字文件 |
p | 管道 |
查找当前目录下的文件
$ ll
total 16
drwxr-xr-x 2 root root 4096 Aug 9 2018 dir1
drwxrwxrwx 2 root root 4096 Aug 9 2018 dir2
drwxr-xrw- 2 root root 4096 Aug 9 2018 dir3
-rwxr--r-- 1 root root 0 Aug 9 2018 file
-rw-r--r-- 1 root root 178 Aug 7 14:19 hehe
$ find . -type f
./hehe
./file
按照时间戳查找
说明
+n表示最近一次修改是在n天之前(常用,用于删除n天前的日志),n为数字-n表示最近一次修改是在n天之内,n为数字
子选项
| 选项 | 说明 |
|---|---|
-atime | access 访问时间,指文件被访问的时间 |
-ctime | change 改变时间,指文件属性被改变 |
-mtime | modify 修改时间,指文件内容被修改 |
查找 /tmp 下文件修改时间是在3天之前的
$ find /tmp/ -mtime +3
/tmp/systemd-private-81d53e50debf4c9db3563ac49d9d3d6a-php-fpm.service-8iRh7w
/tmp/systemd-private-81d53e50debf4c9db3563ac49d9d3d6a-php-fpm.service-8iRh7w/tmp
/tmp/hsperfdata_root
/tmp/.XIM-unix
/tmp/.font-unix
/tmp/supervisord.pid
/tmp/mysql.sock
/tmp/.X11-unix
/tmp/.ICE-unix
/tmp/supervisor.sock
/tmp/systemd-private-81d53e50debf4c9db3563ac49d9d3d6a-ntpd.service-lCUH17
/tmp/systemd-private-81d53e50debf4c9db3563ac49d9d3d6a-ntpd.service-lCUH17/tmp
/tmp/.Test-unix
-name 按照文件名查找
查找 /tmp 中以 .txt 结尾的文件
$ find /tmp -type f -name *.txt
/tmp/test/3.txt
/tmp/test/1.txt
/tmp/test/2.txt
/tmp/test/6/3.txt
/tmp/test/6/1.txt
/tmp/test/6/5.txt
/tmp/test/6/2.txt
/tmp/test/6/4.txt
/tmp/test/6/6.txt
/tmp/ip_date.txt
-regex 基于正则表达式匹配文件
$ ls
1.pdf 1.py 1.txt
$ find . -regex ".*\(\.txt\|\.pdf\)$"
./1.pdf
./1.txt