Guides

diff command in Linux: --color, unified and side-by-side output, patches

The Linux diff command: diff -u and diff --color output, side by side with -y, directories, ignoring whitespace, making a patch, and exit codes 0, 1, 2.

Published ~9 min read
On this page

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.

Linux · GNU diffutils
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 differ

Every 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 and new.conf
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=30

diff options at a glance

TaskCommand
Unified output (for patches and review)diff -u a b
Colored outputdiff --color -u a b
Side by side, 120 columnsdiff -y -W 120 a b
Only say whether files differdiff -q a b
Compare directories recursivelydiff -r dir1 dir2
Ignore all whitespacediff -w a b
Ignore changes in the amount of whitespacediff -b a b
Ignore blank-line-only changesdiff -B a b
Ignore Windows line endingsdiff --strip-trailing-cr a b
Make a patch of a directorydiff -ruN app.orig app > fix.patch
Compare command outputdiff <(sort a) <(sort b)

Reading the normal output

diff old.conf new.conf
2,4c2,3
< host=127.0.0.1
< debug=true
< workers=2
---
> host=0.0.0.0
> workers=4
5a5
> timeout=30

Each 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

diff -u old.conf new.conf
--- 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=30

The 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:

Linux · GNU diffutils
diff -u --color=always --palette='ad=1;32:de=1;31' old.conf new.conf | less -R
  • --color arrived in GNU diffutils 3.4 (2016). BusyBox diff, the one in Alpine-based containers, supports unified output only per its documentation, so --color and -y fail there; install GNU diff with apk add diffutils or compare on the host.
  • Git colors on its own: git diff --no-index old.conf new.conf compares any two files, even outside a repository, with Git’s colors and pager, and --word-diff marks changed words instead of whole lines. Like diff, it exits with 1 when the files differ.

Side by side: diff -y and -W

diff -y -W 60 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=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

diff -rq site-old site-new
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 differ

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

  • -B ignores 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, -B alone changed nothing and -w -B together reported the files as equal.
  • A file edited on Windows has \r\n line endings and differs on every line; --strip-trailing-cr made our two copies compare equal.
  • -i ignores case, and -I REGEX ignores 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:

example output · single file
$ 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.conf

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

example output · directory
$ 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.txt

patch -R -p1 < ~/feature.patch undoes it, and removed the new health.txt again in our test. Two messages mean the command needs changing:

example output · wrong -p level (shortened)
can't find file to patch at input line 4
Perhaps you used the wrong -p or --strip option?
example output · patch already applied (shortened)
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:

diff <(sort pk1.txt) <(sort pk2.txt)
2a3
> htop
4d4
< rsync

The 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 codeMeaningExample from our test
0The inputs are the samediff old.conf same.conf
1The inputs differdiff old.conf new.conf
2Trouble: a missing file, no permissiondiff: 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:

bash
if diff -q /etc/app/app.conf /srv/backup/app.conf >/dev/null; then
  echo "unchanged"
else
  [ $? -eq 1 ] && echo "changed" || echo "diff failed"
fi

diff -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:

Debian · Ubuntu
mkdir /tmp/pristine && cd /tmp/pristine
apt download logrotate
dpkg-deb -x logrotate_*.deb .
diff -u /tmp/pristine/etc/logrotate.conf /etc/logrotate.conf

We ran these steps against a copy of logrotate.conf with three edits; the unified diff shows exactly those:

example output
--- /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.d

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

Related articles

All guides