6

I need to archive some files deterministically, so that I get the same archive file if the content of the files is the same. I need this for testing for equality of the archive files later.

however, tar includes the timestamp information, and so I get different archive files even if the content of the files is the same.

how can I create an archive (with tar, zip or something else) that does not include timestamp information?

Note: I am aware that even if the two tar files differ, I can ignore their timestamps and compare only their content with tools such as tar --diff or tarsum. However, I am not allowed to use any other external tool for comparison (because of my setup); I just can test the two archive files for exact equality.

Note: I am aware that I could set the timestamp of all files to a given value before archiving the files, so their timestamps would be the same. However there are a lot of files, and I do not want to do this. I just want to archive these files without timestamp information

example:

$ mkdir copy1
$ touch copy1/file1
$ touch copy1/file2

$ sleep 60
$ mkdir copy2
$ touch copy2/file1
$ touch copy2/file2

$ ls -l copy1
total 0
-rw-r--r--  1 david  wheel  0 Oct 27 00:59 file1
-rw-r--r--  1 david  wheel  0 Oct 27 00:59 file2

$ ls -l copy2
total 0
-rw-r--r--  1 david  wheel  0 Oct 27 01:00 file1
-rw-r--r--  1 david  wheel  0 Oct 27 01:00 file2

# the content of those files is the same; they only differ by the their timestamp    

$ (cd copy1; tar -cvf ../copy1.tar .)
$ (cd copy2; tar -cvf ../copy2.tar .)

$ tar -tvf copy1.tar
drwxr-xr-x  0 david  wheel       0 Oct 27 00:59 ./
-rw-r--r--  0 david  wheel       0 Oct 27 00:59 ./file1
-rw-r--r--  0 david  wheel       0 Oct 27 00:59 ./file2

$ tar -tvf copy2.tar
drwxr-xr-x  0 david  wheel       0 Oct 27 01:00 ./
-rw-r--r--  0 david  wheel       0 Oct 27 01:00 ./file1
-rw-r--r--  0 david  wheel       0 Oct 27 01:00 ./file2

$ diff copy1.tar copy2.tar 
Binary files copy1.tar and copy2.tar differ

I tried with zip -X instead of tar, but I get the same result

3 Answers 3

0

Even if you disable somehow totally the timestamp I wouldn't be 100% sure it would save you in every case. Infact the order of the files could change the results (ie. "tar cf a.tar file1 file2" differs from "tar cf b.tar file2 file1" but by your specification the contents are the same, and the order may be filesystem dependant).

I would suggest that you have to do something more articulate than just a file compare as you said (md5sum and so on).

If you really want just a dumb file compare to work I may suggest a plain shell that sticks the files with a filename header such as:

for i in file1 file2; do echo "$i"; cat $i; done; 

You can of course gzip it if you like. And be carefull to always preserve the ordering.

0

For comparing Zip archives' contents you can use the open-source comp_zip tool @ https://sourceforge.net/projects/unzip-ada/ or https://github.com/zertovitch/zip-ada/ .

The command is comp_zip file1.zip file2.zip; there are switches for verbosity.

0

You can use the option --mtime to set an explicit timestamp:

$ tar --help
...
 Handling of file attributes:
...
      --mtime=DATE-OR-FILE   set mtime for added files from DATE-OR-FILE
...
$ tar --version
tar (GNU tar) 1.29
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://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.

You must log in to answer this question.

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