linux系统中find命令

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、-not 或者 !反向查找

[root@linuxprobe test]# touch a.txt b.txt c.csv d.csv e.pdf f.pdf  
[root@linuxprobe test]# ls
a.txt  b.txt  c.csv  d.csv  e.pdf  f.pdf
[root@linuxprobe test]# find ./ -not -name "a.txt"  ## 排除a.txt	
./
./b.txt
./c.csv
./d.csv
./e.pdf
./f.pdf
[root@linuxprobe test]# find ./ -not -name "c.csv"
./
./a.txt
./b.txt
./d.csv
./e.pdf
./f.pdf
[root@linuxprobe test]# find ./ -not -name "*.txt"  ## 使用通配符
./
./c.csv
./d.csv
./e.pdf
./f.pdf

可以使用!代替 -not

[root@linuxprobe test]# find ./ ! -name "a.txt"
./
./b.txt
./c.csv
./d.csv
./e.pdf
./f.pdf
[root@linuxprobe test]# find ./ ! -name "c.csv"
./
./a.txt
./b.txt
./d.csv
./e.pdf
./f.pdf
[root@linuxprobe test]# find ./ ! -name "*.txt"
./
./c.csv
./d.csv
./e.pdf
./f.pdf

8、同时结合多个条件进行查找

[root@linuxprobe test]# ls
a.txt  b.txt  c.csv  d.csv  e.pdf  f.pdf
[root@linuxprobe test]# find ./ -name "*.txt"
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -name "*.txt" -not -name "a.txt"
./b.txt
[root@linuxprobe test]# find ./ -name "*.txt" ! -name "b.txt"
./a.txt

9、OR操作符

当使用多个条件进行查找时,find模式在多个跳进同时满足时才会返回结果,使用-o参数可以达到“或者”的效果

[root@linuxprobe test]# find ./ -name "*.txt" -name "a*"  ## find默认的是"并且”
./a.txt
[root@linuxprobe test]# find ./ -name "*.txt" -name "*.csv"
[root@linuxprobe test]# find ./ -name "*.txt" -o -name "*.csv" ## 加-o实现“或者”
./a.txt
./b.txt
./c.csv
./d.csv

10、指定文件类型进行查找

[root@linuxprobe test]# touch a.txt  b.txt  c.csv  d.csv
[root@linuxprobe test]# rm -rf test*
[root@linuxprobe test]# touch a.txt  b.txt  c.csv  d.csv
[root@linuxprobe test]# mkdir test{1..3}
[root@linuxprobe test]# ln -s a.txt a.link
[root@linuxprobe test]# ln -s b.txt b.link
[root@linuxprobe test]# ll
total 0
lrwxrwxrwx. 1 root root 5 Oct 18 21:57 a.link -> a.txt
-rw-r--r--. 1 root root 0 Oct 18 21:57 a.txt
lrwxrwxrwx. 1 root root 5 Oct 18 21:57 b.link -> b.txt
-rw-r--r--. 1 root root 0 Oct 18 21:57 b.txt
-rw-r--r--. 1 root root 0 Oct 18 21:57 c.csv
-rw-r--r--. 1 root root 0 Oct 18 21:57 d.csv
drwxr-xr-x. 2 root root 6 Oct 18 21:57 test1
drwxr-xr-x. 2 root root 6 Oct 18 21:57 test2
drwxr-xr-x. 2 root root 6 Oct 18 21:57 test3
[root@linuxprobe test]# find ./ -type f  ## 只查找文件
./a.txt
./b.txt
./c.csv
./d.csv
[root@linuxprobe test]# find ./ -type d  ## 只查找目录
./
./test1
./test2
./test3
[root@linuxprobe test]# find ./ -type l  ## 只查找软链接
./a.link
./b.link

11、同时指定多个目录进行查找

[root@linuxprobe test]# mkdir test{1..3}
[root@linuxprobe test]# touch a.txt b.txt c.csv d.csv
[root@linuxprobe test]# ls
a.txt  b.txt  c.csv  d.csv  test1  test2  test3
[root@linuxprobe test]# for i in test*;do cp a.txt  b.txt  c.csv  d.csv $i;done
[root@linuxprobe test]# rm a.txt  b.txt  c.csv  d.csv
[root@linuxprobe test]# tree  ## 查看测试数据
.
├── test1
│   ├── a.txt
│   ├── b.txt
│   ├── c.csv
│   └── d.csv
├── test2
│   ├── a.txt
│   ├── b.txt
│   ├── c.csv
│   └── d.csv
└── test3
    ├── a.txt
    ├── b.txt
    ├── c.csv
    └── d.csv

