Updated: 30 November 2026 · Applies to: Linux with GNU diffutils and coreutils (tested on AlmaLinux 10.2 with diffutils 3.10, coreutils 9.5, rsync 3.5 and Git 2.52; the same commands work on Rocky Linux, Ubuntu and Debian)
The main commands for comparing files in Linux are diff (which lines differ), cmp (are two files byte for byte the same) and sha256sum (compare checksums). For directories, use diff -rq to list the files that differ, or rsync in dry-run mode to see what would need copying. This page is a quick reference with real output; for full walkthroughs see How to Compare Files in Linux: diff, cmp, vimdiff and Meld and How to Compare Two Directories With diff and Meld.
If a command is missing on a minimal server, install diffutils with dnf install diffutils or apt install diffutils.
Quick reference
diff file1 file2: the lines that differ (no output means identical).diff -u file1 file2: unified format with context, the format of patch files.diff -y file1 file2: side by side.diff -q file1 file2: only says whether they differ.diff -rq dir1 dir2: which files differ or exist on one side only.cmp file1 file2: the first differing byte, works for binary files.sha256sum file1 file2: checksums, useful when the files are on different servers.comm file1 file2: lines unique to each of two sorted lists.rsync -rcn --delete -i dir1/ dir2/: what would change to make dir2 match dir1.git diff --no-index file1 file2: Git-style diff of any two files, even outside a repository.vimdiff file1 file2or Meld: interactive, side by side.
Compare two files
The examples compare two versions of a small configuration file.
diff old.conf new.conf
Output:
1c1 < Listen 80 --- > Listen 8080 3a4 > Timeout 60
Lines with < are only in the first file, lines with > only in the second. 1c1 means line 1 changed; 3a4 means line 4 of the second file was added after line 3. diff exits with status 0 when the files match and 1 when they differ, which is handy in scripts.
Side by side, showing only the changed lines:
diff -y --suppress-common-lines -W 60 old.conf new.conf
Output:
Listen 80 | Listen 8080
> Timeout 60
To confirm two files are identical and say so:
diff -s old.conf copy.conf
Output:
Files old.conf and copy.conf are identical
Binary files and checksums
cmp old.conf new.conf
Output:
old.conf new.conf differ: char 10, line 1
cmp -s prints nothing and only sets the exit status, so cmp -s a b && echo identical works in a script. For a file on two servers, compare checksums instead of copying it across:
sha256sum old.conf new.conf
Output:
db93bb82125b40ea9564f8dbe5666e55d2362eb3592b1fead157a7e307ec9fd9 old.conf 6fdf8e3f2c7cdd27bac367ed9b8d9f3498102cce32f1b855e2e40270822d2786 new.conf
More on checksums in Learn How to Generate and Verify Files with MD5 Checksum in Linux.
Compare two lists with comm
comm needs both files sorted. Column 1 is only in the first file, column 2 only in the second, column 3 in both. -3 hides the common lines:
comm -3 a.txt b.txt
Output (dave is indented because it is only in the second file):
alice dave
comm -12 a.txt b.txt prints only the lines in both. Sort first with sort -o a.txt a.txt if needed.
Compare two directories
diff -rq backup/site site
Output:
Files backup/site/config.php and site/config.php differ Only in site: new.jpg Only in backup/site: old.txt
Add -x '*.log' or --exclude=cache to skip files that always differ. To see what rsync would change to make site match backup/site, without changing anything (-n is the dry run, -c compares by checksum, not by time and size):
rsync -rcn --delete -i backup/site/ site/
Output:
*deleting new.jpg >fc.T...... config.php >f+++++++++ old.txt
*deleting is a file only in the target, >fc a file whose content differs, >f+++++++++ a file that would be copied new. The trailing slashes matter: they compare the contents of the two folders. See 13 rsync Command Examples for Linux for more options.
To compare only the file names in two trees:
diff <(cd backup/site && find . | sort) <(cd site && find . | sort)
Git-style output for any two files
git diff --no-index old.conf new.conf
This gives the familiar @@ hunks with - and + lines and works outside a Git repository. --stat prints only a summary such as 1 file changed, 2 insertions(+), 1 deletion(-).
For more text tools that pair well with these (sort, uniq, cut), see Linux Text Processing Commands: grep, sort, uniq, wc, cut, tr and diff with Examples.
Official documentation: GNU diffutils manual.