Linux常用命令-find

命令

find

描述

search for files in a directory hierarchy
查找文件

用法

1
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
Options:
-H 不跟随链接跳转,除非指定的路径是目录软链接
-L 跟随链接跳转,即显示源文件的属性
-P 不跟随链接跳转,即显示链接文件本身属性而非源文件,此为默认选项
-- 表示后面的参数都不是选项

expression: operators, options, tests, actions

operators (默认为-and):
优先级:括号() > 非(-not) > 与(-a) > 或(-o)
( EXPR ) 分组,注意括号需要转义,如\(...\)
! EXPR 取反
-not EXPR
EXPR1 -a EXPR2 并且
EXPR1 -and EXPR2
EXPR1 -o EXPR2 或者
EXPR1 -or EXPR2
EXPR1 , EXPR2
德摩根定律:
(非A)或(非B) = 非(A且B)
(非A)且(非B) = 非(A或B)
!A -a !B = !(A -o B)
!A -o !B = !(A -a B)

options: 这几个选项需要在其他选项之前指定
-daystart 从今天0点开始计算,而不是当前时间开始计算.find默认是以当前时间的前24h开始计算
-regextype TYPE 指定正则表达式语法,TYPE可选参数emacs(默认),posix-awk,posix-basic,posix-egrep,posix-extended
-depth 先处理每个目录下的内容,再处理目录本身
-maxdepth LEVELS 查找的最大目录层级,当前目录为1,一级子目录为2,二级子目录为3,后续依次累加
-mindepth LEVELS 最小目录层级,如为2表示不查找当前目录,而是从一级子目录开始查找
-maxdepth 3 -mindepth 3 两个目录层级相同时则只递归搜索指定层级目录
-mount 只查找和指定目录在同一文件系统下的文件
-xautofs 不查找autofs文件系统中的文件
-xdev 不查找其他文件系统中的文件
-ignore_readdir_race 在find读取目录的过程中,如果手动删除了目录中的文件,不报错
-noignore_readdir_race 读取目录中的文件失败时报错
-warn 开启警告信息
-nowarn 关闭警告信息
--version find不同版本选项可能存在差异,以下版本为find (GNU findutils) 4.5.11

