Beginner · 8 min · practise every command in the built-in terminal

diff — Comparing Files

Which line changed between two versions of a config? diff tells you exactly.

The files for this lesson are in ~/compare:

ls ~/compare
cat ~/compare/app.conf
cat ~/compare/app.conf.new

Normal output

diff ~/compare/app.conf ~/compare/app.conf.new
3,4c3,4
< workers = 2
< log_level = info
---
> workers = 4
> log_level = debug
5a6
> timeout = 30
  • 3,4c3,4 — lines 3–4 of the first file change into lines 3–4 of the second (a is added, d deleted)
  • < lines are from the first file, > from the second

Unified output: -u

diff -u ~/compare/app.conf ~/compare/app.conf.new
--- /home/student/compare/app.conf	2026-10-30 09:12:44.000000000 +0100
+++ /home/student/compare/app.conf.new	2026-10-30 10:05:13.000000000 +0100
@@ -1,5 +1,6 @@
 # app settings
 port = 8080
-workers = 2
-log_level = info
+workers = 4
+log_level = debug
 cache = on
+timeout = 30

- lines go, + lines come, the rest is context around them. It's the format of git diff and of patches, and the one people send each other:

diff -u ~/compare/app.conf ~/compare/app.conf.new > ~/compare/changes.diff

Just "same or not": -q

diff -q ~/compare/app.conf ~/compare/app.conf.new   # Files ... differ

diff also answers with its exit status — 0 the same, 1 different, 2 trouble — so a script can ask it:

if diff -q ~/compare/app.conf ~/compare/app.conf.new > /dev/null; then echo same; else echo changed; fi

Whole folders: -r

diff -r ~/compare/site-v1 ~/compare/site-v2

Only in names files one folder has and the other doesn't; files in both are compared line by line. diff -qr gives only the names of what's different.

Ignoring what doesn't matter

diff -i a.txt b.txt        # upper/lower case
diff -w a.txt b.txt        # spaces and tabs
diff -b a.txt b.txt        # a different amount of spaces

💡 Before you replace a config with a new one, diff -u old new shows what you're really changing.

Practice tasks

  1. Compare app.conf with app.conf.new
  2. Show the same changes in unified format
  3. Only ask whether the two files differ
  4. Compare the two versions of the site, folder against folder
  5. Save the changes to app.conf as a patch in ~/compare/changes.diff

Commands in this lesson

Reading Files