diff compares two files line by line. Learn how to read `<` and `>`, when to use `-u` or `-y`, and how `-q` answers the yes-or-no question, by running the commands in a real terminal in your browser.
Updated: 2026-09-06
diff [options] file1 file2
It compares the two files line by line and prints only what differs.
You edited a config file and want to check it against the backup.
$ cat site.conf.bak
server_name localhost
listen 80
root /var/www/html
gzip off
$ cat site.conf
server_name example.com
listen 80
root /var/www/html
gzip on
client_max_body_size 10m
Compare them.
$ diff site.conf.bak site.conf
1c1
< server_name localhost
---
> server_name example.com
4c4,5
< gzip off
---
> gzip on
> client_max_body_size 10m
Three things to read.
| Symbol | Meaning |
|---|---|
< | A line from the first file |
> | A line from the second file |
1c1 | Line 1 changed (c for change) |
a is add and d is delete.
So 4c4,5 says line 4 of the first file became lines 4 and 5 of the second.
-u includes the surrounding lines.
$ diff -u site.conf.bak site.conf
--- site.conf.bak
+++ site.conf
@@ -1,4 +1,5 @@
-server_name localhost
+server_name example.com
listen 80
root /var/www/html
-gzip off
+gzip on
+client_max_body_size 10m
- is a removed line, + an added one, and unmarked lines are context.
This is the format git prints, so it is usually the fastest one to read.
-y puts the files side by side.
$ diff -y site.conf.bak site.conf
server_name localhost | server_name example.com
listen 80 listen 80
root /var/www/html root /var/www/html
gzip off | gzip on
> client_max_body_size 10m
| marks a changed line and > a line only the second file has.
Identical files produce nothing at all.
$ diff a.txt b.txt
$ echo $?
0
Output only ever means a difference.
When you want the answer without the detail, use -q.
$ diff -q site.conf.bak site.conf
Files site.conf.bak and site.conf differ
$ echo $?
1
The exit status is 0 for identical and 1 for different, which is what a script branches on.
diff is what you type to check what you meant to change against what actually changed.
| Situation | What to type |
|---|---|
| Compare an edited config with its backup | diff site.conf.bak site.conf |
| Read it the way git prints it | diff -u old new |
| See them side by side | diff -y old new |
| Only ask whether they differ | diff -q old new |
| Ignore case differences | diff -i a.txt b.txt |
Pair it with the backup that sed -i.bak leaves behind and you can review a substitution directly.
The argument order decides what < and > mean.
The convention is diff old new.
Reverse them and every addition reads as a deletion.
No output is success. Nothing printed simply means the files match. It is not an error, so carry on.
A missing file says so.
$ diff nothing.txt a.txt
diff: nothing.txt: No such file or directory
It needs two files.
$ diff site.conf
diff: missing operandwebterm.appthis site
Terminal Fundamentals
Learn commonly used commands
learn.webterm.appa separate site

Commands stick when they show up in a real sequence of work, not one at a time. There is a course that builds them up in order.
See the course