tests:
(N can be +N or -N or N): N为整数,+N表示在时间N之前,-N表示在时间N以内,N表示等于时间N
以-mtime选项为例
+1表示时间在48h之前,1表示时间在48-24h之间,-1表示时间在24h之内
+2表示时间在72h之前,2表示时间在72-48h之间,-2表示时间在48h之内
+3表示上次修改文件的时间mtime在4天之前,-3表示mtime在3天以内,3表示mtime等于3天
<___|___>today
^ ^ ^
+3 3 -3
-amin N 上次访问文件的时间atime,单位分钟
-atime N 上次访问文件的时间atime,单位天,24h
-cmin N 上次更改文件状态的时间ctime,单位分钟
-ctime N 上次更改文件状态的时间ctime,单位天
-mmin N 上次修改文件的时间mtime,单位分钟
-mtime N 上次修改文件的时间mtime,单位天
-empty 空文件或空目录
-true true
-false false
-fstype TYPE 指定文件系统的类型,如xfs,ext4,tmpfs
-gid N 指定gid
-group NAME 指定group
-inum N inode值为N的文件
-links N 链接数为N的文件
-lname PATTERN 匹配PATTERN的链接文件
-ilname PATTERN 忽略大小写匹配PATTERN的链接文件
-name PATTERN 匹配PATTERN的文件,基于文件名匹配,可用元字符包括*,?,[],[^]
# PATTERN使用通配符时必须使用"或'括起来,不能包括路径
-iname PATTERN 忽略大小写匹配PATTERN的文件
-anewer FILE 比指定文件的atime更新的文件
-cnewer FILE 比指定文件的ctime更新的文件
-newer FILE 比指定文件的mtime更新的文件
-newerXY reference 参考reference属性查找目标文件
# X表示目标文件属性,可选参数a,c,m. Y表示参考属性,可选参数a,c,m,t
# acm表示atime,ctime,mtime. t表示具体时间,格式为'YYYYMMDD hh:mm:ss',即可以通过date -d命令解释的时间格式
# 如-newermt '20210215 10:00:00' 表示查找mtime在指定时间之后的文件
# 如-newermt TIME1 ! -newermt TIME2 表示指定时间范围在TIME1-TIME2之间
-nouser 没有与文件的uid对应的用户,即文件的所有者显示为uid而非用户名
-nogroup 没有与文件的gid对应的用户组,即文件的所属组显示为gid而非用户组名
-path PATTERN 匹配文件名或路径,不转义/和.
-ipath PATTERN 忽略大小写匹配文件名和路径
-perm [-/]MODE 匹配文件权限,有三种匹配方式
# 严格匹配mode,如指定权限777,文件权限777则匹配,其他文件权限均不匹配;
# 包含匹配-mode,表示当文件权限包括指定权限时即匹配,如指定权限-555,文件权限755则匹配,文件权限644则不匹配;
# 模糊匹配/mode,只要有一个权限位包含指定权限即匹配,0表示忽略权限位,如指定权限/002,文件权限666则匹配,文件权限600则不匹配
-regex PATTERN 正则匹配文件名,注意这个正则是包括路径的,所以需要以.*开头,如.*access.log
-iregex PATTERN 忽略大小写的正则匹配
-readable 匹配可读文件
-writable 匹配可写文件
-executable 匹配可执行文件和可搜索目录
-samefile NAME 与指定文件inode值相同的文件,即查找硬链接文件
-size N[bcwkMG] 匹配文件大小,单位有块b(默认),字节c,双字节w,k,M,G,如+5k表示超过5k大小的文件
# N: (N-1,N],如6k表示(5k,6k],包括6k,但不包括5k
# -N: [0,N-1],如-6k表示[0,5k],包括0k和5k
# +N: (N,∞),如+6k表示(6k,∞),不包括6k,大于6k
-type [bcdpflsD] 匹配文件类型,块文件b,字符文件c,目录d,管道文件p,普通文件f,链接文件l,套接字s
-used N 上次访问文件是在上次修改文件状态的N天之后,即文件修改N天后被访问过
-uid N 指定uid
-user NAME 指定user

