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.txt

Output:

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. 2d1 means “line 2 of the first file was deleted”, and 3a3 means “after line 3 of the first file, line 3 of the second file was added”.
  • If the files are the same, diff prints 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.txt

Output:

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.txt

Output (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 diff shows what you changed in a project, in the unified format (see Viewing History and Undoing Changes).
  • Claude Code: the /diff command 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.

Websites that compare text

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?

  1. The line is in both files
  2. The line is only in first.txt
  3. The line is only in second.txt
  4. The line is shorter in first.txt

b. < points to the first (left) file, > to the second. Lines that are in both files aren’t shown at all in this format (a), lines only in second.txt start with > (c), and diff doesn’t compare line lengths (d).

Question 2 · True or false

If two files are exactly the same, diff prints nothing.

True. diff only lists differences, so for identical files it prints nothing.

Question 3 · Short answer

Name two places, besides the diff command, where you’ll see changes shown in the unified (- and +) format.

git diff, which shows what you changed in a Git project, and Claude Code’s /diff command, which shows the uncommitted changes in your project, including the edits Claude made.

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?

2d1
< bread

Line 2 of old.txt (bread) was deleted; after that, the files line up at line 1 of short.txt.

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?

  • -bread: removed (it’s only in the old file).
  • +butter: added (it’s only in the new file).
  • apples and cheese start with a space: they are unchanged and shown for context.

The output also starts with the ---/+++ lines naming the two files and an @@ -1,3 +1,3 @@ line.

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

The price of tea went up from 2.50 to 2.75. A changed line appears as the old version (-) followed by the new version (+). Coffee and muffin are unchanged; they are shown only for context.

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?

Compare the files locally, for example diff -u old.env.local .env.local. An .env.local file holds secret keys, and anything pasted into a website is sent to someone else’s server, where it may be stored or seen by others. Comparison websites are fine for ordinary text, never for secrets, private code or personal data. (If a key has already been pasted somewhere public, treat it as leaked and replace it.)

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?

Before, the “delete all” button was shown only to administrators. After the change, the condition is always true, so every user sees it. That’s probably not part of the bug fix and could be dangerous. Don’t commit it as is: ask Claude Code why it made this change, and have it fix the bug without removing the admin check. This is why you read every - and + line.

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.

Some possibilities:

  • She hasn’t saved the file in the editor yet, so the file on disk hasn’t changed.
  • She edited a different todo-v2.txt (for example, one in another folder), not the one in the current folder.
  • The two files really are the same: for example, she made the same change in both, or copied one over the other.

Because diff prints nothing only when the files are identical, the files diff compared must be the same.

© 2025-2026 Y. Rosenthal. All rights reserved.