3 directories, 12 files
[root@linuxprobe test]# find ./ -name "*.txt"  
./test1/a.txt
./test1/b.txt
./test2/a.txt
./test2/b.txt
./test3/a.txt
./test3/b.txt
[root@linuxprobe test]# find ./test1 ./test2 -name "*.txt"   ## 指定两个目录进行查找
./test1/a.txt
./test1/b.txt
./test2/a.txt
./test2/b.txt

12、根据文件或者目录权限进行查找

[root@linuxprobe test]# touch a.txt b.txt c.csv d.csv
[root@linuxprobe test]# mkdir test{1..3}
[root@linuxprobe test]# ls
a.txt  b.txt  c.csv  d.csv  test1  test2  test3
[root@linuxprobe test]# ll -h
total 0
-rw-r--r--. 1 root root 0 Oct 18 22:11 a.txt
-rw-r--r--. 1 root root 0 Oct 18 22:11 b.txt
-rw-r--r--. 1 root root 0 Oct 18 22:11 c.csv
-rw-r--r--. 1 root root 0 Oct 18 22:11 d.csv
drwxr-xr-x. 2 root root 6 Oct 18 22:11 test1
drwxr-xr-x. 2 root root 6 Oct 18 22:11 test2
drwxr-xr-x. 2 root root 6 Oct 18 22:11 test3
[root@linuxprobe test]# find ./ -perm 644  ## r:4  w:2  x:1   644代表所有者具有读写、所属组拥有读、其他人具有读的权限
./b.txt
./c.csv
./d.csv
[root@linuxprobe test]# find ./ -perm 755  ## 同上
./
./test1
./test2
./test3
[root@linuxprobe test]# chmod 755 a.txt  ## 赋予a.txt 755权限
[root@linuxprobe test]# find ./ -perm 755
./
./a.txt
./test1
./test2
./test3
[root@linuxprobe test]# chmod 777 b.txt  ##同上
[root@linuxprobe test]# find ./ -perm 777
./b.txt
[root@linuxprobe test]# ll -h
total 0
-rwxr-xr-x. 1 root root 0 Oct 18 22:11 a.txt
-rwxrwxrwx. 1 root root 0 Oct 18 22:11 b.txt
-rw-r--r--. 1 root root 0 Oct 18 22:11 c.csv
-rw-r--r--. 1 root root 0 Oct 18 22:11 d.csv
drwxr-xr-x. 2 root root 6 Oct 18 22:11 test1
drwxr-xr-x. 2 root root 6 Oct 18 22:11 test2
drwxr-xr-x. 2 root root 6 Oct 18 22:11 test3
[root@linuxprobe test]# find ./ -perm /o=x  ## 查找所有者用于执行权限的文件
./
./a.txt
./b.txt
./test1
./test2
./test3
[root@linuxprobe test]# find ./ -perm /g=w  ## 所属组具有写的权限的文件
./b.txt
[root@linuxprobe test]# find ./ -perm /u=x  ## 所有者具有执行权限的文件
./
./a.txt
./b.txt
./test1
./test2
./test3
[root@linuxprobe test]# chmod u=r a.txt b.txt  ##给所有者重新分配权限
[root@linuxprobe test]# ll -h
total 0
-r--r-xr-x. 1 root root 0 Oct 18 22:11 a.txt
-r--rwxrwx. 1 root root 0 Oct 18 22:11 b.txt
-rw-r--r--. 1 root root 0 Oct 18 22:11 c.csv
-rw-r--r--. 1 root root 0 Oct 18 22:11 d.csv
drwxr-xr-x. 2 root root 6 Oct 18 22:11 test1
drwxr-xr-x. 2 root root 6 Oct 18 22:11 test2
drwxr-xr-x. 2 root root 6 Oct 18 22:11 test3
[root@linuxprobe test]# find ./ -perm /u=x  ##同上
./
./test1
./test2
./test3
[root@linuxprobe test]# find ./ -perm /u=w ##同上
./
./c.csv
./d.csv
./test1
./test2
./test3

13、基于所属用户进行查找