actions:
-delete 删除文件,建议先通过-ls确认文件列表然后再删除文件
-print 显示查找到的完整的文件路径
-print0 显示文件名,但是不加换行符
-printf FORMAT 指定显示格式,如\b,\n,\t,\v
-fprint FILE 保存查找到的文件名到指定文件
-fprint0 FILE 保存文件名到文件,不加换行符,经测试该选项有bug无法显示所有文件名
-fprintf FILE FORMAT 指定文件格式保存
-ls 列表显示查找到的文件详情
-fls FILE 保存-ls结果到指定文件
-prune 如果文件是一个目录则不进入目录,该选项与-delete和-depth两个选项冲突,不能同时使用
-quit 立即退出
-exec COMMAND ; 执行命令,最后的分号;不能省略
-exec COMMAND {} + -ok COMMAND ;
-execdir COMMAND ;
-execdir COMMAND {} + -okdir COMMAND ;
-ok COMMAND ; 等同于-exec,但是执行前会先询问用户
-okdir COMMAND ; 等同于-execdir,但是执行前会先询问用户

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# 默认路径为当前目录,并且递归查找子目录
$ find -name "a.txt"
./a.txt
./bak/a.txt
./bak/aa/a.txt
# 支持指定多个目录查找
$ find dir1 dir3 dir4 -name 'a.tar.gz'
dir1/a.tar.gz
dir4/a.tar.gz
dir4/bak/a.tar.gz
# 指定路径,三个命令结果一致
$ find ./bak -name '*.txt'
$ find -path './bak/*.txt'
$ find -path './bak*' -name '*.txt'
./bak/aa.txt
./bak/abc.txt
./bak/a b.txt
./bak/a.txt
./bak/b.txt
./bak/url.txt
./bak/aa/a.txt
# 过滤路径中包括aa的文件
$ find -name '*.txt' -path '*aa*'
./aa.txt
./bak/aa.txt
./bak/aa/a.txt
# 排除子目录./bak/
$ find -path './bak/*' -prune -o -print
# 排除./bak/子目录,同时查找*.txt或*.tar.gz文件
$ find ! -path './bak/*' \( -name '*.txt' -o -name '*.tar.gz' \)
./a.tar.gz
./b.txt
./a.txt
./aa.txt
./abc.txt
./url.txt
./a b.txt
./images.tar.gz
./a1.txt
# 查找当前目录下的所有普通文件
$ find -type f
# 查找子目录
$ find -type d
# 指定文件大小查找
$ find -size +5k -ls
50888803 12 -rw-r--r-- 1 root root 8281 Feb 7 01:22 ./download.html
33601985 12 -rw-r--r-- 1 root root 8281 Feb 17 15:35 ./bak/download.html
# 查找空文件
$ find -maxdepth 1 -empty -ls
50888797 0 -rw-r--r-- 1 root root 0 Feb 17 22:35 ./cc.xt
50888798 0 -rw-r--r-- 1 root root 0 Feb 17 15:22 ./aa.txt
50888804 0 -rw-r--r-- 1 root root 0 Feb 17 15:33 ./a\ b.txt
50888805 0 -rw-r--r-- 1 root root 0 Feb 17 15:35 ./aa\ bb
$ chown 1005:1005 a
# 查找没有用户的文件,即文件显示为uid
$ find -nouser -ls
50888806 0 -rwxrwxrwx 1 1005 1005 0 Feb 19 20:01 ./a
# 多个条件,逻辑与查找
$ find -size +1k -size -5k -ls
50888802 4 -rw-r--r-- 1 root root 4080 Nov 20 2019 ./messages.log
# 多个条件,逻辑或查找
$ find -name '*.txt' -o -name '*.tar.gz'
./a.tar.gz
./b.txt
./a.txt
./images.tar.gz
# 注意-name '*.tar.gz' -ls中间省略了-a,两者是并且的关系,所以结果只显示了*.tar.gz的列表
$ find -name '*.txt' -o -name '*.tar.gz' -ls
50888799 4 -rw-r--r-- 1 root root 223 Feb 17 15:25 ./a.tar.gz
50888807 4 -rw-r--r-- 1 root root 442 Feb 17 18:04 ./images.tar.gz
# 需要使用括号()来分组表示前两个-name条件优先匹配,然后再执行-ls
$ find \( -name '*.txt' -o -name '*.tar.gz' \) -ls
50888799 4 -rw-r--r-- 1 root root 223 Feb 17 15:25 ./a.tar.gz
50888794 4 -rw-r--r-- 1 root root 34 Feb 17 10:12 ./b.txt
50888795 4 ----r----- 2 root root 11 Feb 19 16:06 ./a.txt
50888807 4 -rw-r--r-- 1 root root 442 Feb 17 18:04 ./images.tar.gz
# 排除*.txt文件
$ find ! -name '*.txt'
# 删除两周15天以前的日志
find /data/logs -maxdepth 1 -type f -mtime +14 -print0 | xargs -0 rm -f
# 正则匹配需要以.*开头,否则结果为空,因为默认包括路径./
$ find -regex a.*.txt
$ find -regex .*a.txt
./a.txt
./aa.txt
./bak/aa.txt
./bak/a.txt
./bak/aa/a.txt
$ find -regex '.*\.\(txt\|tar.gz\)'
./a.tar.gz
./b.txt
./a.txt

