Explain Text Changes with diff
Explain Text Changes with diff
A student's mark was corrected, and the teacher wants to know exactly what changed between the earlier and later report. diff compares text line by line and describes differences. This is more useful than merely saying that the filenames differ or that the files have different sizes.
We will compare a tiny before-and-after pair first. A difference is a normal outcome for this tool, so its exit status deserves careful interpretation: nonzero does not automatically mean that the comparison itself broke.
What you’ll learn
- Compare two reports and interpret changed lines.
- Distinguish identical files, differences and an actual comparison error.
Line comparison explains what changed
Before
The earlier report records the first mark.
72After
The corrected report records a different mark.
80Comparison
diff describes changed lines and returns 1 for differences.
72 → 80; status 1
Compare marks before and after a correction
Prepare the example
Use a fresh folder. The first two lines prepare reports with 72 and 80 respectively. These are known text inputs, making the comparison easy to read. The example intentionally expects a difference; do not add a change that makes both files equal just to obtain status zero.
Bash · Linux terminal
printf '72\n' > before.txt
printf '80\n' > after.txt
diff before.txt after.txtExpected result
1c1
< 72
---
> 80diff before.txt after.txt treats the first file as the earlier side and the second as the later side. The normal output identifies the changed line; markers distinguish text from the first file and text from the second. Here the useful observation is that 72 became 80.
For GNU diff, status 0 means no differences, 1 means differences were found, and a higher status indicates trouble. A missing input is therefore a different situation from a valid comparison that finds a correction. When reports grow longer, a unified format such as diff -u provides nearby context that makes a change easier to review.
Read changed lines and choose comparison context
diff compares text files line by line and reports what must change for one file to match the other. It is useful for scripts, configuration files, generated output and version comparisons.
What is diff?
The diff command shows the difference between two files line by line. It tells you what needs to be changed in the first file to make it identical to the second file.
Let’s Start with a Simple Example
First, we’ll create two text files. Open your terminal and type:
echo "apple" > file1.txt
echo "banana" >> file1.txt
echo "cherry" >> file1.txt
echo "apple" > file2.txt
echo "blueberry" >> file2.txt
echo "cherry" >> file2.txt
This creates two files with slight differences. Now let’s compare them using diff:
diff file1.txt file2.txt
Expected output for this example:
2c2
< banana
---
> blueberry
Let’s decode this:
- 2c2: Line 2 in file1 was "changed" to line 2 in file2
<means the line in file1.txt>means the line in file2.txt
So the second line changed from “banana” to “blueberry.” Easy, right?
Optional Flags for Better Comparison
You can also use diff with some useful options:
-y: Shows side-by-side comparison--suppress-common-lines: Hides lines that are the same
diff -y --suppress-common-lines file1.txt file2.txt
Expected output for this example:
banana | blueberry
Now it’s even easier to spot what changed!
Wrapping Up
And that’s it! You’ve just learned how to use diff to compare files in Linux. It’s a powerful tool that’s simple to use and can save you a lot of time when debugging or reviewing changes.
Try it, then check the worked answer
Your task: Interpret diff status 1 after a valid comparison.
Use a fresh folder. The first two lines prepare reports with 72 and 80 respectively. These are known text inputs, making the comparison easy to read. The example intentionally expects a difference; do not add a change that makes both files equal just to obtain status zero.
Bash · complete example for this question
printf '72\n' > before.txt
printf '80\n' > after.txt
diff before.txt after.txtExpected result
1c1
< 72
---
> 80Worked answer
The files differ.
Status 1 after a valid comparison means the files differ. It is not the same as a missing-file error. Read the comparison and any diagnostics together, using the tool's documented statuses instead of the blanket rule that every nonzero result is a failure.
Check your decision
Differences are a result, not necessarily an error
Use diff when the changes in text matter, not just equality. Keep track of the order of the inputs and interpret the output using that order. Treat 'different' as a meaningful result, and distinguish it from an operation that could not compare the inputs.
Command reference: GNU diff options and exit statuses.
Reaching the recap and completing the quizzes are separate milestones.