Fundamentals 7 min read

Master Linux diff: Compare Files, Understand Formats, and Use Powerful Options

This guide explains how to use the Linux diff command for file comparison, covering its basic syntax, common options like -b, -B, -i, -r, and demonstrating normal, context, unified, and side‑by‑side output formats with concrete examples and interpretation of the results.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux diff: Compare Files, Understand Formats, and Use Powerful Options

Why Use diff for Text Comparison

When editing code or documents, you often need to see exactly what changed between two versions. The Unix diff utility provides a reliable way to compare files without installing any extra software, and it forms the basis of many version‑control systems.

Basic Command Syntax

diff [OPTION]... FILES

The command takes one or more options followed by the files to compare.

Commonly Used Options

-b

– ignore differences in the amount of whitespace within a line. -B – ignore completely blank lines. -i – ignore case differences. -r – if a directory is given, compare files recursively.

Output Formats

diff can produce several output styles, each suited to different needs.

Normal diff – the default, concise format.

Context diff – shows a few unchanged lines around each change.

Unified diff – a compact version of context diff, used by git diff.

Side‑by‑side diff – displays the two files in parallel columns.

Example Files

Consider a file a.c:

We copy it to b.c and change the third line from hello to HELLO:

1) Normal diff

Command: diff a.c b.c Result:

Explanation of the lines:

3c3 – line 3 in a.c is changed to line 3 in b.c (c = change).

< hello world! – the original line removed from a.c.

------ – separator.

> HELLO world! – the new line added to b.c.

2) Context diff

Command: diff -c a.c b.c Result (illustrated):

The header shows the file names and timestamps. Lines prefixed with *** indicate the original context, and --- indicate the new context. A ! marks a changed line, - a deletion, and + an addition.

3) Unified diff

Command: diff -u a.c b.c Result (illustrated):

The first two lines are the file headers. Lines beginning with - show the original content, and lines beginning with + show the new content. This format is the one used by git diff.

4) Side‑by‑side diff

Command: diff a.c b.c -y -W 50 Result (illustrated):

In this layout a vertical bar | marks a changed line. A < indicates the left file has fewer lines, while > indicates the right file has extra lines.

Conclusion

The diff command is a versatile tool for quickly spotting differences between text files. By mastering its basic syntax, useful options, and the four main output formats, you can efficiently review changes without relying on graphical tools or paid software.

Command LineVersion Controldifftext comparison
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.