diff

file managementLinux/Unix
The diff command is one of the most frequently used commands in Linux/Unix-like operating systems. diff Compare files line by line

Quick Reference

Command Name:

diff

Category:

file management

Platform:

Linux/Unix

Basic Usage:

diff file1.txt file2.txt

Common Use Cases

  • 1

    File comparison

    Compare two files line by line to identify differences

  • 2

    Code review

    Review changes made to source code files

  • 3

    Configuration management

    Track changes to configuration files between versions

  • 4

    Patch creation

    Generate patch files that can be applied to update files

Syntax

diff [OPTION]... FILES

Options

Option Description
-q, --brief Report only when files differ
-s, --report-identical-files Report when two files are identical
-c, -C NUM, --context[=NUM] Output NUM (default 3) lines of context
-u, -U NUM, --unified[=NUM] Output NUM (default 3) lines of unified context
-y, --side-by-side Output in two columns
-i, --ignore-case Ignore case differences in file contents
-w, --ignore-all-space Ignore all white space
-B, --ignore-blank-lines Ignore changes where lines are all blank
-r, --recursive Recursively compare any subdirectories found
--color[=WHEN] Colorize the output; WHEN can be 'never', 'always', or 'auto'
-a, --text Treat all files as text
--help Display help information
--version Display version information

Examples

How to Use These Examples

The examples below show common ways to use the diff command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

# Basic Examples Basic
diff file1.txt file2.txt
Compare two text files and show differences.
diff -y file1.txt file2.txt
Show differences in side-by-side format.
diff -u file1.txt file2.txt
Show differences in unified format. # Advanced Examples Advanced diff -r dir1 dir2 Recursively compare two directories. diff -i file1.txt file2.txt Ignore case differences when comparing. diff -w file1.txt file2.txt Ignore whitespace when comparing. diff -B file1.txt file2.txt Ignore blank lines when comparing. diff -u file1.txt file2.txt > changes.patch Create a patch file that can be applied with the patch command. diff --color=always file1.txt file2.txt | less -R Show colored output for better readability.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

The diff command is a powerful tool for comparing files line by line in Linux and Unix-like operating systems. Output Format: - By default, diff uses a special notation where a letter (a for add, c for change, d for delete) followed by line numbers indicates the type of difference. - The < symbol indicates content from the first file - The > symbol indicates content from the second file - The unified format (-u) is widely used in software development as it's more compact and readable - The side-by-side format (-y) is useful for visual comparison Patch Creation: One of the most common uses of diff is to create patch files that can be applied with the patch command: 1. Generate the patch: diff -u original.txt modified.txt > changes.patch 2. Apply the patch: patch original.txt < changes.patch Directory Comparison: When used with the -r option, diff recursively compares directories, showing differences between corresponding files. Ignore Options: Various ignore options make diff more flexible: - -i ignores case differences - -w ignores all whitespace - -b ignores changes in amount of whitespace - -B ignores blank lines - -E ignores tab expansion Exit Status: - 0: Files are identical - 1: Files are different - 2: An error occurred Alternatives: - colordiff: A wrapper around diff that adds color to the output - vimdiff: Opens files in Vim with differences highlighted - git diff: Specialized for Git repositories - meld: Graphical diff tool with merge capabilities

Tips & Tricks

1

Use the -r option to compare directories recursively

2

Use the -q option to suppress output and return a non-zero exit status if files differ

3

Use the -s option to report only when files differ

4

Use the -y option to output in a side-by-side format

5

Use the -W width option to set the output width

Common Use Cases

File comparison

Compare two files line by line to identify differences

Code review

Review changes made to source code files

Configuration management

Track changes to configuration files between versions

Patch creation

Generate patch files that can be applied to update files

Quality assurance

Verify if expected changes have been correctly implemented

Related Commands

These commands are frequently used alongside diff or serve similar purposes:

Use Cases

1

File comparison

Compare two files line by line to identify differences

2

Code review

Review changes made to source code files

3

Configuration management

Track changes to configuration files between versions

4

Patch creation

Generate patch files that can be applied to update files

5

Quality assurance

Verify if expected changes have been correctly implemented

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the diff command works in different scenarios.

$ diff
View All Commands