Check if two directory is identical with sha1sum

I want to share a little script I wrote that check if two directory is identical rather efficiently. First you may need to understand what sha1sum does.

The sha1sum tool

As the name may suggest, sha1sum compute the SHA-1 checksum of a particular file / text string.

You can see from the above example that sha1sum produce a hash of the content, by comparing whether the hash is the same, you can tell that the two file / string is identical or not.

Running sha1sum on a directory

The problem is that sha1sum only compute the hash for 1 file at a time. With the help of a few bash tools, we can achieve our needs.

1. To get a list of all files in a directory:

2. To run SHA-1 hash on each and every file:

3. By doing the above, you have an output of the identity hash of all the files in the directory. The next step we have to produce a single hash value for the entire directory so that we can easily compare. One clever trick is to perform a hash again on the output of the above. Thus,

4. That’s pretty much it, but we can still improve on this by sorting the rows before hashing, to ensure the output is the same on different systems.

My “custom” version

Finally, add some checking to check if the arguments are passed

save this as “”

Please find the Gist here. Feel free to give me suggestions to improve this script. Leave a comment below!

1 reply on “Check if two directory is identical with sha1sum”

Why run sha1sum twice? I’ve found that this works great:
find $SOMEDIR -xdev -type f -print0 | LC_COLLATE=C sort -z | xargs -0 cat | md5sum –

Share your thoughts