Verify a Downloaded File's Checksum with sha256sum -c
Verify a download with sha256sum -c SHA256SUMS. A mismatch prints FAILED and exits 1, and a missing file fails too unless you add --ignore-missing.
A release page lists a checksum next to the file, and you want a command that fails loudly when the file is not the one published.
echo "release data" > app.tar.gz
echo "notes" > notes.txt
sha256sum app.tar.gz notes.txt > SHA256SUMS
sha256sum -c SHA256SUMS; echo "status: $?"
echo "tampered" >> app.tar.gz
rm notes.txt
sha256sum -c SHA256SUMS; echo "status: $?"
sha256sum -c --ignore-missing SHA256SUMS; echo "status: $?"app.tar.gz: OK
notes.txt: OK
status: 0
app.tar.gz: FAILED
sha256sum: notes.txt: No such file or directory
notes.txt: FAILED open or read
sha256sum: WARNING: 1 listed file could not be read
sha256sum: WARNING: 1 computed checksum did NOT match
status: 1
app.tar.gz: FAILED
sha256sum: WARNING: 1 computed checksum did NOT match
sha256sum: SHA256SUMS: no file was verified
status: 1sha256sum -c reads lines made of a hash, two spaces (or a space and an asterisk for binary mode) and a file name, recomputes each hash and prints OK or FAILED. The status is 1 if any file differs, which is what a script should test, and a warning line counts the mismatches. The tampered file fails in both later runs. The removed file is an error in the second run, and --ignore-missing skips it in the third, so the report names only the file that really changed. See sha256sum invocation in the coreutils manual.
A checksum fetched from the same server as the file proves only that the download was not damaged. Anyone who can replace the file can replace the sum, so fetch the sum over another channel or verify a signature. --ignore-missing hides a file that should be there, so use it only when the list covers more files than you downloaded. BSD and macOS use shasum -a 256 instead.