$_ CLI In Depth
Learning progress 0%
lesson disks mounts archives

Checksums & Integrity: sha256sum

5 min read Module 12: Disks, Mounts & Archives Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:19

What a checksum proves

A cryptographic hash such as SHA-256 maps any input to a fixed-length output of 256 bits, written as 64 hexadecimal digits. The mapping is designed so that two different files almost never produce the same output, and so that you cannot build a file with a chosen output. A matching digest is therefore strong evidence that the bytes are the same. It says nothing about whether the bytes are good, only that they are the bytes whose digest you recorded.

That distinction matters. A checksum stored next to a file on the same disk can be replaced along with the file. A checksum from a trusted source, such as a vendor publishing it on a separate page, is useful because the attacker would need to change both places.

Computing and reading a digest

sha256sum reads each file and prints its digest followed by the name. The format has two spaces between the digest and the name, and a dash in place of the name means standard input. Use printf rather than echo when you need exact bytes, because echo may or may not add a newline depending on the shell.

BASH3 lines
echo hello > msg.txt
sha256sum msg.txt
printf 'hello\n' | sha256sum

The first and last lines print the same digest, because echo hello writes the bytes hello followed by one newline, and the printf form writes the same bytes. The digest is a property of the bytes, not of how you produced them.

Saving and verifying a manifest

Store the digest in a file and check it later with -c. The check reads each named file, computes its digest, and prints OK or FAILED for each line. The command exits with a nonzero status if any file fails, which makes it usable in scripts.

BASH2 lines
sha256sum msg.txt > msg.sha256
sha256sum -c msg.sha256

The second command prints msg.txt: OK. The file name in the manifest is the one used at check time, so run the check from the same directory where you created the manifest, or use paths that resolve the same way.

Detecting a change

To see the check fail, change the file and check again. Appending a single line is enough. The digest changes, and sha256sum -c reports a mismatch with the word FAILED and a nonzero status.

BASH2 lines
echo tampered >> msg.txt
sha256sum -c msg.sha256 || echo "integrity check failed"

The || runs the message only when the check fails. The pattern of a check followed by an action on failure is the basis of most backup and deployment scripts. Keep it in mind for the module on exit codes that comes later.

Warning

Do not use sha256sum -c with a manifest you received from an untrusted copy of the same file and then assume the data is safe. The manifest and the file could both have been replaced. Compare against a digest that came through a separate, trusted channel.

Digesting a whole tree

A manifest for a directory is a list of digests, one per file. Shell globbing produces the file list, and sha256sum takes the list as arguments. Redirecting the output into a file gives you a manifest you can verify later.

BASH5 lines
mkdir -p bundle
printf 'x\n' > bundle/x
printf 'y\n' > bundle/y
sha256sum bundle/* > bundle.sha256
sha256sum -c bundle.sha256

The manifest now has one line for each file in bundle. Verifying it prints one OK line per file. If a file were missing, the check would report it, which is how a manifest protects a backup from silent gaps.

Checksums are not signatures

A digest proves that two sets of bytes are identical. It does not prove who produced them. A signature adds a secret key that only the publisher holds, and verification checks the signature against a published public key. Projects that distribute software usually publish both a digest and a signature, and the signature is the stronger guarantee. A digest alone is enough to catch accidental damage, such as a truncated download, and to confirm that a copy matches its original.

The distinction tells you what to trust. A checksum on the same page as a download protects against transfer errors, but it cannot protect against a page that was changed along with the checksum. A signature made with a key you already trust protects against both, provided the key itself arrived by a reliable route.

Partial damage and what it looks like

Damaged data is not always a whole file that changed. A disk that returns bad blocks can leave a file of the right length with a few wrong bytes in the middle, and a copy that stopped early leaves a file shorter than expected. A digest catches both cases, because it covers every byte. A size check catches only the second, which is why size checks are a weak substitute for a digest.

When one line of a manifest fails, do not assume the others are fine. Run the check on the whole manifest, read every line of the output, and compare the failing file with a known good copy. The check tells you which file failed. Only a comparison tells you how it failed. Keep the damaged copy until you have decided what went wrong, because checking the same damaged file again tells you nothing new.

Hands-on: fingerprint and detect

Create a file, record its digest, change it, and capture the failure in a file. The point of the exercise is the workflow: record a fingerprint when the data is known good, and check the fingerprint whenever the data is used again.

BASH5 lines
echo original > record.txt
sha256sum record.txt > record.sha256
echo changed >> record.txt
sha256sum -c record.sha256 > verify.txt 2>&1 || true
cat verify.txt

The output in verify.txt contains the word FAILED. The redirection with || true keeps the command from ending early, so the report is saved even though the check failed on purpose.

Terminal lab · 3 steps · real Linux in your browser

Fingerprint, detect, and verify

Record a digest, show that a change fails the check, and verify a manifest for several files.

01
Record a digest
Create msg.txt that contains the word hello and one newline. Save its SHA-256 digest and name in msg.sha256 with sha256sum, then verify the manifest. ``bash echo hello > msg.txt sha256sum msg.txt > msg.sha256 sha256sum -c msg.sha256 ``
Show hint for step 1
Redirect sha256sum output into msg.sha256. The check should print msg.txt: OK.
02
Detect a change
Append the word tampered to msg.txt. Run the check again and save its output in verify.txt. The || true keeps the save from ending the command early. ``bash echo tampered >> msg.txt sha256sum -c msg.sha256 > verify.txt 2>&1 || true cat verify.txt ``
Show hint for step 2
After the change, sha256sum -c reports FAILED for msg.txt.
03
Fingerprint several files
Create a directory named bundle with three files, x, y, and z, holding the letters x, y, and z. Write a manifest of all three to bundle.sha256 and verify it. ``bash mkdir -p bundle printf 'x\n' > bundle/x printf 'y\n' > bundle/y printf 'z\n' > bundle/z sha256sum bundle/* > bundle.sha256 sha256sum -c bundle.sha256 ``
Show hint for step 3
A glob such as bundle/* gives sha256sum every file in the directory. Verify with -c.
Check yourself Question 1 of 4
Concept Check Output of a failed check recall

What does sha256sum -c print for a file whose bytes changed since its digest was recorded?

Recap & Key Takeaways

A SHA-256 digest is a fixed-length fingerprint, and any change to the bytes changes the digest.
Use sha256sum FILE to compute a digest and sha256sum -c MANIFEST to verify one, which prints OK or FAILED per file and exits nonzero on failure.
A checksum stored next to the data can be replaced with it, so compare against a digest from a separate trusted source when security matters.
Use printf rather than echo when you need exact bytes in a hashing test, because the bytes are what the digest describes.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue