diff: compare files and directories

Updated November 2026

diff shows the differences between two files, line by line. Administrators use it to see what changed in a configuration file, and developers to create patches.

diff [options] file1 file2 diff -r [options] dir1 dir2

Examples

diff nginx.conf nginx.conf.bak                  # classic output
diff -u nginx.conf.bak nginx.conf               # unified format: easiest to read
diff -y -W 160 a.txt b.txt                      # side by side
diff -w a.txt b.txt                             # ignore all whitespace
diff -B a.txt b.txt                             # ignore blank lines
diff -q a.bin b.bin                             # only say whether they differ
diff -ru /etc/app /root/app-backup              # compare two directory trees
diff -rq dir1 dir2                              # just list differing files

Reading unified output

--- nginx.conf.bak   2026-10-30 12:00:00
+++ nginx.conf       2026-11-02 09:14:22
@@ -10,7 +10,7 @@
 server {
     listen 443 ssl;
     server_name example.com;
-    client_max_body_size 1m;
+    client_max_body_size 20m;
     root /var/www/example;
     index index.html;
     access_log /var/log/nginx/example.log;

Lines starting with - are only in the first file, + only in the second; the @@ line gives the line numbers. A few unchanged lines around each change give context.

Patches

diff -u original.c fixed.c > fix.patch          # create a patch
patch original.c < fix.patch                    # apply it
patch -R original.c < fix.patch                 # undo it
diff -ruN project-old/ project-new/ > changes.patch
cd project-old && patch -p1 --dry-run < ../changes.patch

Compare a file on two servers

diff <(ssh web1 cat /etc/nginx/nginx.conf) <(ssh web2 cat /etc/nginx/nginx.conf)

The <( … ) syntax (bash and zsh) lets diff read the output of commands as if they were files.