查找目录中的旧文件

查找目录中的旧文件

我正在编写备份轮换脚本,该脚本应该删除所有超过 5 天的文件。问题是我得到了一些奇怪的搜索结果。例如:

[root@spr1-nas01 storage]# date
Wed Feb  8 14:48:09 EET 2017
[root@spr1-nas01 storage]# ll
-rwxr-xr-x. 1 root root  1366884352 Dec 24 02:31 BACKUP_20161224
-rwxr-xr-x. 1 root root    51986944 Jan 28 19:37 BACKUP_20170128
-rwxr-xr-x. 1 root root  9681633280 Jan 31 06:45 BACKUP_20170131

所以我们找到了一些肯定超过 5 天的文件。但我在“查找列表”中没有看到任何文件

[root@spr1-nas01 storage]# find . -ctime  +5 -ls
[root@spr1-nas01 storage]#

有任何想法吗?

答案1

UNIX 中的时间戳有以下区别:

Three times tracked for each file in Unix are these:

    access time – atime
    change time – ctime
    modify time – mtime

atime – File Access Time

Access time shows the last time the data from a file was accessed – read by one of the Unix processes directly or through commands and scripts.
ctime – File Change Time

ctime also changes when you change file's ownership or access permissions. It will also naturally highlight the last time file had its contents updated.
mtime – File Modify Time

Last modification time shows time of the  last change to file's contents. It does not change with owner or permission changes, and is therefore used for tracking the actual changes to data of the file itself.

因此,如果您使用 ctime,您将搜索其数据或所有者/权限自 5 天以来未被修改的文件。

要确定文件的日期,您可以使用以下命令查看不同的时间戳:

# stat fi.le

 Access : 2016-11-14 11:36:21.850314996 +0100
Modif. : 2016-11-10 14:20:25.378246071 +0100
Changt : 2017-02-08 15:00:57.464000000 +0100

比如我改变了fi.le今天的所有者,那么ctime就是今天,mtime停留在2016年。

并且在cat fi.le之后,访问时间也会更新:

# cat fi.le
# stat fi.le

Accès : 2017-02-08 15:03:09.400000000 +0100
Modif. : 2016-11-10 14:20:25.378246071 +0100
Changt : 2017-02-08 15:00:57.464000000 +0100

相关内容