我运行了一个测试,从同一目录创建两个tar(其文件保持不变),然后发现它们的md5sums不同。我假设tar的标头中包含一些时间戳,但是我还没有找到覆盖它的方法。我的操作系统是Ubuntu 9.1。有任何想法吗 ?
谢谢。
我运行了一个测试,从同一目录创建两个tar(其文件保持不变),然后发现它们的md5sums不同。我假设tar的标头中包含一些时间戳,但是我还没有找到覆盖它的方法。我的操作系统是Ubuntu 9.1。有任何想法吗 ?
谢谢。
Answers:
正如丹尼斯在上面指出的那样,它是gzip。gzip标头的一部分是用于压缩文件中任何内容的mod时间。如果需要gzip,则可以将tarfile压缩为tar之外的一个额外步骤,而不是使用tar的内部gzip。gzip命令具有一个标志,以禁止保存该修改时间。
tar -c ./bin |gzip -n >one.tgz
tar -c ./bin |gzip -n >two.tgz
md5sum one.tgz two.tgz
这不会影响tarfile中的时间,只会影响gzip标头中的时间。
GZIP=-n tar -cz ...
                    我也有这个问题,要使gzip不更改时间戳,请使用 gzip -n
-n,--no-name不保存或恢复原始名称和时间戳
[valter.silva@alog ~]$ gzip --help
Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).
Mandatory arguments to long options are mandatory for short options too.
  -c, --stdout      write on standard output, keep original files unchanged
  -d, --decompress  decompress
  -f, --force       force overwrite of output file and compress links
  -h, --help        give this help
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and time stamp
  -N, --name        save or restore the original name and time stamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
  -S, --suffix=SUF  use suffix SUF on compressed files
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better
    --rsyncable   Make rsync-friendly archive
With no FILE, or when FILE is -, read standard input.
Report bugs to <bug-gzip@gnu.org>.
例:
[valter.silva@alog ~]$ ls
renewClaroMMSCanaisSemanal.log.gz  s3
[valter.silva@alog ~]$ gunzip renew.log.gz 
[valter.silva@alog ~]$ gunzip s3/renew.log.gz 
[valter.silva@alog ~]$ md5sum renew.log 
d41d8cd98f00b204e9800998ecf8427e  renew.log
[valter.silva@alog ~]$ md5sum s3/renew.log 
d41d8cd98f00b204e9800998ecf8427e  s3/renew.log
[valter.silva@alog ~]$ gzip -n renew.log 
[valter.silva@alog ~]$ gzip -n s3/renew.log 
[valter.silva@alog ~]$ md5sum renew.log.gz 
7029066c27ac6f5ef18d660d5741979a  renew.log.gz
[valter.silva@alog ~]$ md5sum s3/renew.log.gz 
7029066c27ac6f5ef18d660d5741979a  s3/renew.log.gz
在其他答案使我失败之后,我掉进了一个兔子洞,设法弄清楚我的tar版本(openSUSE 42.3 OSS回购中的1.27.1)pax默认情况下使用的是非确定性存档格式,这意味着即使如果不进行压缩,则使用tar从同一文件创建的归档文件(甚至显式设置mtime)会有所不同:
$ echo hi > test.file
$ tar --create --to-stdout test.file # long form of `tar cO test.file`
./PaxHeaders.13067/test.file0000644000000000000000000000013213427447703012603 xustar0030 mtime=1549684675.835011178
30 atime=1549684726.410510251
30 ctime=1549684675.835011178
test.file0000644000175000001440000000000313427447703013057 0ustar00hartusers00000000000000hi
$ tar --create --to-stdout test.file
./PaxHeaders.13096/test.file0000644000000000000000000000013213427447703012605 xustar0030 mtime=1549684675.835011178
30 atime=1549684726.410510251
30 ctime=1549684675.835011178
test.file0000644000175000001440000000000313427447703013057 0ustar00hartusers00000000000000hi
请注意,即使不使用压缩,上述输出也会有所不同;未压缩的存档内容(通过在同一内容上运行tar两次而生成)不同,因此即使GZIP=-n用作其他答案,压缩后的内容也将有所不同
--format gnu:$ tar --create --format gnu --to-stdout test.file
test.file0000644000175000001440000000000313427447703011557 0ustar  hartusershi
$ tar --create --format gnu --to-stdout test.file
test.file0000644000175000001440000000000313427447703011557 0ustar  hartusershi
这与上面有关gzip的建议配合使用:
# gzip refuses to write to stdout, so we'll use the `-f` option to create a file
$ GZIP=-n tar --format gnu -czf test.file.tgz test.file && md5sum test.file.tgz
0d8c7b3bdbe8066b516e3d3af60ade75  test.file.tgz
$ GZIP=-n tar --format gnu -czf test.file.tgz test.file && md5sum test.file.tgz
0d8c7b3bdbe8066b516e3d3af60ade75  test.file.tgz
# without GZIP=-n we see a different hash
$ tar --format gnu -czf test.file.tgz test.file && md5sum test.file.tgz
682ce0c8267b90f4103b4c29903c5a8d  test.file.tgz
但是,除了有充分的理由更倾向于使用更好的压缩格式而不是gzip之外,您可能还想考虑使用xz(tar也支持使用--xz或-J标志而不是-z),因为这样做可以为您节省很多时间;的默认行为xz是在未压缩的内容相同时生成相同的压缩输出,因此无需指定以下选项GZIP=-n:
$ tar --format gnu --xz -cf test.file.txz test.file && md5sum test.file.txz 
dea99037d4b0ee4565b3639e93ac0930  test.file.txz
$ tar --format gnu --xz -cf test.file.txz test.file && md5sum test.file.txz 
dea99037d4b0ee4565b3639e93ac0930  test.file.txz
touch filename更改文件的修改时间也足以更改校验和。