Skip to content

Checksums and differences

Module
Archives and Everyday Workflow
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab system), GNU Bash 5.2.21
On this page

Two copies of a note look similar, but one has been edited. You need evidence that their bytes differ and a readable explanation of the edit. By the end, you can use a checksum for the first question and diff for the second.

Finish Creating and inspecting archives and its reset. Use ~/lab from the practice directory setup. Reserve the absent directory ~/lab/b15-compare.

Prepare two copies of a short note:

Terminal window
mkdir ~/lab/b15-compare
cd ~/lab/b15-compare

In your editor, create before.txt with these contents and a final newline:

before.txt
Project notes
status=draft

Save and exit, then copy it and compute both digests:

Terminal window
cp before.txt after.txt
sha256sum before.txt after.txt

A checksum, here a SHA-256 digest, is a fixed-length value computed from a file’s bytes. Sha256sum prints the complete digest followed by the filename. The two filenames differ, but the bytes match, so their digests match. The pathname is not part of the file’s digest.

Open after.txt in your editor. Change only the second line and keep its final newline:

after.txt after editing
Project notes
status=ready

The recording uses printf to write these same versions. Compare them again:

Terminal window
sha256sum before.txt after.txt
diff before.txt after.txt
echo $?

Predict: will the digest identify the changed line number?

Run it, then compare with your guess
Checksums and diff
learner@rootvolt:~/lab$ mkdir ~/lab/b15-comparelearner@rootvolt:~/lab$ cd ~/lab/b15-comparelearner@rootvolt:~/lab/b15-compare$ printf 'Project notes\nstatus=draft\n' > before.txtlearner@rootvolt:~/lab/b15-compare$ cp before.txt after.txtlearner@rootvolt:~/lab/b15-compare$ sha256sum before.txt after.txt684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4  before.txt
684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4  after.txtlearner@rootvolt:~/lab/b15-compare$ printf '%s\n' 'Project notes' status=ready > after.txtlearner@rootvolt:~/lab/b15-compare$ sha256sum before.txt after.txt684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4  before.txt
f23b781fbbc1873bb58a8f90752522272a62f449df2ff5c80e1e1c417270dff9  after.txtlearner@rootvolt:~/lab/b15-compare$ diff before.txt after.txt2c2
< status=draft
---
> status=readylearner@rootvolt:~/lab/b15-compare$ echo $?1learner@rootvolt:~/lab/b15-compare$ rm before.txt after.txtlearner@rootvolt:~/lab/b15-compare$ cd ~/lablearner@rootvolt:~/lab$ rmdir ~/lab/b15-comparelearner@rootvolt:~/lab$ test ! -e ~/lab/b15-compare && printf 'Reset complete\n'Reset completelearner@rootvolt:~/lab$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Matching copies share a digest; editing the status changes the digest, and diff identifies the changed text line before the reset.
Steps (31)
  1. Type mkdir ~/lab/b15-compare
  2. Press Enter — Reserve a directory for the two text versions
  3. Type cd ~/lab/b15-compare
  4. Press Enter — Work where both versions can be named explicitly
  5. Type printf 'Project notes\nstatus=draft\n' > before.txt
  6. Press Enter — Write the original two-line note
  7. Type cp before.txt after.txt
  8. Press Enter — Keep an unchanged original for comparison
  9. Type sha256sum before.txt after.txt
  10. Press Enter — Compute both complete digests before editing
  11. Output: 684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4 before.txt 684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4 after.txt
  12. Type printf '%s\n' 'Project notes' status=ready > after.txt
  13. Press Enter — Replace only the status line in the edited copy
  14. Type sha256sum before.txt after.txt
  15. Press Enter — Compare the unchanged original with the edited copy
  16. Output: 684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4 before.txt f23b781fbbc1873bb58a8f90752522272a62f449df2ff5c80e1e1c417270dff9 after.txt
  17. Type diff before.txt after.txt
  18. Press Enter — Read which text line changed
  19. Output: 2c2 < status=draft --- > status=ready
  20. Type echo $?
  21. Press Enter — A difference is status 1 rather than a command failure
  22. Output: 1
  23. Type rm before.txt after.txt
  24. Press Enter — Remove both disposable versions
  25. Type cd ~/lab
  26. Press Enter — Leave the comparison directory
  27. Type rmdir ~/lab/b15-compare
  28. Press Enter — Remove the empty comparison directory
  29. Type test ! -e ~/lab/b15-compare && printf 'Reset complete\n'
  30. Press Enter — Confirm neither version remains
  31. Output: Reset complete

No. The changed digest shows that the bytes differ, without locating the edit. Diff supplies the line-by-line explanation. The digest and changed diff lines are highlighted in the replay.

The full captured values are repeated here so wrapped checksum rows remain readable:

Version SHA-256 digest
before.txt, unchanged 684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4
after.txt, edited f23b781fbbc1873bb58a8f90752522272a62f449df2ff5c80e1e1c417270dff9

Compare every character when checking digests. A matching beginning is not a matching digest. With exactly these bytes, including newlines, your values match the recording.

Every input byte matters: a changed space, newline style, or final newline changes the digest in practical checks. A different digest proves the files differ. Matching SHA-256 digests give strong evidence of identical bytes, though a finite digest cannot mathematically rule out every collision. For a direct byte-by-byte comparison of two local files, use cmp.

In diff’s normal format, 2c2 says line 2 in the first file changes to line 2 in the second. The line beginning < comes from before.txt; the line beginning > comes from after.txt. The separator is not part of either file. Reversing the argument order reverses which version those markers describe.

The changed lines are status=draft and status=ready. Diff returned status 1 because it found differences. Status 0 means no differences; status 2 indicates trouble comparing the inputs. Echo must immediately follow diff to read its status rather than another command’s status.

Diff is useful for readable text. For an archive or an image, a digest or cmp can test byte identity without pretending there is a useful line-by-line edit.

A checksum is useful only in relation to an expected value. If an attacker can replace both a download and the checksum beside it, a match does not prove who published it. Use a trusted source for the expected digest and verify signatures when the publisher provides them. MD5 appears in older checksum lists, but it is unsuitable for checking authenticity against deliberate tampering; prefer SHA-256 for these exercises.

The replay ends with:

Terminal window
rm before.txt after.txt
cd ~/lab
rmdir ~/lab/b15-compare
test ! -e ~/lab/b15-compare && printf 'Reset complete\n'

Treating a checksum as a description of the edit. Its characters do not encode a line number. Use diff for readable text changes.

Expecting matching appearance to mean matching bytes. Spaces and newline conventions can differ without an obvious visual change. Compare the complete digests.

Goal: Decide whether renaming after.txt without changing its contents should change its SHA-256 digest. Explain which part of sha256sum’s displayed row changes.

Hint

The command reads file contents, then labels the result with a name.

Solution

The digest stays the same. The printed pathname changes. A renamed copy has the same bytes; editing its contents is a different operation.

  • SHA-256 summarizes bytes, including invisible text differences.
  • A different digest proves a difference without locating it.
  • Diff identifies text edits and uses status 1 for differences.

Next, Putting the workflow together prepares and checks a project handoff.