Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 90System · Linux

How to compare two files or folders

"What changed in this config since yesterday?", "Are the two web servers really the same?", "Is the copy identical to the original?" — comparing is daily admin work. This how-to uses diff for text, checksums for any file, diff -r and rsync for whole folders, compares files on two servers without copying them, and shows the unified diff format you see in every code review.

  • 5 Lessons
  • 8 Commands

$ run as a normal user# run as root (or with sudo)

90.1

The quick answer

diff -u nginx.conf.bak nginx.conf
diff -rq /etc/nginx /tmp/nginx-old
sha256sum backup.tar.gz /mnt/copy/backup.tar.gz
diff <(ssh web1 cat /etc/nginx/nginx.conf) <(ssh web2 cat /etc/nginx/nginx.conf)

90.2

Step by step

  1. Text files — the unified diff:
diff -u nginx.conf.bak nginx.conf
--- nginx.conf.bak	2026-11-18 09:10:00
+++ nginx.conf	2026-11-19 10:02:41
@@ -12,7 +12,7 @@
     keepalive_timeout 65;
-    client_max_body_size 1m;
+    client_max_body_size 50m;

- = only in the first file, + = only in the second, @@ = the line numbers. Add --color for colour; -w ignores whitespace changes.

  1. Folders:
diff -rq /etc/nginx /tmp/nginx-old
Files /etc/nginx/nginx.conf and /tmp/nginx-old/nginx.conf differ
Only in /etc/nginx/conf.d: shop.conf
diff -ru /etc/nginx /tmp/nginx-old > nginx-changes.diff

-q = only the names; -u = the full changes of every file.

  1. Binary files and copies — compare checksums (exact, fast, works for any size):
sha256sum backup.tar.gz /mnt/copy/backup.tar.gz
cmp -s file1.bin file2.bin && echo same || echo different
  1. Big folder trees (millions of files) — let rsync compare by checksum without copying:
rsync -rcn -i --delete /srv/data/ /mnt/backup/data/ | head

No output = the trees are equal.

  1. Two servers, without copying files — process substitution <( … ):
diff -u <(ssh web1 cat /etc/nginx/nginx.conf) <(ssh web2 cat /etc/nginx/nginx.conf)
diff <(ssh web1 rpm -qa | sort) <(ssh web2 rpm -qa | sort)

The second line shows which packages differ between the two servers.

  1. Changes of config files over time: keep /etc in Git (etckeeper) or compare with the package original:
rpm -V nginx
S.5....T.  c /etc/nginx/nginx.conf

5 = content changed, c = config file. On Debian/Ubuntu: dpkg --verify nginx.

  1. Sorted lists (users, packages, IPs) — comm shows only-in-A, only-in-B and both:
comm -3 <(sort list-a.txt) <(sort list-b.txt)

90.3

Check that it worked

You can name every difference — or prove there is none: diff prints nothing and returns exit code 0, checksums match, and the rsync dry run lists nothing.

diff -q a.conf b.conf && echo "identical"

Commands in this lesson

90.4

Common problems

ProblemCauseFix
Every line is differentWindows line endings (\r\n) or different indentation.diff --strip-trailing-cr, -w; convert with dos2unix.
Binary files … differNot text.Use checksums or cmp; for archives compare the file list.
diff -r is very slowHuge trees.rsync -rcn -i or compare checksum lists.
The diff of two servers shows only order changesLists not sorted.sort both sides first.
ssh asks for a password twiceNo key login.Set up SSH keys first.
Same content, diff says differentHidden whitespace or encoding (UTF-8 BOM).cat -A to see hidden characters.

90.5

Go further

1In a unified diff, what do lines starting with - and + mean?

- is only in the first (old) file, + only in the second (new) file.

2How do you compare one config file on two servers without copying it?

diff <(ssh web1 cat FILE) <(ssh web2 cat FILE).

All commands in this chapter