[root@linuxprobe test]# ll -h
total 0
-rw-r--r--. 1 root       root       0 Oct 18 22:26 a.txt
-rw-r--r--. 1 root       root       0 Oct 18 22:26 b.txt
-rw-rw-r--. 1 linuxprobe linuxprobe 0 Oct 18 22:26 c.csv
-rw-rw-r--. 1 linuxprobe linuxprobe 0 Oct 18 22:26 d.csv
[root@linuxprobe test]# find ./ -user root
./
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -user linuxprobe
./c.csv
./d.csv
[root@linuxprobe test]# chown linuxprobe a.txt
[root@linuxprobe test]# ll -h
total 0
-rw-r--r--. 1 linuxprobe root       0 Oct 18 22:26 a.txt
-rw-r--r--. 1 root       root       0 Oct 18 22:26 b.txt
-rw-rw-r--. 1 linuxprobe linuxprobe 0 Oct 18 22:26 c.csv
-rw-rw-r--. 1 linuxprobe linuxprobe 0 Oct 18 22:26 d.csv
[root@linuxprobe test]# find ./ -user root
./
./b.txt
[root@linuxprobe test]# find ./ -user linuxprobe
./a.txt
./c.csv
./d.csv

14、基于所属组进行查找

[root@linuxprobe test]# ll -h
total 0
-rw-r--r--. 1 linuxprobe root       0 Oct 18 22:26 a.txt
-rw-r--r--. 1 root       root       0 Oct 18 22:26 b.txt
-rw-rw-r--. 1 linuxprobe linuxprobe 0 Oct 18 22:26 c.csv
-rw-rw-r--. 1 linuxprobe linuxprobe 0 Oct 18 22:26 d.csv
[root@linuxprobe test]# find ./ -group root
./
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -group linuxprobe
./c.csv
./d.csv
[root@linuxprobe test]# chgrp root c.csv
[root@linuxprobe test]# ll -h
total 0
-rw-r--r--. 1 linuxprobe root       0 Oct 18 22:26 a.txt
-rw-r--r--. 1 root       root       0 Oct 18 22:26 b.txt
-rw-rw-r--. 1 linuxprobe root       0 Oct 18 22:26 c.csv
-rw-rw-r--. 1 linuxprobe linuxprobe 0 Oct 18 22:26 d.csv
[root@linuxprobe test]# find ./ -group root
./
./a.txt
./b.txt
./c.csv
[root@linuxprobe test]# find ./ -group linuxprobe
./d.csv

15、根据时间和日期进行查找

[root@linuxprobe test]# ll -h
total 4.0K
-rw-r--r--. 1 root root 21 Oct 18 22:36 a.txt
-rw-r--r--. 1 root root  0 Oct 18 22:36 b.txt
[root@linuxprobe test]# find ./ -atime 3  ## 过去第三天被访问的文件   a:表示访问   m:表示修改,指文件内容的修改  c:表示改变,指的是文件或者目录的权限
[root@linuxprobe test]# find ./ -atime -3  ## 三天内被访问的文件
./
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -amin 3  ## 过去第三分钟被访问的文件
[root@linuxprobe test]# find ./ -amin -3  ## 过去三分钟内被访问的文件
[root@linuxprobe test]# find ./ -amin -30  ## 过去三十分钟内被访问的文件
./
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -mtime 3  ## 过去第三天被修改的文件
[root@linuxprobe test]# find ./ -mtime -3  ## 过去三天内被修改的文件
./
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -mmin -3  ## 过去三分钟内被修改的文件
[root@linuxprobe test]# find ./ -ctime 3  ## 过去第三天被改变权限的文件
[root@linuxprobe test]# find ./ -ctime -3 ## 过去三天内权限被改变的文件
./
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -cmin -3  ## 过去三分钟内权限被改变的文件
[root@linuxprobe test]# ls -l
total 4
-rw-r--r--. 1 root root 21 Oct 17 00:00 a.txt
-rw-r--r--. 1 root root  0 Oct 18 22:36 b.txt
[root@linuxprobe test]# touch -t 202010162210.20 a.txt   ## 修改时间戳,将a.txt向前设定两天
[root@linuxprobe test]# ls -l
total 4
-rw-r--r--. 1 root root 21 Oct 16 22:10 a.txt
-rw-r--r--. 1 root root  0 Oct 18 22:36 b.txt
[root@linuxprobe test]# find ./ -atime +10 -atime -100  ## 查找10天到100天内被访问的文件
[root@linuxprobe test]# find ./ -atime +5 -atime -100   ## 同上
[root@linuxprobe test]# find ./ -atime +1 -atime -100   
./a.txt
[root@linuxprobe test]# find ./ -mtime +1 -mtime -100  ## 查找1天到100天之间修改过的文件
./a.txt
[root@linuxprobe test]# find ./ -ctime +1 -ctime -100  ## 查找1天到100天之间权限发生改变的文件
[root@linuxprobe test]# chmod 777 b.txt ## 修改b.txt文件的权限
[root@linuxprobe test]# ll
total 4
-rw-r--r--. 1 root root 21 Oct 16 22:10 a.txt
-rwxrwxrwx. 1 root root  0 Oct 18 22:36 b.txt
[root@linuxprobe test]# sleep 120  
[root@linuxprobe test]# find ./ -cmin +10 -cmin -50  
./
[root@linuxprobe test]# find ./ -cmin +1 -cmin -5  ## 查找1分钟前到5分钟之间权限发生改变的文件
./b.txt
[root@linuxprobe test]# find ./ -cmin +1 -cmin -50  ## 同上
./
./a.txt
./b.txt

