Linux 日常命令 — find

Linux 日常命令归档

Posted by zhouqian on Wednesday, May 25, 2022

介绍

find 命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。

如果使用该命令时,不设置任何参数,则 find 命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。

常用参数

选项 作用
-name 根据文件名查找,支持通配符
-iname 同 -name,但是忽略文件名称大小写
-type 限定查找目标类型,常用有 f:一般文件 d:目录
-perm 根据权限查找
-empty 查找空文件、空目录
-user 根据文件、目录拥有者查找
-group 根据文件、目录所属组查找
-atime(-amin) 从当前起第 n 天(分钟)时访问的文件
-ctime(-cmin) 从当前起第 n 天(分钟)时创建的文件
-mtime(-mmin) 从当前起第 n 天(分钟)时更新过的文件
-size 根据文件大小筛选结果

示例

根据文件格式查找

root@ad09c5d14f8d:~# find ./helloworld/ -name "*.go"
./helloworld/main.go
root@ad09c5d14f8d:~# find ./helloworld/ -name Main.go
root@ad09c5d14f8d:~# find ./helloworld/ -iname Main.go
./helloworld/main.go
root@ad09c5d14f8d:~# find ./helloworld/ -type f -name "main.go"
./helloworld/main.go

查找目录

root@ad09c5d14f8d:~# find . -type d -name "hello*"
./helloworld
root@ad09c5d14f8d:~# find . -type d -name "helloworld"
./helloworld
root@ad09c5d14f8d:~# find . -type d -name "HelloWorld"

根据权限查找

root@ad09c5d14f8d:~#  find ./helloworld/ -type f -perm 0644
./helloworld/go.mod
./helloworld/go.sum
./helloworld/main.go
root@ad09c5d14f8d:~#  find ./helloworld/ -type f ! -perm 0644
root@ad09c5d14f8d:~#  find ./helloworld/ -type f ! -perm 0755
./helloworld/go.mod
./helloworld/go.sum
./helloworld/main.go
root@ad09c5d14f8d:~# ll ./helloworld/
total 7
-rw-rwSr-- 1 root root    0 May 25 06:59 sgid-file
-rwSr--r-- 1 root root    0 May 25 06:57 suid-file
-rw-r--r-- 1 root root    0 May 25 06:24 main.go
-rw-r--r-- 1 root root  584 May 25 05:59 go.mod
-rw-r--r-- 1 root root 2956 May 25 05:59 go.sum
root@ad09c5d14f8d:~# find ./helloworld/ -readable
./helloworld/
./helloworld/sgid-file
./helloworld/suid-file
./helloworld/go.mod
./helloworld/go.sum
./helloworld/main.go
root@ad09c5d14f8d:~# find ./helloworld/  -executable
./helloworld/
root@ad09c5d14f8d:~# find ./helloworld/  -writable
./helloworld/
./helloworld/sgid-file
./helloworld/suid-file
./helloworld/go.mod
./helloworld/go.sum
./helloworld/main.go

查找文件并删除

root@ad09c5d14f8d:~# ll ./helloworld/
total 8
-rw-r--r-- 1 root root    0 May 25 07:27 test
-rw-r--r-- 1 root root    0 May 25 06:24 main.go
-rw-r--r-- 1 root root  584 May 25 05:59 go.mod
-rw-r--r-- 1 root root 2956 May 25 05:59 go.sum
root@ad09c5d14f8d:~# find ./helloworld/ -type f -name "test" -exec rm -f {} \;
root@ad09c5d14f8d:~# ll ./helloworld/
total 8
-rw-r--r-- 1 root root    0 May 25 06:24 main.go
-rw-r--r-- 1 root root  584 May 25 05:59 go.mod
-rw-r--r-- 1 root root 2956 May 25 05:59 go.sum

查找文件并更改权限

root@ad09c5d14f8d:~# ll ./helloworld/
total 8
-rw-r--r-- 1 root root    0 May 25 06:24 main.go
-rw-r--r-- 1 root root  584 May 25 05:59 go.mod
-rw-r--r-- 1 root root 2956 May 25 05:59 go.sum
root@ad09c5d14f8d:~# find ./helloworld/ -type f -perm 0644 -exec chmod 755 {} \;
root@ad09c5d14f8d:~# ll ./helloworld/
total 8
-rwxr-xr-x 1 root root    0 May 25 06:24 main.go
-rwxr-xr-x 1 root root  584 May 25 05:59 go.mod
-rwxr-xr-x 1 root root 2956 May 25 05:59 go.sum

查找空文件、空目录

root@ad09c5d14f8d:~# ll ./helloworld/
total 12
drwxr-xr-x 2 root root 4096 May 25 07:32 test
-rwxr-xr-x 1 root root    0 May 25 06:24 main.go
-rwxr-xr-x 1 root root  584 May 25 05:59 go.mod
-rwxr-xr-x 1 root root 2956 May 25 05:59 go.sum
root@ad09c5d14f8d:~# ll ./helloworld/test/
total 0
root@ad09c5d14f8d:~# find ./helloworld/ -type f -name "*.go" -empty
./helloworld/main.go
root@ad09c5d14f8d:~# find ./helloworld/ -type d -empty
./helloworld/test

查找隐藏文件

root@ad09c5d14f8d:~# ll -a
total 24
drwxr-xr-t 3 root root 4096 May 25 07:32 helloworld
drwx------ 1 root root 4096 May 25 07:05 .
drwxr-xr-x 3 root root 4096 May 25 06:02 .cache
drwxr-xr-x 1 root root 4096 May 25 05:57 ..
-rw-r--r-- 1 root root  571 Apr 10  2021 .bashrc
-rw-r--r-- 1 root root  161 Jul  9  2019 .profile
root@ad09c5d14f8d:~# find . -type f -name ".*"
./.profile
./.bashrc

