Quick take: The diff command shows the differences between two files. diff old new lists the changes; diff -u gives the familiar unified format used by patches, and diff -y shows a side-by-side view.

Introduction

The diff command compares two files line by line and reports what changed — the basis of version control, patches, and configuration audits. Its output can look cryptic at first, but a couple of formats (unified and side-by-side) make differences easy to read.

Syntax

The basic syntax of the diff command is:

diff [OPTIONS] FILE1 FILE2

Common Options and Parameters

The most useful options and parameters for the diff command:

OptionDescription
-uUnified format (the format used by patches and Git).
-ySide-by-side comparison.
-cContext format with surrounding lines.
-iIgnore case differences.
-wIgnore all whitespace.
-rRecursively compare directories.
-qBrief — only report whether files differ.

Practical Examples

Real diff commands you can run today:

# Compare two files
diff config.old config.new
# Unified diff (patch-style)
diff -u config.old config.new
# Side-by-side view
diff -y config.old config.new
# Ignore whitespace differences
diff -w file1 file2
# Just say whether they differ
diff -q file1 file2
# Compare two directories recursively
diff -r dir1/ dir2/

Tips and Best Practices

  • diff -u produces the unified format you see in Git and patches — the most readable and widely understood.
  • Use -w to ignore whitespace when comparing files that differ only in indentation or line endings.
  • diff -r dir1 dir2 compares whole directory trees, listing files that differ or exist on only one side.

Final Thoughts

diff reveals exactly what changed between two files or directories, and its unified format (-u) is the lingua franca of patches and version control. Learn to read that format, use -y for a visual side-by-side, and -w to ignore noise. For interactive merging, pair it with tools like vimdiff or meld.

FAQ: diff Command in Linux

How do I compare two files in Linux?+

Run diff file1 file2. It lists the lines that differ. Use diff -u for the readable unified format or diff -y for a side-by-side view.

How do I read diff output?+

Lines marked with < come from the first file and lines with > from the second. In unified format (-u), lines starting with - were removed and + were added.

How do I compare two directories?+

Use diff -r dir1 dir2 to compare them recursively. It reports files that differ and files that exist in only one directory.

How do I ignore whitespace when comparing files?+

Use -w to ignore all whitespace, or -b to ignore changes in the amount of whitespace. This is useful when only indentation or line endings differ.

What is a unified diff?+

A unified diff (diff -u) shows changes with a few lines of context, marking removed lines with - and added lines with +. It is the format used by patch files and Git, and the easiest to read.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support