# -perm mode,-mode,/mode
# 严格匹配mode,如指定权限777,文件权限777则匹配,其他文件权限均不匹配;
# 包含匹配-mode,表示当文件权限包括指定权限时即匹配,如指定权限-555,文件权限755则匹配,文件权限644则不匹配;
# 模糊匹配/mode,只要有一个权限位包含指定权限即匹配,0表示忽略权限位,如指定权限/002,文件权限666则匹配,文件权限600则不匹配
# 755精确匹配755权限的文件
find -perm 755 -ls
# -222 三种用户ugo都必须有写权限即匹配,如文件644则匹配,文件755则匹配,文件600则不匹配
find -perm -222 -ls
# /222 三种用户ugo只要有一个有写权限即匹配
find -perm /222 -ls
# /002 other用户有写权限即匹配,如文件666则匹配,文件777则匹配,文件644则不匹配
find -perm /002 -ls
$ touch a b c d e f
$ chmod 777 a
$ chmod 755 b
$ chmod 444 c
$ chmod 600 d
$ chmod 000 e
$ ll a b c d e f
-rwxrwxrwx 1 root root 0 Feb 19 20:01 a
-rwxr-xr-x 1 root root 0 Feb 19 20:01 b
-r--r--r-- 1 root root 0 Feb 19 20:01 c
-rw------- 1 root root 0 Feb 19 20:01 d
---------- 1 root root 0 Feb 19 20:01 e
-rw-r--r-- 1 root root 0 Feb 19 20:01 f
# -perm 777表示严格匹配该权限值的文件
$ find -perm 777 -ls
50888806 0 -rwxrwxrwx 1 root root 0 Feb 19 20:01 ./a
# -perm -700表示模糊匹配权限值,依次从左往右匹配,当文件权限包括指定权限时即匹配,如文件权限为777包括700,即匹配文件
$ find -perm -700 -ls
50888783 0 drwxr-xr-x 3 root root 334 Feb 19 20:37 .
50888806 0 -rwxrwxrwx 1 1005 1005 0 Feb 19 20:01 ./a
50888828 0 -rwxr-xr-x 1 root root 0 Feb 19 20:01 ./b
$ find -perm -770 -ls
50888806 0 -rwxrwxrwx 1 1005 1005 0 Feb 19 20:01 ./a
# 文件a的权限为777,包括555,文件b的权限为755,也包括555
$ find -perm -555 -ls
50888783 0 drwxr-xr-x 3 root root 309 Feb 19 20:01 .
50888806 0 -rwxrwxrwx 1 root root 0 Feb 19 20:01 ./a
50888828 0 -rwxr-xr-x 1 root root 0 Feb 19 20:01 ./b

