How to use diff Command in Linux

The diff command in Linux is a powerful command-line utility used to compare the contents of two files or directories line by line and display the differences between them. This tool is essential for developers, system administrators, and anyone working with text files to identify changes, track modifications, and analyze variations in code, configuration files, or documents.

Basic File Comparison

The most fundamental use of the diff command is comparing two files. The basic syntax is:

diff file1 file2

Example

Consider two files with slight differences:

$ diff file1.txt file2.txt
2c2
< Original line 2
---
> Modified line 2
4d3
< This line was deleted

Directory Comparison

The diff command can recursively compare entire directories using the -r option:

diff -r directory1 directory2

This compares all files within the directories and their subdirectories, showing which files differ and how.

$ diff -r project_v1 project_v2
diff -r project_v1/config.txt project_v2/config.txt
3c3
< debug=false
---
> debug=true
Only in project_v2: new_feature.py

Common Options

Option Description Usage
-i Ignore case differences diff -i file1 file2
-w Ignore whitespace changes diff -w file1 file2
-b Ignore space and tab changes diff -b file1 file2
-u Unified format output diff -u file1 file2
-c Context format output diff -c file1 file2
-q Report only if files differ diff -q file1 file2

Output Formats

Unified Format (-u)

The unified format is widely used and shows changes with + and - symbols:

$ diff -u original.txt modified.txt
--- original.txt	2023-01-01 10:00:00.000000000 +0000
+++ modified.txt	2023-01-01 11:00:00.000000000 +0000
@@ -1,4 +1,4 @@
 line 1
-old line 2
+new line 2
 line 3
 line 4

Context Format (-c)

The context format shows several lines around the changes for better understanding:

$ diff -c file1.txt file2.txt
*** file1.txt	2023-01-01 10:00:00.000000000 +0000
--- file2.txt	2023-01-01 11:00:00.000000000 +0000
***************
*** 1,4 ****
  line 1
! original content
  line 3
  line 4
--- 1,4 ----
  line 1
! modified content
  line 3
  line 4

Interpreting diff Output

The standard diff output uses specific symbols to indicate changes:

  • c − Change (lines were modified)

  • a − Add (lines were added)

  • d − Delete (lines were removed)

  • < − Lines from the first file

  • > − Lines from the second file

Line numbers before and after the change are shown as line_range action line_range.

Practical Examples

Ignoring Whitespace Differences

# Ignore all whitespace differences
diff -w config_old.txt config_new.txt

# Ignore only trailing spaces and tabs
diff -b script_v1.sh script_v2.sh

Quick File Comparison

# Check if files differ without showing details
diff -q file1.txt file2.txt
Files file1.txt and file2.txt differ

Conclusion

The diff command is an indispensable tool for comparing files and directories in Linux. Its various options allow for flexible comparisons while ignoring irrelevant differences like whitespace or case. Understanding diff output formats helps users quickly identify and analyze changes between files, making it essential for version control, debugging, and system administration tasks.

Updated on: 2026-03-17T09:01:38+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements