Text processing
sha256sum
Compute SHA-256 checksums of files, or verify files against a list of checksums with -c.
sha256sum [OPTION...] [FILE...]
sha256sum reads a file and prints its SHA-256 hash: 64 hex digits that change completely if a
single byte of the file changes. Two files with the same hash have the same content, for all
practical purposes. The output format is the hash, two spaces and the file name, and that same
format is what sha256sum -c reads back to verify files.
Use it to check that a download matches the checksum its publisher lists, that a copy or a
backup is intact, or to detect which files changed between two runs. SHA-256 is the current
default for this; md5sum and sha1sum work the same way but are broken for security
purposes. Siblings sha224sum, sha384sum and sha512sum take the same options.
A checksum only proves integrity if you got the expected value from a trustworthy place. A checksum file downloaded from the same server as the file proves nothing against a compromised server; that is what signatures (GPG, minisign, sigstore) are for.
Examples
Hash a file
sha256sum todo.txt
328f70102b0d08bbdea3cbb5b0e70339167d51a8260a023be34668b70f303ffe todo.txt
The 64-character hex digest comes first, then two spaces and the file name.
Hash several files at once
sha256sum photos/*.png
cfe1499051d4b71d94a5ad48ea9e88fd6408f0bfe3965f1ef4725efe5f84066e photos/IMG_2041.png e6a3a3bae8368ec5ca403457c39e5dda8530857e26ee94e4472a4b375dfcee6d photos/beach.png 6a695ca85efe79803094b5f271c24f2427883f4d2603aa899346bd0616fbf7ca photos/forest.png
Each file gets its own line, in the same format, so the output can be saved and checked later.
Save checksums to a file
sha256sum data/*.csv > SHA256SUMS
cat SHA256SUMS
7bbce1a5d4dac19cd33f75bc0ab6b4d8397a05a47b8681f9c0f14a36dd3540bb data/sales.csv a2f17aba5d694218a3a25a9a3ceba05e13b67db432b3ba9edabf409daac7c21a data/users.csv
SHA256SUMS is the conventional name for such a list; Linux distributions publish their ISO checksums the same way.
Verify files against a checksum list
sha256sum data/*.csv > SHA256SUMS
sha256sum -c SHA256SUMS
data/sales.csv: OK data/users.csv: OK
-c recomputes each file's hash and compares it with the list, printing OK for every match.
Detect a modified file
sha256sum data/*.csv > SHA256SUMS
echo '9,Ivan Petrov,ivan@example.com,sales,2026-03-20' >> data/users.csv
sha256sum -c SHA256SUMS
data/sales.csv: OK data/users.csv: FAILED sha256sum: WARNING: 1 computed checksum did NOT match
After a line is appended to users.csv, its hash no longer matches. sha256sum marks it
FAILED, prints a warning summary and exits with status 1.
Report only problems
sha256sum data/*.csv logs/*.log > SHA256SUMS
echo extra >> logs/error.log
sha256sum --quiet -c SHA256SUMS
logs/error.log: FAILED sha256sum: WARNING: 1 computed checksum did NOT match
--quiet suppresses the OK lines, so in a long list only the failures show up.
Use the result in a script
sha256sum todo.txt > todo.sha256
if sha256sum --status -c todo.sha256; then echo "todo.txt is intact"; fi
todo.txt is intact
--status prints nothing at all; the exit status alone says whether every file matched.
Check a download against a published hash
expected=328f70102b0d08bbdea3cbb5b0e70339167d51a8260a023be34668b70f303ffe
echo "$expected todo.txt" | sha256sum -c
todo.txt: OK
When a website lists only the hash, build a one-line checklist yourself: the hash, two
spaces, the file name, piped into sha256sum -c. This beats comparing 64 characters by eye.
Hash a string, not a file
printf 'hello' | sha256sum
echo 'hello' | sha256sum
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 - 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 -
With no file, sha256sum reads standard input and shows - as the name. The two hashes
differ completely because echo adds a newline: always use printf when hashing a value.
Just the hash
sha256sum < todo.txt | cut -d ' ' -f 1
328f70102b0d08bbdea3cbb5b0e70339167d51a8260a023be34668b70f303ffe
Cutting the first field gives the bare digest for a variable or a comparison.
BSD-style output
sha256sum --tag todo.txt
SHA256 (todo.txt) = 328f70102b0d08bbdea3cbb5b0e70339167d51a8260a023be34668b70f303ffe
--tag prints SHA256 (name) = hash, the format used by BSD and macOS tools. sha256sum -c understands both formats.
Ignore files that are not present
sha256sum todo.txt notes.txt > SHA256SUMS
rm notes.txt
sha256sum -c --ignore-missing SHA256SUMS
todo.txt: OK
--ignore-missing skips entries whose files don't exist, which is useful when a published
list covers many files and you downloaded only one.
Common pitfalls
The standard checklist format is the hash, two spaces (or a space and
*) and the name. Recent GNU versions also accept a single space, but older ones and other tools may not, so write two.echo value | sha256sumhashes the trailing newline too. Useprintf '%s' value.File names in a checksum list are relative to where
sha256sumwas run. Run-cfrom the same directory, or the files will be reported as missing.A file name containing a backslash or newline is written with a leading
\and escaped characters. Such lines are correct; don't strip the backslash when parsing.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
macOS has traditionally offered
shasum -a 256 fileinstead, which prints the same format and supports-c. The BSDs havesha256, which prints the BSD style that--tagimitates.
Options worth knowing
A selection, described in our own words. For the full list, see sha256sum(1) or sha256sum --help.
| Option | What it does |
|---|---|
-c, --check | Read checksums from FILE and verify the listed files. |
--quiet | With -c, don't print OK for files that match. |
--status | With -c, print nothing; report only through the exit status. |
--ignore-missing | With -c, skip files that do not exist. |
-w, --warn | With -c, warn about improperly formatted checksum lines. |
--strict | With -c, exit non-zero for improperly formatted lines. |
--tag | Print BSD-style lines: SHA256 (file) = hash. |
-b, --binary | Read in binary mode and mark names with *; no difference on Linux. |
-t, --text | Read in text mode (the default); no difference on Linux. |
-z, --zero | End each output line with NUL instead of newline, and do not escape names. |
Related
md5sumCompute or verify MD5 checksums; fine for spotting accidental changes, not for security.base64Encode binary data as base64 text, or decode base64 back to the original bytes with -d.cmpCompare two files byte by byte and report the first difference, or just whether they differ.diffCompare two files or directories line by line and print the changes that turn one into the other.