diff file1 file2 is the fastest way to compare two files in Linux — it prints only the lines that differ. Five tools cover the rest: cmp for byte-exact and binary comparison, comm for set-style comparison of sorted files, vimdiff for a visual side-by-side view, and sdiff for interactive merging.
Table of Contents
Using the diff Command
The diff command is a staple for comparing files in Linux. It highlights line-by-line differences, making it easy to identify changes.
Basic Usage:
diff file1.txt file2.txt
What most scripts get wrong is treating diff‘s output like a normal command’s — checking only for empty output, or ignoring the exit code. diff has three distinct exit codes, run here on identical files, files that differ, and a missing file:
$ diff same1.txt same2.txt; echo "exit: $?"
exit: 0
$ diff f1.txt f3.txt; echo "exit: $?"
2c2
< banana
---
> blueberry
exit: 1
$ diff f1.txt nonexistent.txt; echo "exit: $?"
diff: nonexistent.txt: No such file or directory
exit: 2
0 means no differences, 1 means differences were found, and 2 means diff itself failed (bad path, unreadable file). A script that only checks “exit code non-zero = error” will misreport ordinary differences as failures — check for 1 specifically when you mean “files differ.”
Common Options:
-u shows differences in unified format, with a few lines of context and a +/- prefix per changed line — this is the format used in almost every code review tool:
$ diff f1.txt f2.txt
2c2
< banana
---
> blueberry
$ diff -u f1.txt f2.txt
--- f1.txt
+++ f2.txt
@@ -1,4 +1,4 @@
apple
-banana
+blueberry
cherry
grape
The default format is more compact for a single-line change; -u is easier to read once more than a couple of lines are involved, because it keeps unchanged context lines visible around the change.
To compare entire directories instead of single files, add -r:
$ diff -r dir1 dir2
Only in dir1: a.txt
Only in dir2: b.txt
diff -r dir1/changed.txt dir2/changed.txt
1c1
< hello v1
---
> hello v2
It reports files that exist in only one side, plus a per-file diff for anything present in both but changed.
Using the cmp Command
While diff focuses on text differences, cmp compares files byte by byte — and it’s the one that actually works on binary files, where diff just tells you they differ:
$ diff b1.bin b2.bin
Binary files b1.bin and b2.bin differ
$ cmp b1.bin b2.bin
b1.bin b2.bin differ: char 5, line 1
$ cmp -l b1.bin b2.bin
5 1 2
cmp names the exact byte offset and line where the files first diverge; cmp -l lists every differing byte position along with the octal value on each side. That’s the detail diff won’t give you on a binary file.
Using the comm Command
The comm command compares two sorted files line by line and outputs three columns: lines unique to the first file, lines unique to the second, and lines common to both.
Basic Usage:
comm file1.txt file2.txt
Tip: Ensure both files are sorted before using comm for accurate results:
sort file1.txt -o file1.txt
sort file2.txt -o file2.txt
comm file1.txt file2.txt
Using vimdiff for Visual Comparison
For a more visual approach, vimdiff displays both files side by side within the Vim editor, highlighting differences with color.
vimdiff file1.txt file2.txt
You can navigate the files using Vim’s commands, and changed blocks are color-coded for clarity.
Bonus: Using sdiff for Side-by-Side Comparison
The sdiff command provides a plain-text side-by-side view and can merge differences interactively:
sdiff file1.txt file2.txt
BSD diff vs. GNU diff
Every command above was run on this machine’s Apple diff, which is based on FreeBSD diff, not Linux’s GNU diffutils — there’s no Linux box here to test against directly. GNU diff’s own man page, fetched in August 2026, documents the same three exit codes verbatim: “Exit status is 0 if inputs are the same, 1 if different, 2 if trouble” — and lists -u/--unified and -r/--recursive with the same meanings shown above, per the GNU diff man page. Where the two diverge is deeper flags (GNU-only --color, for instance, isn’t in BSD diff) — if a script needs to run on both a Mac and a Linux server, stick to the options demonstrated here.
Whichever diff and cmp are on your PATH can be checked against the exit-code, -u, -r and cmp byte-offset examples above: a script writes its own sample files into a scratch directory, runs those checks there, and removes the directory on exit. The scripts for my other articles are indexed by category on The Verify Scripts.
For scripting or automation, see how to run a bash script to wire any of these commands into a repeatable check.