The diff command
The diff command compares two files and prints the lines that differ…
The diff command compares two files and prints the lines that differ; diff -u shows the changes with - and + the way git does.
Taught in Intermediate, lesson 23; more in Ninja
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
diff [OPTION]... FILE1 FILE2
Compare two files line by line. Prints the lines that differ between two files. In the normal format, each change starts with a line such as 3c3 (line 3 changed), 5d4 (line 5 deleted) or 6a7 (a line added after line 6), then < marks lines from FILE1 and > lines from FILE2. diff -u prints the "unified" format that git and patches use: - for removed lines, + for added ones, with three lines around each change. diff exits with status 0 when the files are the same, 1 when they differ and 2 when something went wrong.
How to use diff
Compare two files
3c3 means line 3 changed; < is the first file’s line and > the second’s. a is a line added, d one deleted.
diff shopping.txt shopping-new.txt3c3 < bread --- > brown bread 4a5 > jam
Unified format: diff -u
The format patches and git diff use: - lines were removed, + lines added, with a little context around each change.
diff -u shopping.txt shopping-new.txt--- shopping.txt 2026-01-12 09:00:00.000000000 +0000 +++ shopping-new.txt 2026-01-12 09:00:00.000000000 +0000 @@ -1,4 +1,5 @@ milk eggs -bread +brown bread butter +jam
Just say whether they differ: diff -q
diff -q part1.txt part2.txtFiles part1.txt and part2.txt differ
Ignore case or spaces
-i ignores case, -w ignores all spaces and -b changes in the amount of space.
tr a-z A-Z < fruit.txt > FRUIT.txt && diff -i -s fruit.txt FRUIT.txtFiles fruit.txt and FRUIT.txt are identical
Options
| -u, --unified | unified format: - and + lines with context, as in git diff |
|---|---|
| -q, --brief | only say whether the files differ |
| -s, --report-identical-files | say so when the files are the same |
| -i, --ignore-case | treat upper and lower case as the same |
| -w, --ignore-all-space | ignore spaces and tabs |
| -b, --ignore-space-change | ignore changes in the amount of space |
More examples
diff -s notes.txt notes.txtFiles notes.txt and notes.txt are identical
Questions about diff
What does diff return when the files are the same?
Nothing is printed and the exit status is 0. It is 1 when the files differ and 2 when there was a problem, so scripts can use diff -q in an if.
How do I compare two folders?
diff -r folder1 folder2 compares every file in both. It isn’t available in this practice shell, which compares two files.
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.