Skip to main content

Command Palette

Search for a command to run...

How to Check Difference Between Two Directories in Linux

Updated
5 min read

When working with Linux, you often need to compare two directories to see what files or content differ between them. Whether you’re syncing backups, verifying code changes, or managing system files, knowing how to check differences between directories is essential. In this article, I’ll guide you through simple and practical methods to compare directories in Linux.

You don’t need to be a Linux expert to follow along. I’ll explain commands and tools step-by-step, so you can confidently find differences between directories. By the end, you’ll know how to spot missing files, changed content, and even sync directories efficiently.

Why Compare Directories in Linux?

Comparing directories helps you keep your files organized and consistent. Here are some common reasons you might want to check differences:

  • Backup verification: Ensure your backup folder matches the original.
  • Code review: Spot changes between project versions.
  • System maintenance: Detect unauthorized or accidental file changes.
  • File synchronization: Prepare for syncing files between locations.

Linux offers several commands and tools to make this task straightforward. Let’s explore the most popular and effective methods.

Using the diff Command to Compare Directories

The diff command is a classic Linux tool that compares files and directories. It shows you what’s different between two inputs.

How to Use diff for Directories

To compare two directories, use:

diff -rq directory1/ directory2/
  • -r means recursive, so it checks all subdirectories.
  • -q means brief output, showing only if files differ or are missing.

What You’ll See

  • Files only in one directory will be listed.
  • Files that exist in both but differ will be noted.
  • Identical files won’t be shown.

Example Output

Only in directory1/: fileA.txt
Files directory1/fileB.txt and directory2/fileB.txt differ

This tells you fileA.txt is missing in directory2, and fileB.txt has different content.

More Detailed Comparison

If you want to see the exact line-by-line differences for files, omit the -q:

diff -r directory1/ directory2/

This will output the specific changes inside differing files.

Limitations of diff

  • It compares text files well but may not handle binary files clearly.
  • Output can be verbose for large directories.

Using rsync to Find Differences Without Syncing

rsync is known for syncing files, but it can also help identify differences without copying anything.

How to Use rsync for Comparison

Run this command:

rsync -avun --delete directory1/ directory2/
  • -a means archive mode (preserves permissions, timestamps).
  • -v is verbose.
  • -u skips files that are newer on the receiver.
  • -n is dry-run mode (no actual changes).
  • --delete shows files that would be deleted to sync.

What You’ll Get

rsync will list files that differ, are missing, or would be deleted to make the directories identical.

Why Use rsync?

  • It’s fast and efficient.
  • Works well with large directories.
  • Shows differences in file size, timestamps, and presence.

Using comm to Compare Directory Listings

If you want a simple way to compare file names only, you can use comm with sorted directory listings.

Steps to Use comm

  1. List files in both directories recursively:
find directory1/ -type f | sort > dir1_files.txt
find directory2/ -type f | sort > dir2_files.txt
  1. Use comm to compare:
comm dir1_files.txt dir2_files.txt

What comm Shows

  • Column 1: Files only in dir1_files.txt.
  • Column 2: Files only in dir2_files.txt.
  • Column 3: Files common to both.

Benefits

  • Quick overview of file presence.
  • No content comparison, just names.
  • Useful for large directories where content check is unnecessary.

Graphical Tools for Directory Comparison

If you prefer a visual interface, Linux offers GUI tools to compare directories.

  • Meld: A user-friendly diff and merge tool.
  • KDiff3: Shows differences and merges files.
  • Diffuse: Lightweight graphical diff viewer.

How to Use Meld

  1. Install Meld:
sudo apt install meld
  1. Launch Meld and select the two directories to compare.

  2. Meld will display files side-by-side, highlighting differences.

Advantages of GUI Tools

  • Easier to understand for beginners.
  • Visual highlighting of differences.
  • Supports merging changes interactively.

Comparing Directory Contents with Checksums

Sometimes, you want to verify if files are identical by content, regardless of timestamps or names.

Using md5sum or sha256sum

  1. Generate checksums for all files in each directory:
find directory1/ -type f -exec md5sum {} + | sort > dir1_checksums.txt
find directory2/ -type f -exec md5sum {} + | sort > dir2_checksums.txt
  1. Compare the checksum files:
diff dir1_checksums.txt dir2_checksums.txt

Why Use Checksums?

  • Detects content changes even if file names are the same.
  • Useful for verifying file integrity.
  • Works well for backups and sensitive data.

Tips for Efficient Directory Comparison

  • Use diff -rq for quick checks.
  • Use rsync -n for large directories with many files.
  • Use checksums when content integrity matters.
  • Use GUI tools for easier visualization.
  • Combine methods for thorough comparison.

Conclusion

Checking differences between two directories in Linux is a common task that can be done in many ways. The diff command is the simplest and most direct tool for quick comparisons. For larger or more complex directories, rsync offers a fast and efficient way to spot differences without syncing. If you want to compare just file names, comm with sorted lists works well. For visual learners, GUI tools like Meld make the process easier.

Additionally, using checksums helps verify file content integrity beyond just names and timestamps. By choosing the right method for your needs, you can confidently manage your files and keep your Linux directories in sync and organized.

FAQs

How do I compare two directories including subdirectories?

Use diff -r directory1/ directory2/ to recursively compare all files and subdirectories.

Can I compare binary files in directories?

diff can compare binary files but only reports if they differ, not the content. Use checksums for detailed verification.

What is the fastest way to check directory differences?

rsync -avun --delete directory1/ directory2/ is fast and efficient for large directories.

How do I ignore file timestamps when comparing?

Use checksum methods like md5sum to compare file content regardless of timestamps.

Are there free graphical tools to compare directories on Linux?

Yes, tools like Meld, KDiff3, and Diffuse are free and easy to use for visual directory comparison.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts

How to Check Difference Between Two Directories in Linux