diff Command in Linux — Compare Files with Examples
The diff command compares files line by line in Linux — showing exactly what changed between two versions. Used with patch, it enables applying updates to codebases without redistributing entire files.
What You’ll Learn
By the end of this tutorial, you’ll read normal, unified, and context diff formats; compare directories recursively; ignore whitespace differences; create patch files; and use colordiff for visual clarity.
Why diff Matters
Every software update is essentially a list of changes between versions. diff quantifies those changes precisely. DodaZIP uses diff to verify archive integrity across versions, and Durga Antivirus Pro compares virus definition files against known-good baselines using diff to detect corruption.
Learning Path
flowchart LR
A[Essential Commands] --> B[Text Processing Tools]
B --> C[diff Command<br/>You are here]
C --> D[Version Control with Git]
C --> E[Patch Management]
style C fill:#f90,color:#fff
Syntax Overview
diff [options] file1 file2
diff [options] dir1 dir2Options Table
| Option | Description |
|---|---|
-u | Unified format (most readable) |
-c | Context format |
-y | Side-by-side output |
-r | Recursively compare directories |
-q | Only report if files differ (not the diff itself) |
-w | Ignore whitespace (all spaces and tabs) |
-b | Ignore changes in amount of whitespace |
-i | Ignore case differences |
-N | Treat absent files as empty |
-s | Report when files are identical |
-W N | Side-by-side output width |
Examples
Example 1: Normal diff
$ cat version1.txt
Hello world
Welcome to Linux
Goodbye
$ cat version2.txt
Hello world
Welcome to Unix
Goodbye for now
$ diff version1.txt version2.txt
2c2
< Welcome to Linux
---
> Welcome to Unix
3c3
< Goodbye
---
> Goodbye for now2c2 means “line 2 in file1 changed to line 2 in file2.” < lines are from file1, > lines from file2.
Example 2: Side-by-Side (-y)
$ diff -y version1.txt version2.txt
Hello world Hello world
Welcome to Linux | Welcome to Unix
Goodbye | Goodbye for nowParallel columns with | marking changed lines. Use -W 60 to control width.
Example 3: Unified Format (-u)
$ diff -u version1.txt version2.txt
--- version1.txt 2026-06-20 10:00:00
+++ version2.txt 2026-06-20 10:00:05
@@ -1,3 +1,3 @@
Hello world
-Welcome to Linux
+Welcome to Unix
-Goodbye
+Goodbye for nowThe - lines are removed, + lines are added. This is the format used by Git and patch tools.
Example 4: Context Format (-c)
$ diff -c version1.txt version2.txt
*** version1.txt 2026-06-20 10:00:00
--- version2.txt 2026-06-20 10:00:05
***************
*** 1,3 ****
Hello world
! Welcome to Linux
! Goodbye
--- 1,3 ----
Hello world
! Welcome to Unix
! Goodbye for nowShows surrounding context lines with ! marking changes.
Example 5: Recursive Directory Compare (-r)
$ diff -rq configs/ backups/configs
Only in configs/: new_setting.conf
Files configs/app.conf and backups/configs/app.conf differRecursively finds all differences between two directories. -q shows only filenames, not the full diff.
Example 6: Ignore Whitespace (-w vs -b)
$ echo "hello world" > a.txt
$ echo "hello world" > b.txt
$ diff a.txt b.txt
1c1
< hello world
---
> hello world
$ diff -b a.txt b.txt
# no output (ignores amount of whitespace)
$ diff -w a.txt b.txt
# no output (ignores all whitespace)Use -b to ignore spacing differences within lines; -w ignores all whitespace entirely.
Example 7: Diff Directories with Filename Listing
$ diff -rq /etc/nginx/ /backup/nginx/
Files /etc/nginx/nginx.conf and /backup/nginx/nginx.conf differ
Only in /etc/nginx/: sites-enabled
Only in /backup/nginx/: sites-availableGreat for comparing configuration directories before and after changes.
Example 8: Create a Patch File
$ diff -u version1.txt version2.txt > changes.patch
$ cat changes.patch
--- version1.txt 2026-06-20 10:00:00
+++ version2.txt 2026-06-20 10:00:05
@@ -1,3 +1,3 @@
Hello world
-Welcome to Linux
+Welcome to Unix
-Goodbye
+Goodbye for now
$ patch < changes.patch
patching file version1.txtThe patch file can be distributed and applied to any machine.
Example 9: Only Report Different Files (-q)
$ diff -qr /var/www /var/www-backup
Files /var/www/index.html and /var/www-backup/index.html differ
Only in /var/www/: new-page.htmlUseful for deployment verification scripts.
Example 10: colordiff for Visual Clarity
$ colordiff -u version1.txt version2.txtIf colordiff is not installed: apt install colordiff or yum install colordiff. It wraps diff with colorized output — red for removals, green for additions.
# Alias for everyday use
alias diff='colordiff -u'Common Use Cases
| Use Case | Command |
|---|---|
| Check config change | diff -u /etc/nginx/nginx.conf /backup/nginx.conf |
| Verify deployment | diff -qr /var/www /staging/www |
| Create software patch | diff -Naur old/ new/ > fix.patch |
| Ignore whitespace diff | diff -w file1 file2 |
| Side-by-side comparison | diff -y -W 80 file1 file2 |
Common Errors
- diff returns no output: The files are identical. Use
diff -sto confirm explicitly. - diff: file: No such file or directory: Verify both file paths exist.
- Too much output on binary files: Use
diff -qordiff -ato force text comparison. - Patch rejects: When running
patch, if the context doesn’t match exactly, use--fuzz=Nto allow tolerance. - Tab alignment in side-by-side: Use
-Wto set the terminal width correctly.
Practice Exercises
- Basic diff: Create two text files with minor differences and run
diff. - Unified format: Generate a unified diff of a config file before and after editing.
- Directory compare: Compare two directories with
diff -rq. - Create and apply patch: Create a patch file from a diff and apply it to the original.
- Scripting: Write a script that diffs critical config files daily and emails changes.
Challenge
Write a script that monitors /etc/ for unauthorized changes by comparing against a known-good snapshot. Durga Antivirus Pro uses this exact approach for integrity monitoring — detecting rootkits and config tampering.
#!/bin/bash
SNAPSHOT="/var/lib/config-snapshot"
REPORT="/tmp/config-changes.txt"
if [ ! -d "$SNAPSHOT" ]; then
echo "Creating baseline snapshot..."
cp -a /etc "$SNAPSHOT"
exit 0
fi
diff -qr /etc "$SNAPSHOT" > "$REPORT"
if [ -s "$REPORT" ]; then
echo "Changes detected:"
cat "$REPORT"
else
echo "No changes — system is clean."
fiReal-World Task
You’ve received a security advisory about a vulnerability in nginx.conf. Compare your production config against the secure baseline provided by the vendor. Generate a patch file, apply it to staging, then roll out to production after verification.
What is diff?
diff (difference) is a command-line tool that compares two files or directories line by line, showing added, removed, or changed lines in several output formats including normal, unified, context, and side-by-side.
Related Tutorials
- Essential Linux Commands — file comparison tools
- Git Version Control — modern diff-based workflow
- Bash Scripting Guide — automate diff in monitoring scripts
- Linux Administration Basics — foundational skills
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro