11

I have two identical folders, with same structure and contents like this:

folder_1
  hello.txt
  subfolder
    byebye.txt

folder_2
  hello.txt
  subfolder
    byebye.txt

if I compress them as tar.xz formats I get two different archives with two different file sizes (just a few bytes, but they're not identical).

$ cd folder_1 && tar -Jcf archive.tar.xz *
$ cd folder_2 && tar -Jcf archive.tar.xz *

I get:

folder_1/archive.tar.xz != folder_2/archive.tar.xz

and of course if I md5sum or sha1sum them I'll get two different hashes

And that's my problem... I need to check if a provided archive is identical to the one I have in my storage. I cannot use hashing nor just check file sizes.

Using zip instead of tar.xz works as zip always produces identical achives from identical files. Why is this happening? Is there a way to prevent it?

3 Answers 3

11

Ok, the explanation given by ddnomad is correct. It's about the timestamp.

Here is the solution:

add --mtime='1970-01-01 00:00:00' to tar command:

tar --mtime='1970-01-01 00:00:00' -Jcf archive.tar.xz *

This will force contents timestamp to a fixed value thus resulting in identical archives.

3
  • 2
    It looks like you're going for the Unix epoch time, if that's the case then you can just use --mtime=@0. FYI, setting mtime to 1970 like you have done has some ambiguity in it - on my system that results in a Unix time of 25200 (add "UTC" to the string to really get a Unix time of 0).
    – Compholio
    Commented Oct 1, 2021 at 17:55
  • This method may not work for sparse files. I have two huge sparse files, which are verified identical by diff. tar them with -S, the archives are even not with the same size. I will try figure out what the reason is. Do you have any ideas?
    – midnite
    Commented Mar 4, 2022 at 9:35
  • It turns out it is an issue with the sparse files. After fallocate --dig-holes the sparse files, your solution works. Thank you +1. For more information, you may check out my post.
    – midnite
    Commented Mar 5, 2022 at 8:23
4

There are a number of reasons why two tarballs of the same directory tree might differ. The main ones are:

  • Metadata such as ownership, timestamps, etc. may differ. To get a reproducible tar archive, you need to have the same ownership, permissions and timestamps. Make sure that you copied all the metadata (if you have identical file contents with differing metadata, cp -a --attributes-only may help). With GNU tar, there are a few options you can use to ignore certain attributes:

    • --numeric-owner only stores numerical user and group IDs, not names.
    • --owner and --group force files to be recorded under a certain user and group respectively (e.g. --owner=0 --group=0 to record all files as belonging to root).
    • --set-mtime allows you to store all files with a particular timestamp instead of the real one.
  • The order in which the files are stored may differ. Most filesystems don't give any particular guarantee as to the order in which files are listed in a directory, and tar lists them as they come. (You can see the order with ls -U.) GNU tar 1.28 has a new option --sort=name. With older versions or other implementations, you can get a reproducible file order by building a sorted list of file names and passing it to tar:

    find . -print0 | LC_ALL=C sort -z | tar --no-recursion -Jcf ../archive.tar.xz -T -
    

You may be interested in the Debian wiki page on reproducible builds.

1
  • Should we also consider adding --mtime='1970-01-01 00:00:00' along with --mode=a+rwX ?
    – alper
    Commented Jan 24, 2021 at 15:20
3

Every file (folder is a file also) has an embedded time stamp.

I presume you can't create these to folder structures in the same time so time stamps for these files are different.

As the result, archiving or hashing would give you different outcomes as time stamp is a part of file that is used in both operations.

So that's the difference between seemingly identical file structures.

UPDATE: as of checking they have similar contents I guess you have actually to check the contents of these files and compare them.

You must log in to answer this question.

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