# -mtime,-daystart
# 当前时间为2021-02-18 19:56:45
$ touch -d "-1day" a
$ touch -d "-2day" b
$ touch -d "-3day" c
$ touch -d "-4day" d
$ touch -d "-5day" e
$ touch -d "-6day" f
$ find -mtime +3 -ls
16785052 0 -rw-r--r-- 1 root root 0 Feb 14 19:57 ./d
16785053 0 -rw-r--r-- 1 root root 0 Feb 13 19:57 ./e
16785054 0 -rw-r--r-- 1 root root 0 Feb 12 19:57 ./f
$ find -mtime 3 -ls
16785051 0 -rw-r--r-- 1 root root 0 Feb 15 19:56 ./c
$ find -mtime -3 -ls
16785048 0 drwxr-xr-x 2 root root 60 Feb 18 19:56 .
16785049 0 -rw-r--r-- 1 root root 0 Feb 17 19:56 ./a
16785050 0 -rw-r--r-- 1 root root 0 Feb 16 19:56 ./b
$ touch -t 02142359 c0
$ touch -t 02150000 c1
$ touch -t 02150000.01 c2
$ touch -t 02150500 c3
$ touch -t 02152200 c4
$ touch -t 02152359 c5
$ touch -t 02160000 c6
$ touch -t 02160000.01 c7
$ ll
-rw-r--r-- 1 root root 0 Feb 17 19:56 a
-rw-r--r-- 1 root root 0 Feb 16 19:56 b
-rw-r--r-- 1 root root 0 Feb 15 19:56 c
-rw-r--r-- 1 root root 0 Feb 14 23:59 c0
-rw-r--r-- 1 root root 0 Feb 15 00:00 c1
-rw-r--r-- 1 root root 0 Feb 15 00:00 c2
-rw-r--r-- 1 root root 0 Feb 15 05:00 c3
-rw-r--r-- 1 root root 0 Feb 15 22:00 c4
-rw-r--r-- 1 root root 0 Feb 15 23:59 c5
-rw-r--r-- 1 root root 0 Feb 16 00:00 c6
-rw-r--r-- 1 root root 0 Feb 16 00:00 c7
-rw-r--r-- 1 root root 0 Feb 14 19:57 d
-rw-r--r-- 1 root root 0 Feb 13 19:57 e
-rw-r--r-- 1 root root 0 Feb 12 19:57 f
$ stat c*
File: ‘c’
Access: 2021-02-15 19:56:58.752573519 +0800
Modify: 2021-02-15 19:56:58.752573519 +0800
Change: 2021-02-18 19:56:58.750934386 +0800
Birth: -
File: ‘c0’
Access: 2021-02-14 23:59:00.000000000 +0800
Modify: 2021-02-14 23:59:00.000000000 +0800
Change: 2021-02-18 19:58:04.146173144 +0800
Birth: -
File: ‘c1’
Access: 2021-02-15 00:00:00.000000000 +0800
Modify: 2021-02-15 00:00:00.000000000 +0800
Change: 2021-02-18 19:58:09.214972733 +0800
Birth: -
File: ‘c2’
Access: 2021-02-15 00:00:01.000000000 +0800
Modify: 2021-02-15 00:00:01.000000000 +0800
Change: 2021-02-18 19:58:41.721928943 +0800
Birth: -
File: ‘c3’
Access: 2021-02-15 05:00:00.000000000 +0800
Modify: 2021-02-15 05:00:00.000000000 +0800
Change: 2021-02-18 19:58:53.128928699 +0800
Birth: -
File: ‘c4’
Access: 2021-02-15 22:00:00.000000000 +0800
Modify: 2021-02-15 22:00:00.000000000 +0800
Change: 2021-02-18 19:59:01.334928524 +0800
Birth: -
File: ‘c5’
Access: 2021-02-15 23:59:59.000000000 +0800
Modify: 2021-02-15 23:59:59.000000000 +0800
Change: 2021-02-18 19:59:19.372461979 +0800
Birth: -
File: ‘c6’
Access: 2021-02-16 00:00:00.000000000 +0800
Modify: 2021-02-16 00:00:00.000000000 +0800
Change: 2021-02-18 19:59:26.513961401 +0800
Birth: -
File: ‘c7’
Access: 2021-02-16 00:00:01.000000000 +0800
Modify: 2021-02-16 00:00:01.000000000 +0800
Change: 2021-02-18 19:59:31.608860988 +0800
Birth: -
# 根据mtime排序
$ ls -lt
total 0
-rw-r--r-- 1 root root 0 Feb 17 19:56 a
-rw-r--r-- 1 root root 0 Feb 16 19:56 b
-rw-r--r-- 1 root root 0 Feb 16 00:00 c7
-rw-r--r-- 1 root root 0 Feb 16 00:00 c6
-rw-r--r-- 1 root root 0 Feb 15 23:59 c5
-rw-r--r-- 1 root root 0 Feb 15 22:00 c4
-rw-r--r-- 1 root root 0 Feb 15 19:56 c
-rw-r--r-- 1 root root 0 Feb 15 05:00 c3
-rw-r--r-- 1 root root 0 Feb 15 00:00 c2
-rw-r--r-- 1 root root 0 Feb 15 00:00 c1
-rw-r--r-- 1 root root 0 Feb 14 23:59 c0
-rw-r--r-- 1 root root 0 Feb 14 19:57 d
-rw-r--r-- 1 root root 0 Feb 13 19:57 e
-rw-r--r-- 1 root root 0 Feb 12 19:57 f
# 当前时间为2021-02-18 19:59:35,这里假设以下find命令为同时执行
# 时间范围为2021-02-14 19:59:35之前
$ find -mtime +3 -ls
16785052 0 -rw-r--r-- 1 root root 0 Feb 14 19:57 ./d
16785053 0 -rw-r--r-- 1 root root 0 Feb 13 19:57 ./e
16785054 0 -rw-r--r-- 1 root root 0 Feb 12 19:57 ./f
# 时间范围为2021-02-14 19:59:35到2021-02-15 19:59:35
$ find -mtime 3 -ls
16785051 0 -rw-r--r-- 1 root root 0 Feb 15 19:56 ./c
16785055 0 -rw-r--r-- 1 root root 0 Feb 14 23:59 ./c0
16785044 0 -rw-r--r-- 1 root root 0 Feb 15 00:00 ./c1
16785056 0 -rw-r--r-- 1 root root 0 Feb 15 00:00 ./c2
16785057 0 -rw-r--r-- 1 root root 0 Feb 15 05:00 ./c3
# 时间范围为2021-02-15 19:59:35之后
$ find -mtime -3 -ls
16785048 0 drwxr-xr-x 2 root root 140 Feb 18 19:59 .
16785049 0 -rw-r--r-- 1 root root 0 Feb 17 19:56 ./a
16785050 0 -rw-r--r-- 1 root root 0 Feb 16 19:56 ./b
16785058 0 -rw-r--r-- 1 root root 0 Feb 15 22:00 ./c4
16785060 0 -rw-r--r-- 1 root root 0 Feb 15 23:59 ./c5
16785059 0 -rw-r--r-- 1 root root 0 Feb 16 00:00 ./c6
16785061 0 -rw-r--r-- 1 root root 0 Feb 16 00:00 ./c7
# 时间范围为20210214-23:59:59之前
$ find -daystart -mtime +3 -ls
16785052 0 -rw-r--r-- 1 root root 0 Feb 14 19:57 ./d
16785053 0 -rw-r--r-- 1 root root 0 Feb 13 19:57 ./e
16785054 0 -rw-r--r-- 1 root root 0 Feb 12 19:57 ./f
16785055 0 -rw-r--r-- 1 root root 0 Feb 14 23:59 ./c0
# 时间范围为20210215-00:00:01到20210216-00:00:00
$ find -daystart -mtime 3 -ls
16785051 0 -rw-r--r-- 1 root root 0 Feb 15 19:56 ./c
16785056 0 -rw-r--r-- 1 root root 0 Feb 15 00:00 ./c2
16785057 0 -rw-r--r-- 1 root root 0 Feb 15 05:00 ./c3
16785058 0 -rw-r--r-- 1 root root 0 Feb 15 22:00 ./c4
16785060 0 -rw-r--r-- 1 root root 0 Feb 15 23:59 ./c5
16785059 0 -rw-r--r-- 1 root root 0 Feb 16 00:00 ./c6
# 时间范围为20210216-00:00:00之后
$ find -daystart -mtime -3 -ls
16785048 0 drwxr-xr-x 2 root root 140 Feb 18 19:59 .
16785049 0 -rw-r--r-- 1 root root 0 Feb 17 19:56 ./a
16785050 0 -rw-r--r-- 1 root root 0 Feb 16 19:56 ./b
16785059 0 -rw-r--r-- 1 root root 0 Feb 16 00:00 ./c6
16785061 0 -rw-r--r-- 1 root root 0 Feb 16 00:00 ./c7

