Linux Command Reference ยท Viewing and Searching Files
Linux diff command
Reference for the Linux diff command: compare files and directories, read unified output, ignore whitespace, understand exit status and create patches.
diff compares two files line by line and prints the instructions that would turn the first file into the second. If the files are identical it prints nothing. Given two directories it compares the files that have the same name in both and tells you which names exist on only one side.
The output format matters as much as the command. The unified format (-u) is what Git, code review tools and patch all use: lines starting with - exist only in the first file, lines starting with + only in the second, and unchanged context lines start with a space.
diff [OPTION]... FILE1 FILE2
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
diff [OPTION]... FILE1 FILE2
diff [OPTION]... DIR1 DIR2
diff [OPTION]... FILE DIR # compares FILE with DIR/FILE
A file name of - means standard input (one of the two only).
| Option | Description |
|---|---|
-u, -U N |
Unified format with 3 lines of context, or N lines. |
-c, -C N |
Older "context" format. Prefer -u. |
-y, -W N |
Side-by-side output, with total width N columns. |
-q |
Only report whether the files differ. |
-s |
Also report when files are identical. |
-r |
Recurse into subdirectories. |
-N |
Treat missing files as empty (so new files show their content). |
-i |
Ignore case differences. |
-b |
Ignore changes in the amount of white space. |
-w |
Ignore all white space. |
-B |
Ignore changes that only add or remove blank lines. |
-a |
Treat all files as text, even if they look binary. |
-x PATTERN |
Skip files whose names match the glob (with -r). |
--color=auto |
Color added and removed lines on a terminal. |
--brief |
Same as -q. |
Returns: exit status 0 when the inputs are the same, 1 when they differ, 2 when there was trouble (for example a missing file). Status 1 is not an error, so in a script with set -e use diff ... || true or test it in an if.
Reading unified output. A hunk header @@ -2,3 +2,4 @@ means: starting at line 2 the old file shows 3 lines, starting at line 2 the new file shows 4. The two header lines before the first hunk (--- old and +++ new) carry the file names and modification times.
Usage notes
- Direction matters.
diff old newdescribes how to get fromoldtonew. Swapping the arguments swaps every+and-. - Patches.
diff -u old new > change.patchproduces input forpatch. For a whole tree, usediff -ruN olddir newdirso that new and deleted files are included. - Binary files are not shown line by line;
diffprintsBinary files a and b differ. Usecmpfor byte-level checks. - Missing newline at end of file. If one side lacks the final newline,
diffadds\ No newline at end of file. This is a common source of "invisible" differences. - Ordering assumption.
difffinds a minimal set of changes by line order. For two unsorted lists where order does not matter, sort both first, or usecomm. - Process substitution lets you compare command output without temp files:
diff <(sort a) <(sort b). - Line endings. Files with Windows line endings (CRLF) differ from LF files on every line.
--strip-trailing-crignores the carriage returns. - Useful in tests.
diff -u expected.txt <(my-command)prints a readable failure message and returns 1 on any difference.
Related
cmp,comm,patch,md5sumandsha256sum- Lessons: diff, comm and patch, Git status and diff