Back to the command reference

diff

FilesFundamentals In simulator In real Linux

Compare files line by line

Name: diff = difference

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

diff compares two files line by line and prints the changes needed to turn the first into the second. The unified format (-u) is the most readable and is what patches and git diff use.

Exit status: 0 same, 1 different, 2 trouble, so scripts can use it to test whether files match.

Syntax

diff [OPTION]... FILE1 FILE2

Common options

-u / --unifiedUnified format, as used by patches
-y / --side-by-sideShow the two files side by side
-r / --recursiveCompare directories recursively
-N / --new-fileWith -r: treat a file missing on one side as empty
-q / --briefOnly report whether files differ
-i / --ignore-caseIgnore case
-w / --ignore-all-spaceIgnore all white space
-b / --ignore-space-changeIgnore changes in the amount of white space
-B / --ignore-blank-linesIgnore changes that are only blank lines
-x / --exclude PATTERNWith -r: skip files matching PATTERN
--colorColour added and removed lines

Examples

  • $ diff old.conf new.conf

    See how two config files differ

    Explain
  • $ diff -u old.py new.py > fix.patch

    Create a patch file

    Explain
  • $ diff -rq dir1 dir2

    List files that differ between two directories

    Explain

Related