# -anewer,-cnewer,newer
# 显示atime并按时间排序
$ ls -ult
-rw-r--r-- 1 root root 3 Feb 19 18:34 c6
-rw-r--r-- 1 root root 0 Feb 17 19:56 a
-rw-r--r-- 1 root root 0 Feb 16 19:56 b
-rw-r--r-- 1 root root 0 Feb 16 00:00 c7
-rw-r--r-- 1 root root 0 Feb 15 23:59 c5
-rw-r--r-- 1 root root 0 Feb 15 22:00 c4
-rw-r--r-- 1 root root 0 Feb 15 19:56 c
-rw-r--r-- 1 root root 0 Feb 15 05:00 c3
-rw-r--r-- 1 root root 0 Feb 15 00:00 c2
-rw-r--r-- 1 root root 0 Feb 15 00:00 c1
-rw-r--r-- 1 root root 0 Feb 14 23:59 c0
-rw-r--r-- 1 root root 0 Feb 14 19:57 d
-rw-r--r-- 1 root root 0 Feb 13 19:57 e
-rw-r--r-- 1 root root 0 Feb 12 19:57 f
# 查找比文件c的atime更新的文件
$ find -anewer c -ls
16785048 0 drwxr-xr-x 2 root root 151 Feb 19 18:30 .
16785049 0 -rw-r--r-- 1 root root 0 Feb 17 19:56 ./a
16785050 0 -rw-r--r-- 1 root root 0 Feb 16 19:56 ./b
16785058 0 -rw-r--r-- 1 root root 0 Feb 15 22:00 ./c4
16785060 0 -rw-r--r-- 1 root root 0 Feb 15 23:59 ./c5
16785059 4 -rw-r--r-- 1 root root 3 Feb 19 18:34 ./c6
16785061 0 -rw-r--r-- 1 root root 0 Feb 16 00:00 ./c7
# 显示ctime并按时间排序
$ ls -clt
# 查找比文件c的ctime更新的文件,但是cnewer选项过滤有问题,此处列出了所有文件
$ find -cnewer c3 -ls
# 显示mtime并按时间排序,由于当前文件的atime和mtime时间一致,所以查找结果也是一样的
$ ls -lt
# 查找比文件c的mtime更新的文件
$ find -newer c -ls

