How to Compare Two Directories in Linux Using Diff and Rsync Commands


14 views

When working with Linux systems, developers often need to compare two directory structures to identify:

  • Files existing in one directory but not the other
  • Modified files with different content
  • Permission or timestamp differences

The simplest method uses the standard diff command with recursive comparison:

diff -qr /path/to/dir1 /path/to/dir2

Key flags:
-q reports only when files differ
-r enables recursive directory comparison

# Compare file names only
diff -qr --brief /path/to/dir1 /path/to/dir2

# Show side-by-side comparison
diff -y -r /path/to/dir1 /path/to/dir2

# Generate unified diff format
diff -u -r /path/to/dir1 /path/to/dir2 > changes.patch

Rsync's dry-run mode reveals differences without actual file transfer:

rsync -n -avic --delete /path/to/dir1/ /path/to/dir2/

Breakdown of flags:
-n = dry run
-a = archive mode
-v = verbose
-i = itemize changes
-c = checksum comparison

# Compare production and staging environments
diff -r --exclude="*.log" --exclude="tmp/" /var/www/prod /var/www/staging

# Sample output:
Only in /var/www/prod: config_prod.php
Files /var/www/prod/index.php and /var/www/staging/index.php differ
Only in /var/www/staging: new_feature.js

For performance-sensitive comparisons:

# Compare only file names and sizes first
find /path/to/dir1 -type f -printf "%P %s\n" | sort > dir1.list
find /path/to/dir2 -type f -printf "%P %s\n" | sort > dir2.list
diff dir1.list dir2.list

# Then focus detailed comparison on differing files

Create a cron job to monitor directory changes:

#!/bin/bash
DIFF_LOG="/var/log/dir_diff_$(date +%Y%m%d).log"
diff -rq /important/dir /backup/dir > $DIFF_LOG
if [ -s $DIFF_LOG ]; then
    mail -s "Directory changes detected" admin@example.com < $DIFF_LOG
fi

When comparing directories in Linux, the traditional diff command remains the most straightforward solution. Here's the basic syntax:

diff -rq /path/to/first/directory /path/to/second/directory

The -r flag enables recursive comparison, while -q only reports when files differ rather than showing content differences. Example output:

Files dir1/config.php and dir2/config.php differ
Only in dir1: temp.log
Only in dir2: cache/

For developers who need to see exactly what changed in each file:

diff -ruN dir1/ dir2/

The -u flag produces unified diff format, and -N treats absent files as empty. This is particularly useful for patching operations.

When dealing with large directories or needing more sophisticated comparison:


# Using Meld (GUI)
meld dir1 dir2

# Using rsync for fast comparison
rsync -n -av --delete dir1/ dir2/ | grep '^deleting'

The rsync approach is particularly efficient for checking if directories are in sync, while Meld provides a visual interface many developers prefer.

For deployment scenarios, you might want to generate patch files:

diff -ruN original_dir/ modified_dir/ > changes.patch

This creates a standard patch file that can be applied elsewhere using the patch command.

When your directories contain symbolic links, add the -L option:

diff -rL dir1 dir2

This tells diff to follow symbolic links while comparing.

For very large directories, consider these optimizations:


# Compare only by file names and attributes
diff -rq --no-dereference dir1 dir2

# Compare only specific file types
find dir1 -name "*.py" -exec diff -q {} dir2/{} \;