Skip to content

Instantly share code, notes, and snippets.

@skratchdot
Created December 9, 2015 11:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save skratchdot/8053931ee7f3e30dec9d to your computer and use it in GitHub Desktop.
Save skratchdot/8053931ee7f3e30dec9d to your computer and use it in GitHub Desktop.
Get the shasum hash of an entire directory (recursively)
#!/bin/sh
find ./src -type f -print0 | sort -z | xargs -0 shasum | shasum
@crazygit
Copy link

good

@YourMJK
Copy link

YourMJK commented Jun 21, 2023

Use LC_ALL=C sort -z if you want consistent sort order across different locales/environment settings.

@skratchdot
Copy link
Author

@YourMJK - good call! i guess this is better:

#!/bin/sh
find ./src -type f -print0 | LC_ALL=C sort -z | xargs -0 shasum | shasum

@gregl83
Copy link

gregl83 commented Jun 29, 2023

Stumbled across this while doing some benchmarks for a Rust crate I wrote to hash directories.

If a package installation isn't too much, give paq a try.

@skratchdot
Copy link
Author

@gregl83 - nice! thanks for adding this link / sharing your project. it looks pretty cool (and i like the explanation in the "how it works" section).

@gregl83
Copy link

gregl83 commented Jun 29, 2023

@skratchdot np, maybe it's helpful for another person!

There are trade-offs. The original solution should work on nix systems without requiring additional packages.

You might consider explicitly using SHA256 in your shasum invocations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment