diff compares two files line by line and shows you what is different between them. It is one of the most useful commands on Linux — you will reach for it every time you need to check what changed in a config file, review a patch, or verify that two directories are in sync.
Syntax
diff [options] file1 file2
diff [options] file1 directory
diff [options] directory1 directory2
When you pass a file and a directory, diff looks for a file with the same name inside the directory. When you pass two directories, it compares files with matching names in both.
Reading diff Output
Running diff without options gives you the normal format. Take these two files:
numbers1.txt
1
2
3
4
5
6
numbers2.txt
1
2
3
4
7
6
diff numbers1.txt numbers2.txt
Output:
5c5
< 5
---
> 7
The notation works like this:
5c5— line 5 in file1 needs to be changed to match line 5 in file2<— lines from file1 (left side)>— lines from file2 (right side)cmeans change,dmeans delete,ameans add
The output is effectively a set of instructions to transform file1 into file2.
Unified Format (-u)
The unified format is the one you will use most. It is easier to read and it is the format used by git diff and most patch tools.
diff -u numbers1.txt numbers2.txt
Output:
--- numbers1.txt 2026-05-29 10:00:00
+++ numbers2.txt 2026-05-29 10:01:00
@@ -3,4 +3,4 @@
3
4
-5
+7
6
Lines starting with - are in file1 only. Lines starting with + are in file2 only. Lines with a space are the same in both (context lines).
To show more context lines (default is 3):
diff -U 5 file1 file2
Side-by-Side (-y)
Shows both files next to each other. Differences are marked with |, lines only in file1 with <, and lines only in file2 with >.
diff -y numbers1.txt numbers2.txt
Output:
1 1
2 2
3 3
4 4
5 | 7
6 6
To suppress identical lines and show only differences:
diff -y --suppress-common-lines numbers1.txt numbers2.txt
You can also use sdiff which is an alias for this behaviour.
Compare Directories Recursively (-r)
diff -r folder1/ folder2/
This compares files with matching names in both directories and reports files that exist only on one side.
To only list which files differ without showing the actual differences:
diff -rq folder1/ folder2/
This is useful for a quick sanity check when you need to verify two directory trees are in sync. For a more detailed article on this, see Find Differences Between Folders Recursively.
Ignore Whitespace
Ignore all whitespace differences:
diff -w file1 file2
Ignore changes in the amount of whitespace (but not its presence):
diff -b file1 file2
Ignore blank lines:
diff -B file1 file2
These are useful when comparing config files that have been reformatted, or code files where indentation was adjusted.
Ignore Case (-i)
diff -i file1 file2
Treats uppercase and lowercase as identical. Useful for comparing output from different systems or case-insensitive configurations.
Only Report Whether Files Differ (-q)
diff -q file1 file2
Output:
Files file1 and file2 differ
No diff output, just a yes/no answer. Very useful in scripts.
Create a Patch File
The standard way to distribute changes is to save the unified diff to a file:
diff -u original.conf modified.conf > changes.patch
The .patch file can be shared and applied on another machine.
Apply a Patch
Use the patch command to apply a patch file:
patch original.conf < changes.patch
To apply a patch to a directory of files:
patch -p1 < changes.patch
The -p1 strips one level of leading directory from the file paths in the patch — common when patches are generated with git diff or from a project root.
To do a dry run before applying:
patch --dry-run original.conf < changes.patch
Practical Examples
Compare two nginx configs and show only differences, side by side:
diff -y --suppress-common-lines nginx.conf nginx.conf.bak
Check if two backup directories are identical:
diff -rq /backup/site/ /backup/site.prev/ && echo "identical" || echo "differs"
Compare a config against a default, ignoring comments and blank lines:
grep -v '^#' file1 > /tmp/f1 && grep -v '^#' file2 > /tmp/f2 && diff /tmp/f1 /tmp/f2
Generate a patch from two versions of a script:
diff -u deploy-v1.sh deploy-v2.sh > deploy.patch
Compare a local file to one on a remote server:
diff local.conf <(ssh user@server cat /etc/app/app.conf)
Recursively compare two directories, show summary only:
diff -rq /etc/nginx/ /etc/nginx.bak/
Compare files ignoring whitespace changes:
diff -uw httpd.conf httpd.conf.new
vimdiff
If you prefer a visual comparison in the terminal, vimdiff opens two files side by side in Vim with differences highlighted:
vimdiff file1 file2
Navigate between differences with ]c (next) and [c (previous). Useful for reviewing changes interactively.
Related: Find differences between folders · sed — find and replace · grep — search for patterns
Originally published: May 20, 2007 · Last updated: May 29, 2026