16、基于文件的大小进行查找

[root@linuxprobe test]# ll -h
total 4.0G
-rw-r--r--. 1 root root 2.0M Oct 19 10:36 a.txt
-rw-r--r--. 1 root root 9.8M Oct 19 10:36 b.txt
-rw-r--r--. 1 root root  98M Oct 19 10:37 c.txt
-rw-r--r--. 1 root root 977M Oct 19 10:37 e.txt
-rw-r--r--. 1 root root 2.9G Oct 19 10:38 f.txt
[root@linuxprobe test]# find ./ -size +5M  ## 查找大于5Mb的文件
./b.txt
./c.txt
./e.txt
./f.txt
[root@linuxprobe test]# find ./ -size +500M ## 查找大于500Mb的文件
./e.txt
./f.txt
[root@linuxprobe test]# find ./ -size -500M  ##同上
./
./a.txt
./b.txt
./c.txt
[root@linuxprobe test]# find ./ -size +5M -size +500M  ##查找大于5Mb而且小于500Mb的文件
./e.txt
./f.txt
[root@linuxprobe test]# find ./ -size +1G  ## 以G为单位进行查找
./f.txt
[root@linuxprobe test]# find ./ -size +5000k ## 以k(小写)为单位进行查找
./b.txt
./c.txt
./e.txt
./f.txt

17、查找空文件和空目录

[root@linuxprobe test]# ll -h  
total 8.0K
-rw-r--r--. 1 root root    0 Oct 19 10:49 a.txt
-rw-r--r--. 1 root root    0 Oct 19 10:49 b.txt
-rw-r--r--. 1 root root  292 Oct 19 10:49 c.txt
-rw-r--r--. 1 root root 3.9K Oct 19 10:50 d.txt
drwxr-xr-x. 2 root root    6 Oct 19 10:50 test1
drwxr-xr-x. 2 root root   18 Oct 19 10:50 test2
[root@linuxprobe test]# tree
.
├── a.txt
├── b.txt
├── c.txt
├── d.txt
├── test1
└── test2
    └── a.txt

2 directories, 5 files
[root@linuxprobe test]# find ./ -maxdepth 1 -type f -empty  ##查看空文件
./a.txt
./b.txt
[root@linuxprobe test]# find ./ -type d -empty ##查看空目录
./test1

18、对查找到的文件进行操作

[root@linuxprobe test]# touch a.txt b.txt c.csv d.csv e.pdf f.pdf
[root@linuxprobe test]# ls
a.txt  b.txt  c.csv  d.csv  e.pdf  f.pdf
[root@linuxprobe test]# find ./ -name "*.txt" -exec rm {} \;    ## -exec 表示执行,{}表示查找到的内容,";"表示执行命令的结束,\对";"进行转义
[root@linuxprobe test]# ls
c.csv  d.csv  e.pdf  f.pdf
[root@linuxprobe test]# ll -h
total 0
-rw-r--r--. 1 root root 0 Oct 19 10:55 c.csv
-rw-r--r--. 1 root root 0 Oct 19 10:55 d.csv
-rw-r--r--. 1 root root 0 Oct 19 10:55 e.pdf
-rw-r--r--. 1 root root 0 Oct 19 10:55 f.pdf
drwxr-xr-x. 2 root root 6 Oct 19 10:56 test1
drwxr-xr-x. 2 root root 6 Oct 19 10:57 test2
[root@linuxprobe test]# tree
.
├── c.csv
├── d.csv
├── e.pdf
├── f.pdf
├── test1
└── test2