根据文件拥有者查找

root@ad09c5d14f8d:~# ll ./helloworld/
total 12
drwxr-xr-x 2 root root 4096 May 25 07:32 test
-rwxr-xr-x 1 root root    0 May 25 06:24 main.go
-rwxr-xr-x 1 root root  584 May 25 05:59 go.mod
-rwxr-xr-x 1 root root 2956 May 25 05:59 go.sum
root@ad09c5d14f8d:~# find ./helloworld/ -type f -user "hello"
root@ad09c5d14f8d:~# find ./helloworld/ -type f -user "root"
./helloworld/go.mod
./helloworld/go.sum
./helloworld/main.go

根据文件拥有组查找

root@ad09c5d14f8d:~# ll ./helloworld/
total 12
drwxr-xr-x 2 root root 4096 May 25 07:32 test
-rwxr-xr-x 1 root root    0 May 25 06:24 main.go
-rwxr-xr-x 1 root root  584 May 25 05:59 go.mod
-rwxr-xr-x 1 root root 2956 May 25 05:59 go.sum
root@ad09c5d14f8d:~# find ./helloworld/ -type f -group "hello"
root@ad09c5d14f8d:~# find ./helloworld/ -type f -group "root"
./helloworld/go.mod
./helloworld/go.sum
./helloworld/main.go

根据时间属性查找

root@ad09c5d14f8d:~# ll ./helloworld/
total 12
drwxr-xr-x 2 root root 4096 May 25 07:32 test
-rwxr-xr-x 1 root root    0 May 25 06:24 main.go
-rwxr-xr-x 1 root root  584 May 25 05:59 go.mod
-rwxr-xr-x 1 root root 2956 May 25 05:59 go.sum
root@ad09c5d14f8d:~# find ./helloworld/ -mtime 0
./helloworld/
./helloworld/go.mod
./helloworld/test
./helloworld/go.sum
./helloworld/main.go
root@ad09c5d14f8d:~# find ./helloworld/ -mtime 10
root@ad09c5d14f8d:~# find ./helloworld/ -ctime 0
./helloworld/
./helloworld/go.mod
./helloworld/test
./helloworld/go.sum
./helloworld/main.go
root@ad09c5d14f8d:~# find ./helloworld/ -ctime 10
root@ad09c5d14f8d:~# ll ./helloworld/
total 12
drwxr-xr-x 2 root root 4096 May 25 07:32 test
-rwxr-xr-x 1 root root    0 May 25 06:24 main.go
-rwxr-xr-x 1 root root  584 May 25 05:59 go.mod
-rwxr-xr-x 1 root root 2956 May 25 05:59 go.sum
root@ad09c5d14f8d:~# find ./helloworld/ -atime 0
./helloworld/
./helloworld/go.mod
./helloworld/test
./helloworld/go.sum
./helloworld/main.go
root@ad09c5d14f8d:~# find ./helloworld/ -mtime 0
./helloworld/
./helloworld/go.mod
./helloworld/test
./helloworld/go.sum
./helloworld/main.go
root@ad09c5d14f8d:~# find ./helloworld/ -ctime 0
./helloworld/
./helloworld/go.mod
./helloworld/test
./helloworld/go.sum
./helloworld/main.go
root@ad09c5d14f8d:~# find ./helloworld/ -atime 1
root@ad09c5d14f8d:~# find ./helloworld/ -mtime 1
root@ad09c5d14f8d:~# find ./helloworld/ -ctime 1
root@ad09c5d14f8d:~# ll ./helloworld/
total 12
drwxr-xr-x 2 root root 4096 May 25 07:32 test
-rwxr-xr-x 1 root root    0 May 25 06:24 main.go
-rwxr-xr-x 1 root root  584 May 25 05:59 go.mod
-rwxr-xr-x 1 root root 2956 May 25 05:59 go.sum
root@ad09c5d14f8d:~# find ./helloworld/ -mtime +50 -mtime -100
root@ad09c5d14f8d:~# ll ./helloworld/
total 12
drwxr-xr-x 2 root root 4096 May 25 07:32 test
-rwxr-xr-x 1 root root    0 May 25 06:24 main.go
-rwxr-xr-x 1 root root  584 May 25 05:59 go.mod
-rwxr-xr-x 1 root root 2956 May 25 05:59 go.sum
root@ad09c5d14f8d:~# find ./helloworld/ -amin -120
./helloworld/
./helloworld/test
root@ad09c5d14f8d:~# date
Wed May 25 09:10:57 UTC 2022
root@ad09c5d14f8d:~#

根据文件大小查找

root@ad09c5d14f8d:~# ll -h ./helloworld/
total 12K
drwxr-xr-x 2 root root 4.0K May 25 07:32 test
-rwxr-xr-x 1 root root    0 May 25 06:24 main.go
-rwxr-xr-x 1 root root  584 May 25 05:59 go.mod
-rwxr-xr-x 1 root root 2.9K May 25 05:59 go.sum
root@ad09c5d14f8d:~# find ./helloworld/ -size +2k -size -5k
./helloworld/
./helloworld/test
./helloworld/go.sum
root@2906e5a6917b:~# ll -h ./helloworld/
total 4.0K
-rw-r--r-- 1 root root   27 May 25 09:23 go.mod
-rw-r--r-- 1 root root    0 May 25 09:23 main.go
-rw-r--r-- 1 root root 100M May 25 09:26 test
root@2906e5a6917b:~# find -type f -size +10M -exec rm {} \;
root@2906e5a6917b:~# ll -h ./helloworld/
total 4.0K
-rw-r--r-- 1 root root 27 May 25 09:23 go.mod
-rw-r--r-- 1 root root  0 May 25 09:23 main.go