mtree
is a handy utility whenever you need to compare two file hierarchies looking for same/different timestamps or same/different MD5/SHA256 hashes.
Let's set up a simple test hierarchy:
$ mkdir -p folder/to/check
$ mkdir -p folder/to/search
$ for N in $(jot -w %03d 10); do touch `/image-$N.jpg; done
$ for N in $(jot -w %03d 10 2); do touch folder/to/search/image-$N.jpg; done
folder/to/check
has files image-001
through -010
, while folder/to/search
has image-002
through -011
. All files are identical (zero bytes long), but have varying timestamps.
If you only want to check the MD5 hash (and the presence/absence of each file), then:
$ mtree -kmd5 -cp folder/to/check | mtree -p folder/to/search
extra: image-011.jpg
./image-001.jpg missing
The extra
line says that image-011.jpg
was present in folder/to/search
but wasn't required to be (since it isn't in folder/to/check
), and the missing
line says that image-001.jpg
was found in the source folder (folder/to/check
) but not in the target folder (folder/to/search
).
If you want to check the timestamp in addition to the MD5 hash, then:
$ mtree -Kmd5 -cp folder/to/check | mtree -p folder/to/search
.: modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-010.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-008.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-006.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
extra: image-011.jpg
image-007.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-009.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-004.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-003.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-002.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-005.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
./image-001.jpg missing
That output still shows the same extra
and missing
lines, plus a number of files that are identical in MD5 hash, but have different timestamps. Finally, if any of the files has a different MD5 hash than its counterpart, mtree
will tell you which ones:
$ date > folder/to/search/image-007.jpg
$ mtree -Kmd5 -cp folder/to/check | mtree -p folder/to/search
.: modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-010.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-008.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-006.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
extra: image-011.jpg
image-007.jpg:
size (0, 29)
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:25:05 2019)
md5digest (0xd41d8cd98f00b204e9800998ecf8427e, 0x6fd624d18a8fe0c096b19ff858a2acf7)
image-009.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-004.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-003.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-002.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
image-005.jpg:
modification time (Mon Sep 30 16:16:15 2019, Mon Sep 30 16:16:24 2019)
./image-001.jpg missing