TExcelTAD
Home › Linux & Bash › diff: Compare Files

diff: Compare Files

diff compares two files line by line and prints the changes needed to turn the first into the second. It's how you review what changed in a config file, check that a script's output matches what you expect, and create patches. Git's git diff uses the same ideas.

The default (normal) format

bash · try it yourself
cat > before.txt <<'TXT'
apples
bread
milk
eggs
TXT
cat > after.txt <<'TXT'
apples
bread
oat milk
eggs
coffee
TXT
diff before.txt after.txt
echo "exit status: $?"
3c3
< milk
---
> oat milk
4a5
> coffee
exit status: 1

Read it as instructions:

  • 3c3 means "line 3 of the first file changes into line 3 of the second". Lines starting with < are from the first file, > from the second.
  • 4a5 means "after line 4 of the first file, add line 5 of the second".
  • d would mean delete.

The exit status is part of the answer: 0 = identical, 1 = different, 2 = trouble (for example a missing file).

Unified format: diff -u

The format you'll see in code reviews, Git and patch files shows changes in context, with - for removed lines and + for added ones:

bash · try it yourself
cat > config.old <<'TXT'
server {
    listen 80;
    server_name shop.example.com;
    root /var/www/shop;
}
TXT
cat > config.new <<'TXT'
server {
    listen 443 ssl;
    server_name shop.example.com;
    root /var/www/shop;
    ssl_certificate /etc/ssl/shop.pem;
}
TXT
diff -u config.old config.new | tail -n +3
@@ -1,5 +1,6 @@
 server {
-    listen 80;
+    listen 443 ssl;
     server_name shop.example.com;
     root /var/www/shop;
+    ssl_certificate /etc/ssl/shop.pem;
 }

The first two lines of diff -u output name the files with their modification times; tail -n +3 skips them here because times change every run. @@ -1,5 +1,6 @@ says "this hunk covers 5 lines starting at line 1 in the old file and 6 lines starting at line 1 in the new one". Unchanged lines (starting with a space) give context.

Useful options

Option Effect
-u Unified format
-y Side by side (--width=N to fit your screen)
-q Only say whether the files differ
-i Ignore case
-w Ignore all whitespace
-B Ignore blank lines
-r Compare directories recursively
--color Colour the output in a terminal
bash · try it yourself
printf 'name: shop\nport: 8080\n' > a.yml
printf 'name:   shop\nport: 8080\n\n' > b.yml
diff -q a.yml b.yml
diff -wB a.yml b.yml && echo "same, apart from whitespace"
diff -y --width=40 a.yml b.yml
Files a.yml and b.yml differ
same, apart from whitespace
name: shop	   |	name:   shop
port: 8080		port: 8080
		   >

In side-by-side mode | marks a changed line and > a line only on the right.

Comparing directories

bash · try it yourself
mkdir -p v1 v2
printf 'print("v1")\n' > v1/app.py
printf 'print("v2")\n' > v2/app.py
echo "docs" > v1/README.md
cp v1/README.md v2/README.md
echo "new" > v2/CHANGELOG.md
diff -rq v1 v2
Only in v2: CHANGELOG.md
Files v1/app.py and v2/app.py differ

-rq gives a quick summary: which files differ, and which exist on only one side.

diff in scripts and tests

Because of its exit status, diff is a simple test runner: compare a program's actual output with the expected output.

bash · try it yourself
printf 'ada\nlinus\n' > expected.txt
printf 'linus\nada\n' | sort > actual.txt
if diff -q expected.txt actual.txt > /dev/null; then
  echo "PASS"
else
  echo "FAIL"; diff expected.txt actual.txt
fi
diff <(printf 'a\nb\n') <(printf 'a\nc\n')
PASS
2c2
< b
---
> c

<(command) is process substitution: Bash runs the command and passes its output as if it were a file. It lets you diff the output of two commands without temporary files, e.g. diff <(ls dir1) <(ls dir2).

Patches: diff + patch

A unified diff saved to a file is a patch. The patch command applies it to the original file, which is how fixes were exchanged long before Git:

text
$ diff -u app.conf app.conf.new > fix.patch
$ patch app.conf < fix.patch
patching file app.conf

cmp is a byte-level cousin of diff, good for binary files: it only reports the first difference.

Test yourself

What exit status does diff return when the files are identical?

  1. 0
  2. 1
  3. 2
  4. -1

In unified diff output, what does a line starting with + mean?

  1. A comment
  2. A line added in the second file
  3. A line removed from the first file
  4. An unchanged context line

Which option only reports whether two files differ?

  1. -u
  2. -y
  3. -q
  4. -r