Get the MD5 Hash of a File with OpenSSL
This is a fairly compatible way that works on most distros of Linux and macOS without needing extra dependencies.
You can run openssl md5 "${file}" | cut -d " " -f 2
. Without cut
it will
return MD5(demo)= b1946ac92492d2347c6235b4d2611184
, cut
parses out just the
hash.
There are other solutions to solve this problem but they typically require installing third party packages.
I needed to get the MD5 hash of a YAML file in a script to trigger a Kubernetes rollout when a specific annotation in a deployment config changed. The above did the trick.
# Demo Video
Timestamps
- 0:16 – Parsing out just the hash with cut
When was the last time you needed to get the hash of a file? Let me know below!