I want to create a tar archive which will have files and directories. I need to change the modification time of these files and directories so that their modification timestamp is the current date and time when I'm doing the compression. How may I do that in shell script?

  • 2
    touch all files before archiving them? – Kusalananda Oct 27 '18 at 10:40
  • How can i do that. Can you just tell the command to do that? What i can do on directories – Jasir Bin Siddique Oct 27 '18 at 10:42
  • 1
    It's touch. See man touch for details. – roaima Oct 27 '18 at 10:43

If you have GNU tar, you can use the --mtime option to store arbitrary modification times; in your case,

tar cf archive.tar --mtime="@$(date +%s)" ...

will use the current time for all modification times stored in the archive.

  • appreciate the answer – Jasir Bin Siddique Oct 27 '18 at 11:39
  • This is a good hint for libfind that is used by star and many other programs: it seems to be a good idea to add -chatime, -chctime, -chmtime primaries to modify these values in struct stat. – schily Oct 27 '18 at 22:21

To update the modification timestamp on everything under a directory dir (including the directory itself):

find dir -exec touch {} +

You would do this just before you create your tar archive of the dir directory.

  • Do you really like to shredder all time stamps in the file system? – schily Nov 2 '18 at 17:31
  • @schily Only in the directory that will be archived. – Kusalananda Nov 2 '18 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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