AI Hints
Home›Linux Tutorial›Exit Codes, diff, xargs
Linux · Lesson 56 of 120

diff: Comparing Files

What changed?.

⏱ 2 min read▶ Video at 1:12:03🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

You have two versions of a file, and they look almost the same. What exactly changed? Reading both line by line is slow, and easy to get wrong. Diff spots the differences for you.

diff: Comparing Files
From the video: diff: Comparing Files
  • Two versions of a file
  • What changed?
  • diff finds out

A side by side view

Here are two versions of a shopping list. Diff dash Y shows them side by side. A vertical bar marks a line that changed. Milk became oat milk, and cheese was replaced by apples. Lines with no mark are the same in both files. If a line had only been removed, or only added, diff would mark it with a less than, or a greater than sign.

Terminal
diff -y -W 50 list-v1.txt list-v2.txt
eggs                    eggs
milk                  | oat milk
bread                   bread
cheese                | apples

The unified format

Dash U gives the format programmers use, called unified. Lines starting with minus were removed, and lines starting with plus were added. Lines with a space didn't change. It's the same format Git uses, to show changes to code.

Terminal
diff -u list-v1.txt list-v2.txt
--- list-v1.txt 2026-01-05 09:00:00.000000000 +0000
+++ list-v2.txt 2026-01-05 09:00:00.000000000 +0000
@@ -1,4 +1,4 @@
 eggs
-milk
+oat milk
 bread
-cheese
+apples

Diff tips

If the files are identical, diff prints nothing at all, and returns exit code zero. That makes it great for checking a backup. Dash Q only tells you whether files differ. And dash R compares whole folders.

keyvalue
Same filesprints nothing, exit 0
-yside by side
-uunified, like Git
-rcompare folders

Summary

Diff shows exactly how two files differ. Dash Y side by side, and dash U in the unified format. Next, xargs, for feeding lists into commands.

Key takeaways
  • diff a b
  • diff -y
  • diff -u
Next up: xargs — turn a list into arguments