2 directories, 4 files
[root@linuxprobe test]# find ./ -maxdepth 1 -name "*.pdf" -exec mv {} test2 \;  ## 同上
[root@linuxprobe test]# tree
.
├── c.csv
├── d.csv
├── test1
└── test2
    ├── e.pdf
    └── f.pdf

2 directories, 4 files
[root@linuxprobe test]# find ./ -maxdepth 1 -name "*.csv" -exec mv {} test1 \;  ## 同上
[root@linuxprobe test]# tree
.
├── test1
│   ├── c.csv
│   └── d.csv
└── test2
    ├── e.pdf
    └── f.pdf

2 directories, 4 files
[root@linuxprobe test]#

19、对文件大小进行排序

[root@linuxprobe test]# ll -h
total 317M
-rw-r--r--. 1 root root 1000K Oct 19 11:05 a.txt
-rw-r--r--. 1 root root  9.8M Oct 19 11:05 b.txt
-rw-r--r--. 1 root root   98M Oct 19 11:05 c.txt
-rw-r--r--. 1 root root   88M Oct 19 11:06 d.txt
-rw-r--r--. 1 root root  1.8M Oct 19 11:06 e.txt
-rw-r--r--. 1 root root  108M Oct 19 11:09 f.csv
-rw-r--r--. 1 root root   12M Oct 19 11:09 g.csv
[root@linuxprobe test]# find ./ -type f
./a.txt
./b.txt
./c.txt
./d.txt
./e.txt
./f.csv
./g.csv
[root@linuxprobe test]# find ./ -type f -exec ls -s {} \;
1000 ./a.txt
10000 ./b.txt
100000 ./c.txt
90000 ./d.txt
1800 ./e.txt
110000 ./f.csv
11800 ./g.csv
[root@linuxprobe test]# find ./ -type f -exec ls -s {} \; | sort -n  ## 结合管道符,sort命令进行排序
1000 ./a.txt
1800 ./e.txt
10000 ./b.txt
11800 ./g.csv
90000 ./d.txt
100000 ./c.txt
110000 ./f.csv
[root@linuxprobe test]# find ./ -type f -exec ls -s {} \; | sort -n -r  ## 反向排序
110000 ./f.csv
100000 ./c.txt
90000 ./d.txt
11800 ./g.csv
10000 ./b.txt
1800 ./e.txt
1000 ./a.txt
[root@linuxprobe test]# find ./ -type f -name "*.txt"
./a.txt
./b.txt
./c.txt
./d.txt
./e.txt
[root@linuxprobe test]# find ./ -type f -name "*.txt" -exec ls -s {} \;
1000 ./a.txt
10000 ./b.txt
100000 ./c.txt
90000 ./d.txt
1800 ./e.txt
[root@linuxprobe test]# find ./ -type f -name "*.txt" -exec ls -s {} \; | sort -n
1000 ./a.txt
1800 ./e.txt
10000 ./b.txt
90000 ./d.txt
100000 ./c.txt
[root@linuxprobe test]# find ./ -type f -name "*.txt" -exec ls -s {} \; | sort -n -r
100000 ./c.txt
90000 ./d.txt
10000 ./b.txt
1800 ./e.txt
1000 ./a.txt
[root@linuxprobe test]#

直接使用ls -S(命令)实现按照文件大小排序

[root@linuxprobe test]# ls -S | xargs -n 1 du -s
110000  f.csv
100000  c.txt
90000   d.txt
11800   g.csv
10000   b.txt
1800    e.txt
1000    a.txt
[root@linuxprobe test]# ls -S | xargs -n 1 du -s | tac
1000    a.txt
1800    e.txt
10000   b.txt
11800   g.csv
90000   d.txt
100000  c.txt
110000  f.csv
[root@linuxprobe test]# ls -S *.txt | xargs -n 1 du -h
98M     c.txt
88M     d.txt
9.8M    b.txt
1.8M    e.txt
1000K   a.txt
[root@linuxprobe test]# ls -S *.txt | xargs -n 1 du -h | tac
1000K   a.txt
1.8M    e.txt
9.8M    b.txt
88M     d.txt
98M     c.txt

参考:https://www.jianshu.com/p/779201dee98b

原文地址:https://www.cnblogs.com/liujiaxin2018/p/13829396.html