前景

生产环境指定时间删除图片

实现

查看目录下的文件

[root@bk bin]# ls |wc -l
827
[root@bk bin]# ll|head -9
total 379524
-rwxr-xr-x.   1 root root      41488 Aug 20  2019 [
-rwxr-xr-x.   1 root root     107848 Feb  3  2021 a2p
-rwxr-xr-x.   1 root root      52640 Mar 24  2022 ab
-rwxr-xr-x.   1 root root      29104 Oct  2  2020 addr2line
-rwxr-xr-x.   1 root root         29 Apr  1  2020 alias
lrwxrwxrwx.   1 root root          6 Mar 30  2022 apropos -> whatis
-rwxr-xr-x.   1 root root      62680 Oct  2  2020 ar
-rwxr-xr-x.   1 root root      33080 Aug 20  2019 arch

在上面的例子中,目录中共计有 827 个文件,每天的文件个数是不确定的,可能是几个、几十个或者上百个。刚好,你需要确定某一天的文件个数呢?你会怎么办呢?

使用 find 命令是满足需求的,因为 find 命令的时间筛选都是基于范围的,你只能用它查找以当前时间为起点,若干个小时之内(之外),或者若干天之内(之外)的文件。而需要精确查找某一天的文件,它是无法达到要求的。

[root@bk bin]# find . -type f -mtime -200 -exec ls -lt {} \;
# 基于 find 命令输出,无法精确筛选某一天的文件
-rwxr-xr-x. 1 root root 21508168 Nov  3 00:19 ./ctr
-rwxr-xr-x. 1 root root 14485560 Nov  3 00:19 ./runc
-rwxr-xr-x. 1 root root 59580232 Aug 26  2022 ./containerd
-rwxr-xr-x. 1 root root 7270400 Nov  3 00:19 ./containerd-shim
-rwxr-xr-x. 1 root root 9547776 Aug 26  2022 ./containerd-shim-runc-v1
-rwxr-xr-x. 1 root root 9953280 Nov  3 00:19 ./containerd-shim-runc-v2
-rwxr-xr-x. 1 root root 93 Nov  3 03:36 ./jmsctl
-rwxr-xr-x. 1 root root 13924 Oct 26 01:58 ./dockerd-rootless-setuptool.sh
-rwxr-xr-x. 1 root root 5150 Oct 26 01:58 ./dockerd-rootless.sh
-rwxr-xr-x. 1 root root 11523408 Oct 26 02:10 ./rootlesskit
-rwxr-xr-x. 1 root root 7353112 Oct 26 02:10 ./rootlesskit-docker-proxy
-rwxr-xr-x. 1 root root 500096 Sep  1 22:58 ./rsync

也许我们可以结合ls、stat、awk、grep命令,就像下面这样:

[root@bk bin]# ls|xargs stat |grep Modify|awk '{print $2}'|grep 2014-06-10|wc -l
39

对比上面的输出,应该说组合命令达到了我们的预期要求。但是,有没更简单的方法呢?答案就是:ls -lt –time-style=long-iso,输出如下:

ls -l --time-style=long-iso|grep 2014-06-10

使用 ls 筛选_docker