Short answer: diff -u --color old.conf new.conf compares two files line by line and prints removed lines in red with a -, added lines in green with a +. Plain --color means --color=auto, which colors only when the output goes straight to a terminal; for a pager use --color=always with less -R.
diff old.conf new.conf # normal output
diff -u --color old.conf new.conf # unified, colored
diff -u --color=always old.conf new.conf | less -R # colored in a pager
diff -y -W 60 old.conf new.conf # side by side
diff -rq /srv/site-old /srv/site-new # which files differEvery output on this page was produced on 9 November 2026 with GNU diffutils 3.8 and GNU patch 2.7.6, the versions in Debian 12, on small sample files in a scratch folder. diff --version shows which diff you have. The two files used in most examples are a five-line config before and after an edit:
old.conf new.conf
port=8080 port=8080
host=127.0.0.1 host=0.0.0.0
debug=true workers=4
workers=2 log=/var/log/app.log
log=/var/log/app.log timeout=30diff options at a glance
| Task | Command |
|---|---|
| Unified output (for patches and review) | diff -u a b |
| Colored output | diff --color -u a b |
| Side by side, 120 columns | diff -y -W 120 a b |
| Only say whether files differ | diff -q a b |
| Compare directories recursively | diff -r dir1 dir2 |
| Ignore all whitespace | diff -w a b |
| Ignore changes in the amount of whitespace | diff -b a b |
| Ignore blank-line-only changes | diff -B a b |
| Ignore Windows line endings | diff --strip-trailing-cr a b |
| Make a patch of a directory | diff -ruN app.orig app > fix.patch |
| Compare command output | diff <(sort a) <(sort b) |
Reading the normal output
2,4c2,3
< host=127.0.0.1
< debug=true
< workers=2
---
> host=0.0.0.0
> workers=4
5a5
> timeout=30Each block starts with a command that would turn the first file into the second: 2,4c2,3 means lines 2 to 4 of the old file change into lines 2 and 3 of the new one, 5a5 means a line is added after line 5, and a d would mean deleted. Lines with < come from the first file, lines with > from the second. It is compact but shows no context, which is why the unified format is used almost everywhere else.
Unified output: diff -u
--- old.conf 2026-11-09 09:12:04.000000000 +0000
+++ new.conf 2026-11-09 09:15:41.000000000 +0000
@@ -1,5 +1,5 @@
port=8080
-host=127.0.0.1
-debug=true
-workers=2
+host=0.0.0.0
+workers=4
log=/var/log/app.log
+timeout=30The two header lines name the files and their modification times. Each hunk starts with @@ -1,5 +1,5 @@: five lines starting at line 1 in the old file, five starting at line 1 in the new one. Then come the lines themselves, prefixed with a space (unchanged context), - (only in the old file) or + (only in the new). By default diff shows 3 lines of context; -U 0 shows none and -U 10 shows more. --label NAME, given twice, replaces the header names, which is useful when the inputs are temporary files.
diff --color: red and green output
--color takes never, always or auto, and plain --color is auto: piped into less or a file, the output stays plain. The default palette is rs=0:hd=1:ad=32:de=31:ln=36: bold file headers, cyan @@ lines, green added lines and red deleted lines. You can change it with terminal color codes, for example bold red and bold green:
diff -u --color=always --palette='ad=1;32:de=1;31' old.conf new.conf | less -R--colorarrived in GNU diffutils 3.4 (2016). BusyBoxdiff, the one in Alpine-based containers, supports unified output only per its documentation, so--colorand-yfail there; install GNU diff withapk add diffutilsor compare on the host.- Git colors on its own:
git diff --no-index old.conf new.confcompares any two files, even outside a repository, with Git’s colors and pager, and--word-diffmarks changed words instead of whole lines. Like diff, it exits with 1 when the files differ.
Side by side: diff -y and -W
port=8080 port=8080
host=127.0.0.1 | host=0.0.0.0
debug=true | workers=4
workers=2 <
log=/var/log/app.log log=/var/log/app.log
> timeout=30| marks a changed line, < a line only on the left, > one only on the right. The default width is 130 columns, so long lines are cut; use -W "$(tput cols)" to fit your terminal. --suppress-common-lines drops the unchanged lines and --left-column prints them once, on the left.
Comparing directories: -r, -q, -N and -x
Only in site-old: about.html
Only in site-new: contact.html
Only in site-old: img
Files site-old/index.html and site-new/index.html differWithout -r, diff compares only the top level and reports Common subdirectories: site-old/css and site-new/css instead of going in. -q names the files that differ without showing the changes; drop it to see them. -N treats a missing file as empty, so a file that exists on one side appears as fully added or removed, which a patch needs. -x img (or --exclude=PATTERN) skips names that match. Binary files are reported as Binary files b1.bin and b2.bin differ. To compare a deployed tree with your copy by content without changing anything, rsync -anic works as well, see the rsync guide.
Ignoring whitespace, blank lines and line endings
-b and -w are easy to mix up. We compared max_connections=100 with max_connections = 100: -b still reported the line as changed, because it only ignores a change in the amount of existing whitespace (indentation, trailing spaces, two spaces instead of one); -w ignores whitespace entirely and reported no difference.
-Bignores a change only when every line in it is blank. A blank line added next to a real change still shows: in our YAML test,-Balone changed nothing and-w -Btogether reported the files as equal.- A file edited on Windows has
\r\nline endings and differs on every line;--strip-trailing-crmade our two copies compare equal. -iignores case, and-I REGEXignores changes whose lines all match the expression, for example-I '^#'for comment-only edits.
Make a patch with diff and apply it with patch
A unified diff is a patch. For a single file, save it and apply it where the old version lives; --dry-run checks first without changing anything:
$ diff -u app.conf app.conf.new > fix.patch
$ patch --dry-run -p0 < fix.patch
checking file app.conf
$ patch -p0 < fix.patch
patching file app.confFor a whole tree, compare two directories with -ruN and apply from inside the target with -p1. The number tells patch how many leading path parts to strip from the names in the patch: the header says app.orig/conf/app.conf, and inside the target directory the file is conf/app.conf, so one part goes.
$ diff -ruN app.orig app > feature.patch
$ cd /srv/app
$ patch -p1 --dry-run < ~/feature.patch
checking file conf/app.conf
checking file health.txt
$ patch -p1 < ~/feature.patch
patching file conf/app.conf
patching file health.txtpatch -R -p1 < ~/feature.patch undoes it, and removed the new health.txt again in our test. Two messages mean the command needs changing:
can't find file to patch at input line 4
Perhaps you used the wrong -p or --strip option?Reversed (or previously applied) patch detected! Assume -R? [n]
Apply anyway? [n]
Skipping patch.After the second one, patch also writes the skipped hunk to a .rej file next to the target, here conf/app.conf.rej. Patches made by git diff use a/ and b/ prefixes and apply with -p1 too.
Compare command output: diff <(cmd) <(cmd)
Bash and zsh can hand the output of a command to diff as if it were a file. Sorting first compares the content of two lists instead of their order; unsorted, our two package lists produced nine lines of output, sorted only this:
2a3
> htop
4d4
< rsyncThe same trick compares two servers, for example diff <(ssh web1 cat /etc/nginx/nginx.conf) <(ssh web2 cat /etc/nginx/nginx.conf), and - reads one side from a pipe: command | diff - file. Process substitution does not exist in sh (dash on Debian), so scripts that use it need #!/bin/bash and must be run with bash, as in running a .sh file.
diff exit codes in scripts
| Exit code | Meaning | Example from our test |
|---|---|---|
| 0 | The inputs are the same | diff old.conf same.conf |
| 1 | The inputs differ | diff old.conf new.conf |
| 2 | Trouble: a missing file, no permission | diff: nosuch.conf: No such file or directory |
Exit code 1 is not an error, but a script with set -e stops on it. Test the result explicitly, and treat 2 as the real failure; the >/dev/null only hides the output, as the redirection guide explains:
if diff -q /etc/app/app.conf /srv/backup/app.conf >/dev/null; then
echo "unchanged"
else
[ $? -eq 1 ] && echo "changed" || echo "diff failed"
fidiff -s also says so when files are the same: Files old.conf and same.conf are identical. For binary files or when only the answer matters, cmp -s is the lighter tool.
Compare a server config with the package default
After months on a server it is easy to lose track of what you changed in a config file. On Debian and Ubuntu, dpkg -V PACKAGE lists the package’s files whose content no longer matches the package, in lines like ??5?????? c /etc/logrotate.conf (5: checksum differs, c: config file). To see the actual changes, unpack the original file from the package into a temporary folder and diff against it:
mkdir /tmp/pristine && cd /tmp/pristine
apt download logrotate
dpkg-deb -x logrotate_*.deb .
diff -u /tmp/pristine/etc/logrotate.conf /etc/logrotate.confWe ran these steps against a copy of logrotate.conf with three edits; the unified diff shows exactly those:
--- /tmp/pristine/etc/logrotate.conf 2022-12-14 18:16:50.000000000 +0000
+++ /etc/logrotate.conf 2026-11-09 13:21:15.419191247 +0000
@@ -3,10 +3,10 @@
# global options do not affect preceding include directives
# rotate log files weekly
-weekly
+daily
# keep 4 weeks worth of backlogs
-rotate 4
+rotate 14
# create new (empty) log files after rotating old ones
create
@@ -15,7 +15,7 @@
#dateext
# uncomment this if you want your log files compressed
-#compress
+compress
# packages drop log rotation information into this directory
include /etc/logrotate.dapt download fetches the version apt would install now, which can be newer than the one you have; apt download logrotate=VERSION picks a specific one. After upgrades, a new default often waits next to your file as .dpkg-dist, and diff -u file file.dpkg-dist shows what the new version brings; the Debian 12 to 13 upgrade guide explains those prompts.
Frequently asked
How do I get colored output from diff?
Use diff --color -u file1 file2 with GNU diffutils 3.4 or later: removed lines are red, added lines green, @@ lines cyan and file headers bold. Plain --color means --color=auto, which colors only when the output goes to a terminal, so use diff --color=always -u file1 file2 | less -R for a pager. git diff --no-index file1 file2 is an alternative that colors by default.
What is the difference between diff -b and diff -w?
-b ignores changes in the amount of whitespace that is already there, such as indentation or trailing spaces. -w ignores all whitespace, so max_connections=100 and max_connections = 100 count as equal with -w but not with -b. -B additionally ignores changes that consist only of blank lines.
How do I compare two files side by side in Linux?
Run diff -y file1 file2. A | marks changed lines, < lines only in the first file and > lines only in the second. The default width is 130 columns; set it with -W, for example -W "$(tput cols)", and add --suppress-common-lines to show only the differences.
What do diff exit codes 0, 1 and 2 mean?
0 means the inputs are the same, 1 means they differ and 2 means trouble, such as a missing file. A script with set -e stops at exit code 1 even though nothing went wrong, so test the result with if diff -q a b and treat only 2 as a failure.