I want to do gzip of folder, and also want to retain original timestamp for that folder. Can someone suggest any way around it.

i tried below mentioned code. The timestamp which i am getting the time when zip happened.

tar -zcvf outputFileName folderToCompress

2 Answers 2


There's a number of different timestamps on a typical unix filesystem, atime, mtime, ctime. Which one do you want to preserve?

If you are interested in preserving the mtime (which is what is displayed when doing an ls -l), this already works out of the box, as can be seen here:

$ date
Wed Dec  2 08:52:36 CET 2020
$ tar --version
tar (GNU tar) 1.32
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by John Gilmore and Jay Fenlason.

$ find /usr/share/tasksel/ -exec ls -lhad {} +
drwxr-xr-x 3 root root 4.0K Nov  6  2017 /usr/share/tasksel/
drwxr-xr-x 2 root root 4.0K Nov 20 12:21 /usr/share/tasksel/descs
-rw-r--r-- 1 root root 7.9K Nov 14 16:06 /usr/share/tasksel/descs/debian-multimedia-tasks.desc
-rw-r--r-- 1 root root  21K Nov 18 02:40 /usr/share/tasksel/descs/debian-tasks.desc

$ cd /tmp; rm -rf foo.tgz

$ tar czf foo.tgz /usr/share/tasksel/
tar: Removing leading '/' from member names
$ ls -lhan foo.tgz 
-rw-r--r-- 1 1000 1000 4.6K Dec  2 08:54 foo.tgz
$ tar tvf foo.tgz
drwxr-xr-x root/root         0 2017-11-06 09:45 usr/share/tasksel/
drwxr-xr-x root/root         0 2020-11-20 12:21 usr/share/tasksel/descs/
-rw-r--r-- root/root     20596 2020-11-18 02:40 usr/share/tasksel/descs/debian-tasks.desc
-rw-r--r-- root/root      8033 2020-11-14 16:06 usr/share/tasksel/descs/debian-multimedia-tasks.desc
$ tar xf foo.tgz
$ find /tmp/usr/share/tasksel/ -exec ls -lhadn {} + 
drwxr-xr-x 3 1000 1000 4.0K Nov  6  2017 /tmp/usr/share/tasksel/
drwxr-xr-x 2 1000 1000 4.0K Nov 20 12:21 /tmp/usr/share/tasksel/descs
-rw-r--r-- 1 1000 1000 7.9K Nov 14 16:06 /tmp/usr/share/tasksel/descs/debian-multimedia-tasks.desc
-rw-r--r-- 1 1000 1000  21K Nov 18 02:40 /tmp/usr/share/tasksel/descs/debian-tasks.desc

As can be seen, the timestamps of all the folders and files are preserved in the tarball (when running the tar tvf command) and also when extracting with tar xf and examining with ls -l). They are "preserved" insofar, as the (modification) timestamps of the extracted files match the timestamps of the original file ("November 2017 and November 2020), and are different from the current time ("December 2020").

If you get different results, then most likely your methodology to check the timestamps is flawed. Or the filesystem you extract to does not allow modifying timestamps.


If you want the tarball itself to have the same timestamp as the directory you’ve archived, tar doesn’t have a corresponding option, but you can do it after the fact:

touch -r folderToCompress outputFileName

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .