24 24. Comparing Files with diff
diff shows how two files differ, line by line. A listing of the differences is itself called a diff. Reading diffs is a skill you’ll use all the time: Git shows your changes as diffs, and so does Claude Code when you check the edits it made.
24.1 The basic output
Create two versions of a to-do list in the practice files from Files, Folders, and Paths, and compare them:
cd ~/Documents/bash-tutorial-files/documents/notes
printf 'Buy milk\nCall the bank\nEmail Sam\n' > todo-v1.txt
printf 'Buy milk\nEmail Sam\nBook the room\n' > todo-v2.txt
diff todo-v1.txt todo-v2.txtOutput:
2d1
< Call the bank
3a3
> Book the room
(printf is like echo, but \n starts a new line, so one command writes three lines.)
- Lines that start with
<are only in the first file, and lines that start with>are only in the second. - The line above them says where. The number before the letter is a line in the first file, and the number after it is a line in the second.
2d1means “line 2 of the first file was deleted”, and3a3means “after line 3 of the first file, line 3 of the second file was added”. - If the files are the same,
diffprints nothing.
When a line was replaced, you’ll see c (“changed”), with the old and the new version separated by ---:
printf 'apples\nbread\ncheese\n' > old.txt
printf 'apples\nbutter\ncheese\n' > new.txt
diff old.txt new.txtOutput:
2c2
< bread
---
> butter
24.2 The unified format: diff -u
The -u option (“unified”) shows the changes in the format you’ll see most often:
diff -u todo-v1.txt todo-v2.txtOutput (your dates and times will differ):
--- todo-v1.txt 2026-10-17 22:33:27.370537689 -0400
+++ todo-v2.txt 2026-10-17 22:33:27.370537689 -0400
@@ -1,3 +1,3 @@
Buy milk
-Call the bank
Email Sam
+Book the room
- The
---and+++lines name the old and the new file. @@ -1,3 +1,3 @@says which lines are shown: 3 lines starting at line 1 of the old file, and 3 lines starting at line 1 of the new one.- A line that starts with
-was removed, a line that starts with+was added, and a line that starts with a space is unchanged (it’s shown for context).
24.3 Where you’ll see diffs
- Git:
git diffshows what you changed in a project, in the unified format (see Viewing History and Undoing Changes). - Claude Code: the
/diffcommand shows the changes in your project that aren’t committed yet, including the edits Claude has made so far, so you can check them before you commit.
As of Oct 17, 2026 · Official docs: Review changes with /diff
Read the - and + lines carefully: a one-line change can matter a lot.
Sites such as diffchecker.com let you paste two texts and see the differences side by side, in color. They are handy for ordinary text, but whatever you paste is sent to someone else’s server. Never paste passwords, API keys, .env files, private code or personal data into them; use diff (or git diff) on your own computer instead.
Practice Questions
Easy
Question 1 · Multiple choice
In the output of diff first.txt second.txt, what does a line that starts with < mean?
- The line is in both files
- The line is only in
first.txt - The line is only in
second.txt - The line is shorter in
first.txt
Question 2 · True or false
If two files are exactly the same, diff prints nothing.
Question 3 · Short answer
Name two places, besides the diff command, where you’ll see changes shown in the unified (- and +) format.
Medium
Question 4 · Predict the output
old.txt contains the lines apples, bread, cheese. short.txt contains apples, cheese. What does diff old.txt short.txt print?
Question 5 · Predict the output
old.txt contains the lines apples, bread, cheese. new.txt contains apples, butter, cheese. In the output of diff -u old.txt new.txt, which lines start with -, which start with +, and which start with a space?
Question 6 · Short answer
Describe in words what changed:
--- prices-old.txt
+++ prices-new.txt
@@ -1,3 +1,3 @@
Coffee,3.00
-Tea,2.50
+Tea,2.75
Muffin,3.25
Hard
Question 7 · Scenario
Sam wants to see what changed between two versions of the app’s .env.local file and plans to paste both into diffchecker.com. What should Sam do instead, and why?
Question 8 · Scenario
You ask Claude Code to fix a bug on your app’s settings page. When it’s done, you run /diff and see this change among its edits:
- if (user.isAdmin) {
+ if (true) {
showDeleteAllButton();
What does this change do, and what should you do before you commit?
Question 9 · Scenario
Maria edits todo-v2.txt in a text editor, then runs diff todo-v1.txt todo-v2.txt, and it prints nothing, although she’s sure the files should differ. Give two possible reasons.