Compare files · Linux: diff command

How to Diff Two Files in Linux

Every Linux and macOS system ships with diff. It compares two files line by line and prints what you would have to change to turn the first into the second. Learn four flags and you will never need anything else for text files.

The basic command

Give diff the original first and the changed file second. Lines that exist only in the first file are prefixed with a less-than sign, lines only in the second with a greater-than sign, and a header such as 3c3 says line 3 changed (a for added, d for deleted).

$ diff app.conf app.conf.new
3c3
< workers = 4
---
> workers = 8
7a8
> timeout = 30

Unified output, the format everyone shares

diff -u prints the format used by Git and patch: a few lines of context, removed lines with a minus, added lines with a plus. It is the form to paste into a ticket or a review, and the form the online diff checker produces.

$ diff -u app.conf app.conf.new
--- app.conf
+++ app.conf.new
@@ -1,7 +1,8 @@
 [server]
-workers = 4
+workers = 8
 port = 8080
+timeout = 30

Side by side, colour, and quiet mode

diff -y shows the files in two columns, and --suppress-common-lines hides identical lines so only changes remain. Modern diffutils accept --color=always for red and green output. diff -q reports only whether the files differ, which is what you want in scripts, and the exit code says the same thing: 0 identical, 1 different, 2 error.

$ diff -y --suppress-common-lines app.conf app.conf.new
workers = 4                | workers = 8
                           > timeout = 30
$ diff -q a.txt b.txt && echo same || echo different

Ignore whitespace and case

Reformatting makes every line look changed. -w ignores all whitespace, -b ignores changes in the amount of whitespace, -B ignores blank lines and -i ignores case. Combine them: diff -uwB.

$ diff -uw config.yaml config.formatted.yaml

Compare two directories

diff -r walks both trees and diffs every pair of files with the same name; add -q to list only which files differ, and --brief --exclude to skip build output. For a visual tree comparison, meld and kdiff3 are one package install away, and vimdiff opens two files in a split editor with differences highlighted.

$ diff -rq src/ src-backup/ --exclude=node_modules
Files src/app.js and src-backup/app.js differ
Only in src/: newfile.js
Compare two texts online Paste both versions. Runs in your browser, nothing is uploaded.

FAQ

How do I save a diff to a file and apply it later?

diff -u old new > change.patch, then patch old < change.patch on the other machine. Git users can apply the same file with git apply change.patch.

Why does diff say the whole file changed?

Usually line endings (Windows CRLF against Unix LF) or a tab-to-spaces conversion. Run dos2unix on one copy, or compare with diff -w to ignore whitespace and confirm.

Can I diff two files that are not on the same machine?

Copy both to one place, or paste their contents into the online diff checker; nothing is uploaded and the result reads like diff -u.