其他分享
首页 > 其他分享> > find常用

find常用

作者:互联网

先看目录结构:

allen@lenovo:Demo$ tree $(pwd)
/home/allen/Downloads/Demo
├── dir
│   └── infile
├── file1
├── file2
└── file3

1 directory, 4 files

先看最简单的,按名称搜索:

allen@lenovo:Demo$ find . -name "file1"
./file1

支持通配符:

allen@lenovo:Demo$ find . -name "file*"
./file1
./file2
./file3

搜索出来的都是相对路径,那是因为搜索目录指定了当前目录,如果改成绝对路径:

allen@lenovo:Demo$ find $(pwd) -name "file*"
/home/allen/Downloads/Demo/file1
/home/allen/Downloads/Demo/file2
/home/allen/Downloads/Demo/file3

按类型搜索:

# 文件类型 f
allen@lenovo:Demo$ find . -type f
./dir/infile
./file1
./file2
./file3
# 目录类型 d
allen@lenovo:Demo$ find . -type d -name dir
./dir

匹配条件默认都是AND,如果是OR:

# 名称是dir且类型是文本的
allen@lenovo:Demo$ find . -name dir -type f
# 和上面一样的效果
allen@lenovo:Demo$ find . -name dir -a -type f
# 名称是dir或类型是文本的
allen@lenovo:Demo$ find . -name dir -o -type f
./dir
./dir/infile
./file1
./file2
./file3

按大小检索:

# 大于10k,且小于10M,且小于10G
allen@lenovo:Demo$ find . -size +10k -size -10M -size -10G

按修改时间,精确到天:

# 24小时内修改的
allen@lenovo:Demo$ find . -mtime 0
.
./dir
./dir/infile
./file1
./file2
./file3
# 昨天修改的
allen@lenovo:Demo$ find . -mtime 1
# 2天内修改的,小于2天
allen@lenovo:Demo$ find . -mtime -2
# 2天前修改的,大于2天
allen@lenovo:Demo$ find . -mtime +2

按修改时间,精确到分钟:

# 和上面一样的,只是换了个参数
# 30分钟内修改的,小于30分钟
allen@lenovo:Demo$ find . -mmin -30

按修改时间,指定一个时间:

# 先看修改时间
allen@lenovo:Demo$ ll
总用量 0
drwxrwxr-x 2 allen allen 20 12月 20 09:35 dir
-rw-rw-r-- 1 allen allen  0 12月 20 09:34 file1
-rw-rw-r-- 1 allen allen  0 12月 20 09:43 file2
-rw-rw-r-- 1 allen allen  0 12月 20 09:43 file3
# 比指定时间新的
allen@lenovo:Demo$ find . -newermt "2020-12-20 09:40"
.
./file2
./file3
# 反向
allen@lenovo:Demo$ find . ! -newermt "2020-12-20 09:40"
./dir
./dir/infile
./file1

按权限检索:

# 权限需要正好是664的
allen@lenovo:Demo$ find . -perm 664
./dir/infile
./file1
./file2
./file3
# 所属人,组,其他人,至少都要有r权限
allen@lenovo:Demo$ find . -perm -444
.
./dir
./dir/infile
./file1
./file2
./file3
# 所属人,组,其他人,只要有一个拥有x权限
allen@lenovo:Demo$ find . -perm /111
.
./dir

 

标签:常用,lenovo,Demo,file1,allen,find,dir
来源: https://blog.csdn.net/aliang_godlike/article/details/111413983