diff - Linux


The diff command in Linux is a utility for comparing files line by line. It is commonly used to highlight the differences between two files, often to identify changes or updates. This tool is crucial in various scenarios like programming, where it’s used to manage different versions of source code, as well as in settings where configuration files need to be compared for troubleshooting.


The basic syntax of the diff command is as follows:

diff [OPTION]... FILES

Where FILES are two filenames or directories to be compared. OPTION are the command-line flags and options that affect the command’s operation.


  • -i or --ignore-case: Ignores case differences in file contents.
  • -w or --ignore-all-space: Ignores all white space.
  • -b or --ignore-space-change: Ignores changes in the amount of white space.
  • -B or --ignore-blank-lines: Ignores changes that are just insertions or deletions of blank lines.
  • -u, --unified: Outputs the differences in a unified format, showing a few lines of context for easier human readability.
  • -c, --context: Similar to -u, but outputs more context defaulting to three lines.
  • --normal: Outputs a normal diff, which is the default.
  • -q, --brief: Report only when the files differ.
  • -r, --recursive: Recursively compare any subdirectories.


  • Basic Comparison:
    Compare two files and display the differences.

    diff file1.txt file2.txt
  • Unified Format:
    Display differences in a unified format with 5 lines of context.

    diff -u -5 file1.txt file2.txt
  • Recursive and Brief:
    Check if files in directories differ, without detailing the differences.

    diff -qr dir1 dir2

Common Issues

  • Large Files: diff can consume significant resources when comparing large files. Splitting the files or increasing system resources might be required.
  • Binary Files: By default, diff is not useful for binary files and might output that they differ without specifics. Use tools like cmp for binary file comparisons.


diff can be combined with other commands like patch or grep for more powerful utilities:

  • Creating Patches:
    Use diff to create a patch file which can be later applied using patch.

    diff -u old_version.txt new_version.txt > update.patch
  • Filtering Output:
    Process diff output through grep to find specific changes:

    diff file1.txt file2.txt | grep '^>'
  • cmp: Compare two files byte by byte.
  • patch: Apply a diff file to an original.
  • sdiff: Display file differences side by side.

Further reading and comprehensive details can be accessed through the Linux man pages (use man diff in the terminal) or online resources such as the GNU documentation for diff.