# -newerXY reference
$ date -d '20210215 18:25:30'
Mon Feb 15 18:25:30 CST 2021
# 查找20210215全天的文件
$ find -newermt '20210214 23:59:59' ! -newermt '20210215 23:59:59' -ls
16785051 0 -rw-r--r-- 1 root root 0 Feb 15 19:56 ./c
16785044 0 -rw-r--r-- 1 root root 0 Feb 15 00:00 ./c1
16785056 0 -rw-r--r-- 1 root root 0 Feb 15 00:00 ./c2
16785057 0 -rw-r--r-- 1 root root 0 Feb 15 05:00 ./c3
16785058 0 -rw-r--r-- 1 root root 0 Feb 15 22:00 ./c4
16785060 0 -rw-r--r-- 1 root root 0 Feb 15 23:59 ./c5
# 或者通过时间戳文件来查找,结果一样
$ touch -t 202102142359.59 timestamp1
$ touch -t 202102152359.59 timestamp2
$ find -newer timestamp1 ! -newer timestamp2 -ls
16785051 0 -rw-r--r-- 1 root root 0 Feb 15 19:56 ./c
16785044 0 -rw-r--r-- 1 root root 0 Feb 15 00:00 ./c1
16785056 0 -rw-r--r-- 1 root root 0 Feb 15 00:00 ./c2
16785057 0 -rw-r--r-- 1 root root 0 Feb 15 05:00 ./c3
16785058 0 -rw-r--r-- 1 root root 0 Feb 15 22:00 ./c4
16785060 0 -rw-r--r-- 1 root root 0 Feb 15 23:59 ./c5
16785063 0 -rw-r--r-- 1 root root 0 Feb 15 23:59 ./timestamp2
$ rm -f timestamp1 timestamp2
# 指定时间范围查找
$ find -newermt '20210215 12:00:00' ! -newermt '20210215 22:00:00' -ls
16785051 0 -rw-r--r-- 1 root root 0 Feb 15 19:56 ./c
16785058 0 -rw-r--r-- 1 root root 0 Feb 15 22:00 ./c4

### -mtime,-daystart 验证过程start ###
# 如当前时间为20210218-15:25:00
# 默认以当前时间为时间节点,按24h推算
# 时间范围在20210214-15:25:00之前的文件,即4*24h之前的文件
find -mtime +3
# 时间范围从20210214-15:25:00到20210215-15:25:00,即前4*24h到前3*24h之间的文件
find -mtime 3
# 时间范围在20210215-15:25:00之后的文件,即3*24h以内的文件,其实也包括未来时间的文件
fine -mtime -3

# -daystart表示以今天00:00为时间节点,按天推算
# 时间范围在20210214-23:59:59之前的文件,即4天之前的文件
find -daystart -mtime +3
# 时间范围从20210215-00:00:01到20210216-00:00:00,即查找20210215当天修改过的文件
find -daystart -mtime 3
# 时间范围在20210216-00:00:00之后的文件,即3天以内的文件,包括当天,当天也算1天
find -daystart -mtime -3

