Updated: 17 October 2026 · Applies to: GNU diffutils 3.7 to 3.12 (AlmaLinux and Rocky Linux 9 and 10, Debian 13, Ubuntu 24.04 and 26.04), Vim 8.2 to 9.1, Git 2.53, Meld 3.22
To compare two files in Linux, run diff file1 file2. It prints only the lines that differ; no output means the files are identical. For output that is easier to read, use diff -u file1 file2, add -r to compare whole directories, or open both files side by side with vimdiff file1 file2. On a desktop, Meld shows the differences graphically. The same commands work on Ubuntu, Debian, AlmaLinux and Rocky Linux.
If the shell answers diff: command not found (as on minimal AlmaLinux images), install the package: sudo dnf install diffutils or sudo apt install diffutils.
1. diff: what changed between two files
Our examples compare two versions of a small configuration file. Output on Ubuntu 26.04 (diffutils 3.12):
$ diff old.conf new.conf 2c2 < Listen 80 --- > Listen 8080 4a5 > KeepAliveTimeout 5
- Lines with
<are only in the first file, lines with>only in the second. 2c2: line 2 was changed.4a5: after line 4 of the first file, line 5 of the second was added. Admeans deleted.
2. Unified and side-by-side output
The unified format shows each change with a few lines of context. - marks removed lines and + added lines; this is also the format of patch files. Add --color for colours in a terminal.
$ diff -u old.conf new.conf --- old.conf 2026-10-12 02:43:33.494846225 +0000 +++ new.conf 2026-10-12 02:43:33.497846231 +0000 @@ -1,5 +1,6 @@ ServerName example.com -Listen 80 +Listen 8080 Timeout 300 KeepAlive On +KeepAliveTimeout 5 MaxKeepAliveRequests 100
To see both files in two columns, use -y (-W sets the width). | marks a changed line, > and < lines in only one file:
$ diff -y --suppress-common-lines -W 70 old.conf new.conf Listen 80 | Listen 8080 > KeepAliveTimeout 5
sdiff gives a similar view and can merge the files interactively: see sdiff command examples.
3. Ignore case, spaces and blank lines
-iignores upper and lower case,-wignores all white space,-Bignores blank lines. In our test,KeepAlive onandkeepalive ondiffered with plain diff and matched withdiff -i -w.-qonly reports whether the files differ:Files old.conf and new.conf differ.-salso reports identical files:Files old.conf and old.conf are identical.
4. Compare two files in a Bash script
diff exits with 0 when the files are the same, 1 when they differ and 2 on an error (for example diff: missing.conf: No such file or directory). That makes it easy to use in a script:
if diff -q old.conf new.conf >/dev/null; then
echo same
else
echo changed
fi
To compare the output of commands instead of files, use process substitution. For two lists in a different order, such as users1 (alice, bob, carol) and users2 (carol, alice, dave), sort them first; comm -3 then prints the entries found in only one list (those of the second list are indented):
$ comm -3 <(sort users1) <(sort users2) bob dave
A file on another server can be compared the same way: diff <(ssh user@203.0.113.10 cat /etc/nginx/nginx.conf) /etc/nginx/nginx.conf. More in compare local and remote files.
5. cmp and checksums: are two files identical?
$ cmp old.conf new.conf old.conf new.conf differ: char 33, line 2
cmp works on any file, including binaries and images, and stops at the first difference. cmp -s prints nothing and only sets the exit code. For large files, or files on two servers, compare checksums instead: run sha256sum file on both sides and compare the strings (see verify files with checksums).
6. Compare two directories recursively
$ diff -rq site site-backup Files site/about.html and site-backup/about.html differ Only in site: cache.tmp Only in site/img: logo.png Only in site-backup: old.txt
-r goes through all subfolders and -q lists only the names. Leave out -q to see the changed lines as well, skip files with --exclude='*.tmp', or save all differences as a patch with diff -ruN site-backup site > site.patch (-N treats missing files as empty). More on folders, including Meld: compare two directories with diff and Meld.
7. vimdiff: compare and edit side by side
- Install Vim if needed:
sudo apt install vimorsudo dnf install vim-enhanced. - Open the files:
vimdiff old.conf new.conf(the same asvim -d old.conf new.conf). Changed lines are highlighted and identical blocks are folded. - Jump with
]cto the next change and[cto the previous one. - Copy a change with
do(obtain it from the other file) ordp(put it into the other file). After manual edits,:diffupdaterefreshes the highlighting. - Switch between the panes with
Ctrl+Ww, then save and quit both with:wqa, or leave without saving with:qa!.
vimdiff compares files, not folders; use diff -r for those.
8. Word-level differences with git diff
Git can compare any two files, even outside a repository, and mark the changed words inside a line:
$ git diff --no-index --word-diff old.conf new.conf
...
Listen [-80-]{+8080+}
Timeout 300
KeepAlive On
{+KeepAliveTimeout 5+}
This helps with long lines, such as a changed option in a long command line. Install Git with sudo apt install git or sudo dnf install git.
9. Graphical diff tools on a Linux desktop
- Meld compares two or three files or folders and lets you copy changes with one click:
sudo apt install meld(Meld 3.22 on Ubuntu 24.04, 26.04 and Debian 13). On AlmaLinux and Rocky Linux it is in EPEL:sudo dnf install epel-release, thensudo dnf install meld. - KDiff3 (
kdiff3) and Kompare (kompare) are KDE tools in the same repositories.
A server usually has no desktop: copy the files to your computer, or use vimdiff over SSH. Be careful with online diff websites: never paste configuration files that contain passwords, keys or customer data.
Other Unix systems
diff is a POSIX standard tool, so diff, diff -u and diff -r also work on macOS and the BSDs, with the same exit codes. Options that are not in the standard, such as -q, -y and --color, are GNU extensions and may be missing or behave differently there; check man diff. For grep, sort, cut and diff together, see Linux text processing commands.
Official documentation: GNU diffutils manual, Vim diff mode and Meld.