Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to compare the files available in two directories using diff command in Linux?
The diff command in Linux is a powerful tool for comparing files and directories. When working with two directories containing multiple files, diff helps identify which files are unique to each directory, which files are common, and what differences exist between files with the same name.
Understanding the diff Command
The diff command (short for difference) compares files line by line and can also compare entire directories. When comparing directories, it identifies files that exist in one directory but not the other, as well as files that differ in content.
Example Setup
Let's work with two directories d1 and d2 containing different files. First, let's examine the directory structure −
ls -ltr
total 0 drwxr-xr-x 5 immukul staff 160 Jul 5 20:03 d1 drwxr-xr-x 4 immukul staff 128 Jul 5 20:03 d2
Contents of directory d1 −
ls -ltr d1
total 0 -rw-r--r-- 1 immukul staff 0 Jul 5 20:03 1.txt -rw-r--r-- 1 immukul staff 0 Jul 5 20:03 3.txt -rw-r--r-- 1 immukul staff 0 Jul 5 20:03 5.txt
Contents of directory d2 −
ls -ltr d2
total 0 -rw-r--r-- 1 immukul staff 0 Jul 5 20:03 2.txt -rw-r--r-- 1 immukul staff 0 Jul 5 20:03 3.txt
Comparing Directories with diff
Basic Directory Comparison
To compare two directories and identify unique files, use the -q (quiet) option −
diff -q d1 d2
Only in d1: 1.txt Only in d2: 2.txt Only in d1: 5.txt
Recursive Directory Comparison
For directories with subdirectories, use the -r (recursive) option to compare all nested content −
diff -qr d1 d2
Only in d1: 1.txt Only in d2: 2.txt Only in d1: 5.txt
Common diff Options for Directory Comparison
| Option | Description | Example |
|---|---|---|
| -q | Quiet mode; reports only whether files differ | diff -q d1 d2 |
| -r | Recursively compare subdirectories | diff -r d1 d2 |
| -u | Unified format showing context | diff -u d1 d2 |
| --exclude | Exclude files matching pattern | diff --exclude="*.tmp" d1 d2 |
Practical Examples
Finding Common Files
To identify files that exist in both directories, combine diff with other commands −
comm -12 <(ls d1 | sort) <(ls d2 | sort)
Detailed File Differences
To see actual content differences in files with the same name −
diff -u d1/3.txt d2/3.txt
Key Points
The
-qoption provides a summary of differences without showing file contentUse
-rfor recursive comparison of nested directoriesFiles with identical names but different content will be flagged as "differ"
The
diffcommand only compares file content, not metadata like timestamps
Conclusion
The diff command is essential for comparing directories and identifying file differences in Linux. Using options like -q and -r, you can quickly determine which files are unique to each directory and which files have different content, making it invaluable for system administration and file management tasks.
