Skip to content

Instantly share code, notes, and snippets.

@skratchdot
Created December 9, 2015 11:21
Show Gist options
  • Select an option

  • Save skratchdot/8053931ee7f3e30dec9d to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

good

@YourMJK

YourMJK commented Jun 21, 2023

Copy link
Copy Markdown

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

@skratchdot

Copy link
Copy Markdown
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

gregl83 commented Jun 29, 2023

Copy link
Copy Markdown

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
Copy Markdown
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

gregl83 commented Jun 29, 2023

Copy link
Copy Markdown

@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