realspirituals realspirituals - 4 months ago 9
Linux Question

Why atime is not preserved in tar?

According to the documentation,

tar
is capable of preserving the access time, but when I tried the same, it failed to do so. Can someone please explain this?

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


Create two files with older timestamp

$$$:~/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


Move it to another folder

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


Extract the 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


Result

$$$:~/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

Answer

The referenced man page is confusing. Where it says of --atime-preserve

preserve access times on dumped files [...]

(emphasis added), the current docs say

Preserve the access times of files that are read."

(emphsis added). The word "dumped" refers to putting a file into a tar archive, not to extracting a file to the file system. Thus, the --atime-preserve option is for archiving files without causing the originals' atimes to reflect the access. This is useful in support of making incremental backups (dumps).

Under no circumstances does tar fail to set the atimes of files that it extracts, contrary to your expectation. It certainly cannot give them the same atimes as the originals, for the tar file format does not even record atimes.

Comments