如何在 OSX 上为 find 命令指定日期范围?

Mic*_*ott 4 date-time find date-modified macos

我需要在日期范围内查找文件。我想有一天知道如何做到这一点可能会有所帮助。无论如何。我已经学会了如何使用 -newermt 甚至 -not -newermt;但是,当我组合它们时,我似乎无法得到正确的结果。我找到了使用 find 的 linux 示例,但认为这些开关不适用于 OSX。请参阅以下文件列表:

#$ ls -l
total 32
-rw-r--r--  1 testuser  staff  1024 Oct 26 20:12 test1.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 19:00 test1_old.swc
-rw-r--r--  1 testuser  staff  1024 Oct 26 20:12 test2.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 19:00 test2_old.swc
-rw-r--r--  1 testuser  staff  1024 Oct 26 20:12 test3.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 19:00 test3_old.swc
-rw-r--r--  1 testuser  staff  1024 Oct 26 20:12 test4.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 19:00 test4_old.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 20:11 test5.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 20:13 test6.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 19:00 timestamp.tmp
Run Code Online (Sandbox Code Playgroud)

我希望以下命令通过 test4.swc 返回 test1.swc;但是,请注意它返回了 test6.swc:

#$ find . -newermt '2010-10-26 20:11' -a -not -newermt '2010-10-26 20:13'
./test1.swc
./test2.swc
./test3.swc
./test4.swc
./test6.swc
Run Code Online (Sandbox Code Playgroud)

我认为 -not 条件的分钟已关闭,因此我尝试了以下操作,但没有返回任何内容:

#$ find . -newermt '2010-10-26 20:11' -a -not -newermt '2010-10-26 20:12'
#$ 
Run Code Online (Sandbox Code Playgroud)

我已经得出结论,我没有正确组合 -newermt 和 -not -newermt 开关。关于如何纠正这个问题的任何建议?

Rob*_*kie 5

您的语法看起来正确。你是如何创建这些文件的。仅使用触摸更新日期时,我看到了错误的返回。

在 BSD 中 -a 不是必需的,我倾向于使用 ! 而不是 -not(个人喜好)

搜索命令帮助时记住要搜索 BSD,因为有一些差异我使用FreeBSD 手册页

请记住,文件上有一些超出分钟的内容,因此如果您想要第一秒,则需要对其进行标记。我建立了一个像你这样的结构,当我添加 :00 秒时,我可以获得与你相同的结果,但它没有意义。所以我做了一个 ls -lT 并且能够看到文件上的秒数,然后运行 ​​find 命令并得到我预期的结果,默认情况下看起来像 ! 或 -not 将秒设置为 59,因此它包括整个分钟。

#$ 查找。-newermt "2010-10-26 20:11:00" !-newermt "2010-10-26 20:13:00"