This question already has an answer here:

I found an old archive filled with old folders on my pc. I would like to know how to make a copy of this archive, but keeping everything intact, including their date of creation

marked as duplicate by muru command-line Oct 20 at 9:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    Note that most Linux filesystems don't store dates of creation — the best you can have is date of last modification. – Ruslan Oct 19 at 15:51
  • @Ruslan most do, it's just hasn't been accessible via kernel APIs until the statx call got merged. And even then, I don't think the creation time can be set manually. – muru Oct 19 at 20:20
  • 2
    @muru so, we can't actually copy a file preserving its date of creation in the copy, can we? If we can't, then this attribute is useless: it's then the date of creation of the inode, not of the document. – Ruslan Oct 19 at 22:06

Read man cp, and, in addition to the --recursive switch use one of:

   -p     same as --preserve=mode,ownership,timestamps

   --preserve[=ATTR_LIST]
          preserve the specified attributes (default: mode,ownership,timestamps), if possible
          additional attributes: context, links, xattr, all

As indicated in the other answers, you need to use the terminal if you want to preserve every file attribute. However, I would suggest to use the -a (--archive) option with the cp command, which is specifically aimed at creating an identical archive copy.

cp -a <source> <destination>

The same can be achieved with the rsync utility, the local and remote file copy tool that may perform faster than cp. It also uses the option -a for the same purpose.

rsync -a <source> <destination>

For example, to copy a folder Archive in your home folder to an external USB drive mounted under /media/$USER/USB_drive:

cp -a /home/$USER/Archive /media/$USER/USB_drive/

or

rsync  -a /home/$USER/Archive /media/$USER/USB_drive/
  • Both commands will create a folder Archive containing all your subfolders and files in the existing destination folder /media/$USER/USB_drive/.
  • You can find where your USB drive is mounted in the output of the command mount | grep /media
  • You can leave $USER in place if it is for your current user. This variable is automatically substituted by your login name.
  • Beware: if you use tab completion, you may unthinkingly leave a trailing slash on the source directory, which will do quite a different action. (Specifically, it would copy all the contents of /home/$USER/Archive/ into /media/$USER/USB_drive/ rather than making a new subdirectory.) The commands as shown are correct; I just want to emphasize that adding a trailing slash to the source is not equivalent. – Wildcard Oct 22 at 18:10
sudo cp -rp /home/my_home /media/backup/my_home

Or

sudo cp -a /home/my_home /media/backup/my_home

This should do it.

  • -r is recursive

  • -p is preserve-mode

There is no easy way to get or set the creation time of a file in Linux. If that absolutely must be preserved, for which I don't see much point, you can fake the system time (using, for example, the aptly named faketime command) when copying the files, but you'd need to copy each entry individually, so that the time can be set for each file correctly. Even then, there might be some inaccuracy. Since getting the creation time itself is a chore, and since time can be faked pretty easily, but probably not exactly, there really isn't much point to doing so, however.

  • I think the OP just wants to have the output of ls -l show the same timestamps for source and target (without actually knowing or being interested in what those timestamps precisely refer to). Yet +1 for explaining the background. – PerlDuck Oct 19 at 20:48
  • @PerlDuck in that case it would be a dupe of askubuntu.com/questions/1040885/… or other such questions – muru Oct 19 at 21:20
  • Which I think it is. – PerlDuck Oct 20 at 8:13

simply copying an archive file won't change the created dates of the files/folders in the archive

  • 2
    Likely the OP is referring to an old archive folder, not to an archive file – vanadium Oct 19 at 14:09

While the other answers are correct, you may want to add the file to .zip archive (maybe in addition to plain copy if it's not huge), as it will preserve timestamps better in the long term (given enough time, you data will be copied around to various storage media, restored from backups, put to and from the cloud etc. and file metadata like timestamps will eventually be lost. Being inside ZIP archive will protect it.)

Not the answer you're looking for? Browse other questions tagged or ask your own question.