1、linux系统中find命令主要用于查找文件,可以设置不同的查找条件进行查找
创建测试数据:
[root@linuxprobe test]# touch a.txt b.txt c.txt ## 创建三个普通文件
[root@linuxprobe test]# mkdir test1 test2 test3 ## 创建三个目录
[root@linuxprobe test]# ll -h
total 0
-rw-r--r--. 1 root root 0 Oct 16 22:42 a.txt
-rw-r--r--. 1 root root 0 Oct 16 22:42 b.txt
-rw-r--r--. 1 root root 0 Oct 16 22:42 c.txt
drwxr-xr-x. 2 root root 6 Oct 16 22:42 test1
drwxr-xr-x. 2 root root 6 Oct 16 22:42 test2
drwxr-xr-x. 2 root root 6 Oct 16 22:42 test3
2、基本用法,默认查找当前路径内的文件及目录
[root@linuxprobe test]# find ##直接输入find,默认查找当前目录
.
./a.txt
./b.txt
./c.txt
./test1
./test2
./test3
[root@linuxprobe test]# find * ## 注意和直接输入find的区别
a.txt
b.txt
c.txt
test1
test2
test3
3、指定查找的路径
[root@linuxprobe test]# mv b.txt test3 ## 将b.txt移动至 test3
[root@linuxprobe test]# find ./test3 ## 指定查找的路径
./test3
./test3/b.txt
4、按照文件或目录名称进行查找
[root@linuxprobe test]# rm -rf * ## 清空当前目录
[root@linuxprobe test]# touch {a..c}.txt ## 创建测试文件
[root@linuxprobe test]# seq -f test%g 3 | xargs mkdir ## 创建测试文件
[root@linuxprobe test]# ll -h
total 0
-rw-r--r--. 1 root root 0 Oct 16 22:49 a.txt
-rw-r--r--. 1 root root 0 Oct 16 22:49 b.txt
-rw-r--r--. 1 root root 0 Oct 16 22:49 c.txt
drwxr-xr-x. 2 root root 6 Oct 16 22:49 test1
drwxr-xr-x. 2 root root 6 Oct 16 22:49 test2
drwxr-xr-x. 2 root root 6 Oct 16 22:49 test3
[root@linuxprobe test]# find ./ -name "b.txt" ## ./表示当前路径, -name指定查找的名称
./b.txt
[root@linuxprobe test]# find ./ -name "test3" ## 同上
./test3
[root@linuxprobe test]# find ./ -name "test*" ## 使用通配符
./test1
./test2
./test3
[root@linuxprobe test]# find ./ -name "*.txt" ## 使用通配符
./a.txt
./b.txt
./c.txt
[root@linuxprobe test]# find ./ -name *.txt ## 使用通配符不叫引号将报错
find: paths must precede expression: b.txt
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@linuxprobe test]# find ./ -name test* ##同上
find: paths must precede expression: test2
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
默认情况下回递归查找:
[root@linuxprobe test]# mv b.txt test3
[root@linuxprobe test]# ls
a.txt c.txt test1 test2 test3
[root@linuxprobe test]# find ./ -name "*.txt" ## 当前路径下的所有目录中的txt文件都能被查找到
./a.txt
./c.txt
./test3/b.txt
5、查找文件名时忽略大小写
[root@linuxprobe test]# rm -rf * ##清空当前目录
[root@linuxprobe test]# touch abc.txt ABC.TXT AbC.Txt abc.txt bbb.txt CCC.txt ##重新创建测试文件
[root@linuxprobe test]# ls
abc.txt AbC.Txt ABC.TXT bbb.txt CCC.txt
[root@linuxprobe test]# find ./ -name "abc.txt" ##指定名称查找
./abc.txt
[root@linuxprobe test]# find ./ -iname "abc.txt" ##忽略大小写
./abc.txt
./ABC.TXT
./AbC.Txt
[root@linuxprobe test]# find ./ -iname "ABC.txt" ##忽略大小写
./abc.txt
./ABC.TXT
./AbC.Txt
6、指定查找的深度
[root@linuxprobe test]# rm -rf * ## 清空当前目录
[root@linuxprobe test]# mkdir -p dep1/dep2/dep3/dep4 ## 创建测试目录
[root@linuxprobe test]# touch dep1/dep2/dep3/dep4/{xxx.txt,yyy.txt} ## 创建测试文件爱你
[root@linuxprobe test]# touch dep1/dep2/dep3/{mmm.txt,nnn.txt} ## 同上
[root@linuxprobe test]# touch dep1/dep2/{ooo.txt,ppp.txt} ## 同上
[root@linuxprobe test]# touch dep1/{rrr.txt,ttt.txt} ## 同上
[root@linuxprobe test]# touch a.txt b.txt ##同上
[root@linuxprobe test]# tree ## 查看测试数据结构
.
├── a.txt
├── b.txt
└── dep1
├── dep2
│ ├── dep3
│ │ ├── dep4
│ │ │ ├── xxx.txt
│ │ │ └── yyy.txt
│ │ ├── mmm.txt
│ │ └── nnn.txt
│ ├── ooo.txt
│ └── ppp.txt
├── rrr.txt
└── ttt.txt
4 directories, 10 files
测试:
[root@linuxprobe test]# find ./ -name "*.txt" ## 列出当前目录所有的txt文件
./dep1/dep2/dep3/dep4/xxx.txt
./dep1/dep2/dep3/dep4/yyy.txt
./dep1/dep2/dep3/mmm.txt
./dep1/dep2/dep3/nnn.txt
./dep1/dep2/ooo.txt
./dep1/dep2/ppp.txt
./dep1/rrr.txt
./dep1/ttt.txt
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -maxdepth 1 -name "*.txt" ##最大深度为1
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -maxdepth 2 -name "*.txt" ##最大深度为2
./dep1/rrr.txt
./dep1/ttt.txt
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -maxdepth 3 -name "*.txt" ##最大深度为3
./dep1/dep2/ooo.txt
./dep1/dep2/ppp.txt
./dep1/rrr.txt
./dep1/ttt.txt
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -maxdepth 4 -name "*.txt" ##最大深度为4
./dep1/dep2/dep3/mmm.txt
./dep1/dep2/dep3/nnn.txt
./dep1/dep2/ooo.txt
./dep1/dep2/ppp.txt
./dep1/rrr.txt
./dep1/ttt.txt
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -mindepth 1 -name "*.txt" ##最低深度为1
./dep1/dep2/dep3/dep4/xxx.txt
./dep1/dep2/dep3/dep4/yyy.txt
./dep1/dep2/dep3/mmm.txt
./dep1/dep2/dep3/nnn.txt
./dep1/dep2/ooo.txt
./dep1/dep2/ppp.txt
./dep1/rrr.txt
./dep1/ttt.txt
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -mindepth 2 -name "*.txt" ## 最低深度为2
./dep1/dep2/dep3/dep4/xxx.txt
./dep1/dep2/dep3/dep4/yyy.txt
./dep1/dep2/dep3/mmm.txt
./dep1/dep2/dep3/nnn.txt
./dep1/dep2/ooo.txt
./dep1/dep2/ppp.txt
./dep1/rrr.txt
./dep1/ttt.txt
[root@linuxprobe test]# find ./ -mindepth 3 -name "*.txt" ## 最低深度为3
./dep1/dep2/dep3/dep4/xxx.txt
./dep1/dep2/dep3/dep4/yyy.txt
./dep1/dep2/dep3/mmm.txt
./dep1/dep2/dep3/nnn.txt
./dep1/dep2/ooo.txt
./dep1/dep2/ppp.txt
[root@linuxprobe test]# find ./ -mindepth 4 -name "*.txt" ## 最低深度为4
./dep1/dep2/dep3/dep4/xxx.txt
./dep1/dep2/dep3/dep4/yyy.txt
./dep1/dep2/dep3/mmm.txt
./dep1/dep2/dep3/nnn.txt
[root@linuxprobe test]# find ./ -mindepth 5 -name "*.txt" ## 最低深度为5
./dep1/dep2/dep3/dep4/xxx.txt
./dep1/dep2/dep3/dep4/yyy.txt
[root@linuxprobe test]# find ./ -mindepth 6 -name "*.txt" ## 最低深度为6
[root@linuxprobe test]# find ./ -mindepth 2 -maxdepth 4 -name "*.txt" ##最低深度为2,同时最大深度为4
./dep1/dep2/dep3/mmm.txt
./dep1/dep2/dep3/nnn.txt
./dep1/dep2/ooo.txt
./dep1/dep2/ppp.txt
./dep1/rrr.txt
./dep1/ttt.txt
[root@linuxprobe test]# find ./ -mindepth 2 -maxdepth 3 -name "*.txt" ## 最低深度为2,同时最大深度为3
./dep1/dep2/ooo.txt
./dep1/dep2/ppp.txt
./dep1/rrr.txt
./dep1/ttt.txt
7、
原文:https://www.cnblogs.com/liujiaxin2018/p/13829396.html