Fundamentals 6 min read

Master Linux diff: Compare Files and Directories with Real‑World Examples

This guide walks you through creating sample files, using the Linux diff command to detect differences, interpreting its output symbols, handling case‑sensitivity, ignoring whitespace, and swapping file order, all illustrated with step‑by‑step commands and screenshots.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux diff: Compare Files and Directories with Real‑World Examples

When you need to see what changed between two files or two versions of the same file, the Linux diff command provides a quick line‑by‑line comparison. The article demonstrates the command with concrete examples.

Creating sample files

First, open a terminal and create two identical files using nano: $ nano diffsample1 Enter the three lines:

发布 优质的 技术文章

Save and exit with Ctrl+X. Then create the second file: $ nano diffsample2 Enter the same three lines and save.

Basic diff usage

Run: $ diff diffsample1 diffsample2 No output means the files are identical.

Detecting a change

Edit diffsample2 changing the word 文章 to 帖子 and save.

Run diff again: $ diff diffsample1 diffsample2 The output shows 3c3, indicating line 3 in the first file should be replaced, and displays the differing words.

Understanding diff symbols

c : change

a : add

d : delete

The numbers before the letters refer to line numbers in the first file; the numbers after refer to the second file. The < and > symbols show the original and new lines respectively.

Swapping file order

Running diff diffsample2 diffsample1 reverses the reference file, so the output now suggests changing 技术帖子 back to 技术文章 .

Case‑sensitivity

If only the case of a word differs, diff reports it. Use the -i option to ignore case: $ diff -i diffsample1 diffsample2 No output means the files are considered identical when case is ignored.

Unified format and whitespace handling

Use -u for unified diff output, which prefixes removed lines with - and added lines with +:

To ignore spaces and tabs, add -w: $ diff -w diffsample1 diffsample2 After inserting extra spaces/tabs in diffsample2, the plain diff shows differences, but diff -w produces no output.

Further options

Many other flags exist for fine‑grained control; consult the manual with man diff for a complete list.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShellUnixdifffile 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.