How to create a tar file that omits timestamps for its contents?

后端 未结 3 1737
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 05:42

Is there a way to create a .tar file that omits the values of atime/ctime/mtime for its files/directories?

Why do we want to do this?

We have a st

相关标签:
3条回答
  • 2021-02-05 05:54

    GNU tar has a --mtime argument, which can be used to store a fixed date in the archive rather than a file's actual mtime:

    tar --mtime='1970-01-01' input ...
    

    When compressing a tarball with gzip, it's also necessary to specify -n to prevent name and timestamp of the tar archive from being stored:

    tar --mtime='1970-01-01' input ... | gzip -n >input.tar.gz
    
    0 讨论(0)
  • 2021-02-05 06:10

    To have a truly idempotent tar, mtime is a good step but not enough. You also need to set the sort order, the owner and group (together with their mapping) and a proper timezone for mtime (since otherwise you're gonna have issues as well between Mac and Linux).

    I ended up with

    tar --sort=name --owner=root:0 --group=root:0 --mtime='UTC 2019-01-01' ... | gzip -n
    
    0 讨论(0)
  • 2021-02-05 06:13

    We can create the tar file without timestamp appending to it.

    1) Create tar file without compression and timestamp

    tar -cf file_name.tar file_name
    

    2) Create tar file with compression and without timestamp

    tar -cf file_name | gzip -n > file_name.tar.gz
    

    3) Alternate way

    GZIP=-n tar -czf file_name.tar.gz file_name
    

    Note: '-n' will not add timestamp and name

    4) To extract tar file

    tar -xf file_name.tar and tar -xzf file_name.tar.gz
    

    To verify you can do like this

    file file_name.tar or file_name.tar.gz
    output = file_name.tar.gz: gzip compressed data, from Unix
    
    0 讨论(0)
提交回复
热议问题