为什么 tar 中没有保留 atime?

Why atime is not preserved in tar?

根据documentationtar可以保留访问时间,但是当我尝试同样的方法时,它没有这样做。有人可以解释一下吗?

$$$:~/user1/testtar/source> tar --version
tar (GNU tar) 1.15.1

创建两个时间戳较早的文件

$$$:~/user1/testtar/source> touch -t "201501010101" a.txt
$$$:~/user1/testtar/source> touch -t "201501010101" b.txt

$$$:~/user1/testtar/source> ls -ltu
-rw-r--r-- 1 usr usr 0 2015-01-01 01:01 a.txt
-rw-r--r-- 1 usr usr 0 2015-01-01 01:01 b.txt

将其移动到另一个文件夹

$$$:~/user1/testtar/source> tar --atime-preserve -cvpf archive.tar *
$$$:~/user1/testtar/source> mv archive.tar ../target/
$$$:~/user1/testtar/source> cd ../target/

提取tar

$$$:~/user1/testtar/target> tar --atime-preserve -xvpf archive.tar
a.txt
b.txt

$$$:~/user1/testtar/target> ls -lt
total 12
-rw-r--r-- 1 usr usr 10240 2016-07-07 15:55 archive.tar
-rw-r--r-- 1 usr usr     0 2015-01-01 01:01 a.txt
-rw-r--r-- 1 usr usr     0 2015-01-01 01:01 b.txt

结果

$$$:~/user1/testtar/target> ls -ltu
-rw-r--r-- 1 usr usr 10240 2016-07-07 15:56 archive.tar
-rw-r--r-- 1 usr usr     0 2016-07-07 15:56 a.txt
-rw-r--r-- 1 usr usr     0 2016-07-07 15:56 b.txt

引用的手册页令人困惑。它说 --atime-preserve

preserve access times on dumped files [...]

(强调),the current docs

Preserve the access times of files that are read."

(强调)。 "dumped" 一词是指将文件放入 tar 存档中,而不是将文件提取到文件系统。因此,--atime-preserve 选项用于归档文件而不会使原件的时间反映访问。这对于支持进行增量备份(转储)很有用。

在任何情况下,tar 都不会未能设置它提取的文件的时间,这与您的预期相反。它当然不能给他们与原件相同的时间,因为 tar 文件格式甚至不记录时间。

您可以在 tar format specification 中观察到没有 atime 字段。这只是为了保留原始文件的访问时间 tar-ed.

我觉得这个话题已经过时了,完全相同的步骤,我得到了以下结果。

tar --version
tar (GNU tar) 1.28

mkdir -p source target
touch -t "201501010101" source/a.txt
touch -t "201501010101" source/b.txt
tar -cvpf archive.tar -C source  .
tar -xvpf archive.tar -C target/

ls -l target/
total 16
-rw-rw-r-- 1 usr usr 0 Jan  1  2015 a.txt
-rw-rw-r-- 1 usr usr 0 Jan  1  2015 b.txt