# 当前时间为20210218-18:01:20,使用-daystart选项的时间划分如下
past<--20210214-23:59:59 0215-00:00:01-->0216-00:00:00 20210216-00:00:00-->future
+3 3 -3
# 有问题:使用+3和3均无法查找到0215-00:00:00的文件;使用3和-3均会查找到0216-00:00:00的文件
# 验证过程如下

# +3 20210214-23:59:59之前
$ date
Thu Feb 18 18:01:20 CST 2021
$ date -d '-3day'
Mon Feb 15 18:01:25 CST 2021
# 以ISO8601时间格式显示
$ date -Iseconds
2021-02-18T18:01:31+0800
$ date -Iseconds -d '-3day'
2021-02-15T18:01:40+0800
$ touch -t 02150000 ccc
$ stat ccc
Access: 2021-02-15 00:00:00.000000000 +0800
Modify: 2021-02-15 00:00:00.000000000 +0800
Change: 2021-02-18 18:15:57.562912707 +0800
$ find -daystart -mtime +3 -ls
$ touch -t 02142359.59 ccc
$ stat ccc
Access: 2021-02-14 23:59:59.000000000 +0800
Modify: 2021-02-14 23:59:59.000000000 +0800
Change: 2021-02-18 18:16:19.380145738 +0800
$ find -daystart -mtime +3 -ls
15968 0 -rw-r--r-- 1 root root 0 Feb 14 23:59 ./ccc

# 3 20210215-00:00:01到20210216-00:00:00
$ touch -t 02150000 ccc
$ find -daystart -mtime 3 -ls
$ touch -t 02150000.01 ccc
$ stat ccc
Access: 2021-02-15 00:00:01.000000000 +0800
Modify: 2021-02-15 00:00:01.000000000 +0800
Change: 2021-02-18 18:18:22.859905581 +0800
$ find -daystart -mtime 3 -ls
15968 0 -rw-r--r-- 1 root root 0 Feb 15 00:00 ./ccc
$ touch -t 02160000 ccc
$ stat ccc
Access: 2021-02-16 00:00:00.000000000 +0800
Modify: 2021-02-16 00:00:00.000000000 +0800
Change: 2021-02-18 18:18:57.150702224 +0800
$ find -daystart -mtime 3 -ls
15968 0 -rw-r--r-- 1 root root 0 Feb 16 00:00 ./ccc
$ touch -t 02160000.01 ccc
$ stat ccc
Access: 2021-02-16 00:00:01.000000000 +0800
Modify: 2021-02-16 00:00:01.000000000 +0800
Change: 2021-02-18 18:19:20.714978260 +0800
$ find -daystart -mtime 3 -ls

# -3 20210216-00:00:00之后
$ touch -t 02160000 ccc
$ find -daystart -mtime -3 -ls
6785 0 drwxr-xr-x 2 root root 17 Feb 18 18:13 .
15968 0 -rw-r--r-- 1 root root 0 Feb 16 00:00 ./ccc
$ touch -t 02152359.59 ccc
$ stat ccc
Access: 2021-02-15 23:59:59.000000000 +0800
Modify: 2021-02-15 23:59:59.000000000 +0800
Change: 2021-02-18 18:37:02.921417518 +0800
$ find -daystart -mtime -3 -ls
6785 0 drwxr-xr-x 2 root root 17 Feb 18 18:13 .
# -3 其实也包括未来时间的文件
$ touch -t 02200000 ccc
$ stat ccc
Access: 2021-02-20 00:00:00.000000000 +0800
Modify: 2021-02-20 00:00:00.000000000 +0800
Change: 2021-02-18 18:28:41.238861864 +0800
$ find -daystart -mtime -3 -ls
6785 0 drwxr-xr-x 2 root root 17 Feb 18 18:13 .
15968 0 -rw-r--r-- 1 root root 0 Feb 20 2021 ./ccc
### -mtime,-daystart 验证过程end ###