How can diff show file differences?

QuestionsQuestions8 SkillsProDec, 25 2025
0108

The diff command shows differences by comparing files line by line. When you run diff file1 file2, it analyzes the text and tells you exactly what needs to be modified in the first file to make it match the second file.

Here is how it visualizes those differences:

1. The Change Indicators (a, d, c)

As we discussed, diff uses specific letters to describe the "action" required:

  • 1c1: Line 1 needs to be changed.
  • 3a4,5: Add lines 4 and 5 from the second file after line 3 of the first file.
  • 8d7: Delete line 8 from the first file so it matches the second file at line 7.

2. The Symbols (< and >)

To help you see the actual text, diff uses markers:

  • < (Less than): This points to the left, representing the first file (the original).
  • > (Greater than): This points to the right, representing the second file (the new version).
  • ---: This acts as a separator between the content of the two files.

3. Practical Example

If your file1 has "Apple" and file2 has "Banana", the output looks like this:

1c1
< Apple
---
> Banana

This tells you: "At line 1, there is a change. Remove 'Apple' and put 'Banana' instead."

Pro Tip: Side-by-Side Comparison

If the standard output is hard to read, you can use the -y flag to see the differences side-by-side:

diff -y file1 file2

This is often much easier for humans to read! Give it a try in your terminal!

0 Comments

no data
Be the first to share your comment!