获取输入日期之前创建的目录中的所有文件名

for*_*une 3 shell timestamps files

我有一个包含大量文件的目录。ls命令的输出是

-rw-r--r-- 1 nobody nogroup    427494 May 26 13:59 14_20150526065238928590_102.txt
-rw-r--r-- 1 nobody nogroup   2113592 May 26 14:00 14_20150526065238928590_105.txt
-rw-r--r-- 1 nobody nogroup    429947 May 26 14:00 14_20150526065238928590_110.txt
-rw-r--r-- 1 nobody nogroup    453831 May 27 14:00 14_20150526065238928590_112.txt
-rw-r--r-- 1 nobody nogroup    551023 May 27 14:01 14_20150526065238928590_118.txt
-rw-r--r-- 1 nobody nogroup     60083 May 27 14:01 14_20150526065238928590_119.txt
-rw-r--r-- 1 nobody nogroup    632324 May 28 03:38 14_20150526065238928590_160.txt
-rw-r--r-- 1 nobody nogroup    735624 May 28 03:38 14_20150526065238928590_161.txt
-rw-r--r-- 1 nobody nogroup   2707507 May 28 03:40 14_20150526065238928590_162.txt
Run Code Online (Sandbox Code Playgroud)

从上面的列表中,我想检索在输入日期之前创建的文件名:

例如:我的输入日期是 20150528,我想要结果:(在 2015 年 5 月 28 日之前创建)

14_20150526065238928590_102.txt
14_20150526065238928590_105.txt
14_20150526065238928590_110.txt
14_20150526065238928590_112.txt
14_20150526065238928590_118.txt
14_20150526065238928590_119.txt
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Joh*_*024 5

查找当前目录及其子目录中最后修改时间早于2015-05-28的所有文件:

find . ! -newermt 20150527
Run Code Online (Sandbox Code Playgroud)

如果您只需要当前目录中的文件而不是其子目录中的文件,请使用:

find . -maxdepth 1 ! -newermt 20150527
Run Code Online (Sandbox Code Playgroud)

这个怎么运作

  • find

    这是 unix 搜索文件时最有用的命令之一。

  • .

    这告诉find开始在当前目录中查找。您可以将其替换为您喜欢的任何目录。

  • !

    这是不合逻辑的:它反转接下来的测试。

  • -newermt 20150527

    这是对修改时间比 2015-05-27 更新的文件的测试。因为前面的!,这个测试是反转,查找文件更新超过2015年5月27日。

    请注意,“更新于 2015-05-27”与“在 2015 年 5 月 28 日之前创建”的含义相同。