Hash of a Directory

serhatteker

Serhat Teker

Posted on October 20, 2021

Hash of a Directory

In linux it's easy and straightforward to find checksum of a file:

$ sha256sum /path/to/file/filename

3c6f25f33bf06d7f9e5f9bb0232a2b1a694acdaa9d27c9a0dffefb827a00773e  filename
Enter fullscreen mode Exit fullscreen mode

Finding a directoy's hashsum with bash is a little bit tricky1 though:

$ find /somepath -type f -print0 | sort -z | xargs -0 sha256sum | sha256sum
Enter fullscreen mode Exit fullscreen mode

Explanation of flags

  • find /somepath -type f means find all files in /somepath

output
hugo/metrics/metrics.go
hugo/metrics/metrics_test.go
hugo/hugofs/fileinfo.go
hugo/hugofs/filter_fs.go
hugo/hugofs/fs.go
hugo/hugofs/createcounting_fs.go
hugo/hugofs/rootmapping_fs_test.go
hugo/hugofs/decorators.go
hugo/hugofs/hashing_fs_test.go
hugo/hugofs/stacktracer_fs.go
hugo/hugofs/slice_fs.go
hugo/hugofs/filter_fs_test.go
hugo/hugofs/language_composite_fs.go
hugo/hugofs/files/classifier_test.go
hugo/hugofs/files/classifier.go
hugo/hugofs/glob/glob.go
hugo/hugofs/glob/glob_test.go
hugo/hugofs/fs_test.go
hugo/hugofs/hashing_fs.go
hugo/hugofs/nosymlink_test.go
hugo/hugofs/nosymlink_fs.go
hugo/hugofs/glob.go
hugo/hugofs/noop_fs.go
hugo/hugofs/walk_test.go
hugo/hugofs/walk.go
Enter fullscreen mode Exit fullscreen mode

  • -print0 removes all headheaches from having spaces or special characters in filenames:

output
hugo/metrics/metrics.go./hugo/metrics/metrics_test.go./hugo/hugofs/fileinfo.go./hugo/hugofs/filter_fs.go./hugo/hugofs/fs.go./hugo/hugofs/createcounting_fs.go./hugo/hugofs/rootmapping_fs_test.go./hugo/hugofs/decorators.go./hugo/hugofs/hashing_fs_test.go./hugo/hugofs/stacktracer_fs.go./hugo/hugofs/slice_fs.go./hugo/hugofs/filter_fs_test.go./hugo/hugofs/language_composite_fs.go./hugo/hugofs/files/classifier_test.go./hugo/hugofs/files/classifier.go./hugo/hugofs/glob/glob.go./hugo/hugofs/glob/glob_test.go./hugo/hugofs/fs_test.go./hugo/hugofs/hashing_fs.go./hugo/hugofs/nosymlink_test.go./hugo/hugofs/nosymlink_fs.go./hugo/hugofs/glob.go./hugo/hugofs/noop_fs.go./hugo/hugofs/walk_test.go./hugo/hugofs/walk.go
Enter fullscreen mode Exit fullscreen mode

  • sort -z: we are sorting with -z flag. -z or --zero-terminated means:
    line delimiter is NUL, not newline

  • with xargs -0 sha256sum we interpreted -print0 output with -0 (--null) flag
    and execute sha256sum command:

output
73185483c784e76a7bfb91295fad4859101304efc0757a78e913cd2ebbe16cd4  hugo/benchbep.sh
1c8cae727c7518c6d92eb62d56630af23d36b4c3743271f2040bf82befaafaa4  hugo/bench.sh
8a0b3d50f7630eea822d0ac6dc12f283abc55c80328d43323861bdb77a062a77  hugo/benchSite.sh
bebc05f6c2511a964a61ec1d022ddd2caaf2755f9e9ca158006f1a545044d032  hugo/bepdock.sh
2e2b6c7fc1ff73e17eae698224b72acbf27d3c6db8378a069824d870638d5b96  hugo/bufferpool/bufpool.go
3b035bf75ef31fc7bc0115bce5549fcb4d0071191843f8be3528b993a1959d8d  hugo/bufferpool/bufpool_test.go
8ef49137a2d9671d6f57a8452a199cb14ad47654dafdf88ad0016f957cb4e3d3  hugo/cache/filecache/filecache_config.go
d696c1f65b2b30307bf06d1dfedceb4c147352c95874e8685c994065392d074a  hugo/cache/filecache/filecache_config_test.go
4b6f2148aad5f29bfa275e0ed40e16c2789e0fcaf436b7fc64cf540c14d17895  hugo/cache/filecache/filecache.go
c37ca9b578f07ee38642b98775a1b622f082e20d3bad79a1f09adbaa156e8ad3  hugo/cache/filecache/filecache_pruner.go
431f3aa42fd296a7163bcbcaf4ada17e00d9f8940a5ebd8f8f20f79529189c52  hugo/cache/filecache/filecache_pruner_test.go
b1135bb572e330a1b2619a8eafa81e5f3b1d7e1c2c2d7e1d7ae266e1f7543f9f  hugo/cache/filecache/filecache_test.go
b78985d510c1d617daffe48c571310ef82c0aaa31bd28c15ce13602baf961ef1  hugo/cache/namedmemcache/named_cache.go
645d56dca7355b2bcf5c2df322810c214db8bf5c44359c81e70cced2fbce85c0  hugo/cache/namedmemcache/named_cache_test.go
bb3f9169a05369de2dc4571902e536b0a11fced490e48fb22fbc01363c2b553e  hugo/.circleci/config.yml
3b7b8ba156730e0f16c1fb64f3758c92c7e82c00c6cb5e43ed811b243c455ff8  hugo/codegen/methods2_test.go
43fdf285083f13146b307699ae0eb4b5e5d85ce354a19fa69af9c9f8fb644720  hugo/codegen/methods.go
23d780ad3a8725a514e03a51fb10e654023cacb5f8f27af96f81e6feecf469cc  hugo/codegen/methods_test.go
5cd6a0e2a7084ce2c388d22a4db605d72a3814100570aa43a3b48c7e8d13b560  hugo/commands/check_darwin.go
8d2a6c0c456b8fad57c3e2c8e4155b7085ad63bf85b6635e1e27706d8ea931e5  hugo/commands/check.go
44e17d4657ac1d19b33392641473b2b13f1347073b3bb610ce638fc050f815a5  hugo/commands/commandeer.go
bed981879e1daf007e49a9882a0276527980366694db09e3c3e3342541a5a723  hugo/commands/commands.go
5b97aaecb4c8cb9f08c32e89b0f22bf38ff5c7b6b37b2ced1668472f436c5cd1  hugo/commands/commands_test.go
e1f49c774c5d1d76f52049e3094c6f264574b18b780fa06a89b77680a783881b  hugo/commands/config.go
15000952b0fd581b9056b7cea4ccd1262ffda232685b44b521088298c4ac5ad7  hugo/commands/convert.go
Enter fullscreen mode Exit fullscreen mode

  • and with the last pipe | sha256sum we hashing all hash values and filenames together:
48b2ca42fe22bf180d25248396df431408a74344b85539fc23c55f0c5fcc356e  -
Enter fullscreen mode Exit fullscreen mode

All done!


  1. https://superuser.com/questions/458326/sha1sum-for-a-directory-of-directories/1316125#1316125 

💖 💪 🙅 🚩
serhatteker
Serhat Teker

Posted on October 20, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related