Text and processing

The diff command

Compares two files or folders line by line and shows the differences. It is the basis of patches and version control. Its exit status is 0 if they are equal and 1 if they differ.

Syntax

diff [opciones] archivo1 archivo2Try it in the terminal →

Most useful options

-uUnified format: context lines with - and +. It is the one used by patches and git
-rCompares whole folders, subfolders included, and reports which files are missing on each side
-qOnly says whether the files differ, without showing the lines
-iIgnores upper and lower case when comparing
-wIgnores white space: useful if only the indentation changed
-BIgnores changes that only affect blank lines

Copy-ready examples

$ diff a.txt b.txtshows the lines that change, with their number and the before (<) and after (>).

Example output

2c2
< dos
---
> dos y medio
$ diff -u viejo.conf nuevo.confunified format, the easiest to read and the one patches use.

Example output

--- viejo.conf 2026-11-02 18:40:01.000000000 +0100
+++ nuevo.conf 2026-11-03 09:12:44.000000000 +0100
@@ -1,3 +1,3 @@
 puerto=8080
-modo=debug
+modo=produccion
 log=/var/log/app.log
$ diff -r proyecto-v1 proyecto-v2compares two whole folders and says which files exist in only one of them.

Example output

Only in proyecto-v1: viejo.py
Only in proyecto-v2: nuevo.py
$ diff -w codigo.c codigo-ident.cignores spaces: if only the indentation changed, nothing is shown.
$ diff -q a.txt b.txtjust confirms they differ, useful inside an if.

Example output

Files a.txt and b.txt differ

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
diff: nofile: No such file or directoryOne of the files does not exist or is misspelled.List the folder with ls and use the exact name.
diff: dir1/a.txt: No such file or directoryYou compared a file with a folder: diff looks inside that folder for a file with the same name and does not find it.Write the full paths of both files, or use diff -r if you want to compare folders.
Binary files bin1 and bin2 differAt least one of the files has non-text bytes, so diff shows no lines.Use cmp to see the first different byte, or xxd to look at the bytes.

Typical mistakes

  • Windows line endings (CRLF) against Linux ones (LF) make every line look changed: try --strip-trailing-cr.

Differences from similar commands

  • cmp: only reports the first byte that differs, without showing lines. It works for binaries.
  • comm: compares two sorted lists in three columns: only in A, only in B and in both.
  • git diff: compares the repository with its saved versions. diff compares any two paths.

Related commands

FAQ

How do I make a patch someone else can apply?

Run diff -u original new > changes.patch. The other person applies it with patch < changes.patch inside the folder; if the paths do not match, try patch -p1.

Why does diff mark every line as changed?

Usually it is the line ending: one file has CRLF and the other LF. file shows the type of each one.

More Text and processing commands

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course