Linux command list

diff command: show what changed between two files

__ __ _ _____
\ \ / /__| |_|_ _|__ _ __ _ __ ___
\ \ /\ / / _ \ '_ \| |/ _ \ '__| '_ ` _ \
\ V V / __/ |_) | | __/ | | | | | |
\_/\_/ \___|_.__/|_|\___|_| |_| |_| |_
 
A sandbox for trying diff. Nothing here can touch your real files.
user@webterm:~/project$
 

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

Syntax

diff [options] file1 file2

It compares the two files line by line and prints only what differs.

Try it first

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.

SymbolMeaning
<A line from the first file
>A line from the second file
1c1Line 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.

Easier formats

-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.

Just tell me whether they differ

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.

When you actually reach for it

diff is what you type to check what you meant to change against what actually changed.

SituationWhat to type
Compare an edited config with its backupdiff site.conf.bak site.conf
Read it the way git prints itdiff -u old new
See them side by sidediff -y old new
Only ask whether they differdiff -q old new
Ignore case differencesdiff -i a.txt b.txt

Pair it with the backup that sed -i.bak leaves behind and you can review a substitution directly.

Things that trip people up

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 operand

Practise it hands-on

webterm.appthis site

learn.webterm.appa separate site

>_WEBTERM LEARN

WebTerm Learn: from one command to actually using it

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

Related commands

git / cat / sed / less

Frequently asked questions

Which file is `<` and which is `>`?
`<` is the first file and `>` is the second. Pass them as `diff old new` and `<` is what it was, `>` is what it became.
What do `1c1` and `4c4,5` mean?
They locate the change. `c` is change, `a` is add, `d` is delete, and the numbers are the line numbers in each file.
It printed nothing. Did it fail?
No, the files are identical. diff prints only differences, and its exit status is 0 when they match and 1 when they do not.
Why add `-u`?
It shows a few unchanged lines around each change, so you can tell where you are. It is also the format git and patch use.
How is this different from git diff?
`diff` compares two files you name. `git diff` compares your working files against what git recorded, so you never pick the other side. For comparing against a backup you keep yourself, use diff.