To compare the contents of two directories from the command line, you use the diff command on Linux/macOS with the -r (recursive) flag, or robocopy with the /L and /E flags on Windows. These commands list which files differ, which are missing from either side, and show the actual differences inside text files.
Why Compare Directories at All?
Comparing directories is a routine task for anyone who works with file systems. You might need to verify that a backup copied everything correctly, check that two project folders are in sync after a merge, confirm a deployment matches the staging environment, or simply find out which files changed between two versions of a folder. A manual, file-by-file check is slow and error-prone, especially when directories contain hundreds of files. Command-line tools give you a fast, scriptable, and precise answer.
The Core Tools: diff, rsync, and robocopy
Three commands dominate directory comparison:
diff(Linux, macOS, and Windows with Git Bash or WSL) – the classic text comparison tool. With-rit walks through both directory trees and reports every difference.rsync(Linux, macOS, and Windows via WSL or Cygwin) – primarily a sync tool, but running it with--dry-runand--itemize-changesshows exactly what would change, which is effectively a comparison.robocopy(Windows native) – the built-in file copy tool. The/Lflag makes it list what would be copied without copying, and/Eincludes subdirectories.
Each has its strengths. diff is best for seeing the actual content differences in text files. rsync is best for large trees where you want a summary and the ability to sync. robocopy is the Windows-native option with a clean summary table.
How to Use diff for Directory Comparison
The most common command is:
diff -r dir1 dir2
This prints lines that differ in files with the same relative path. For example, if dir1/file.txt contains “apple” and dir2/file.txt contains “orange”, you will see:
< apple
---
> orange
If a file exists only in one directory, diff prints “Only in dir1: filename”. To ignore whitespace differences, add -w or -b. To get a brief summary instead of full output, use -q – it prints only “Files dir1/file and dir2/file differ”.
For a quick check that two directories are identical, run:
diff -rq dir1 dir2
If the command produces no output, the directories are identical in content and file names.
Using rsync to Compare and Sync
To compare without modifying anything, use:
rsync -avn --itemize-changes dir1/ dir2/
The -n (dry-run) flag prevents any actual copying. The -a flag preserves permissions and timestamps, and -v gives verbose output. The --itemize-changes flag shows a code for each file: f for file, d for directory, and letters like T (timestamp), S (size), or . (no change). A file with no changes is not listed, so if the command outputs nothing, the directories are identical.
To also compare file contents (not just size and timestamp), add -c (checksum). This is slower but catches files that have the same size but different content.
Windows: robocopy for Directory Comparison
On Windows, robocopy is the most powerful built-in tool. The command:
robocopy dir1 dir2 /L /E /NJH /NJS
lists every file that would be copied from dir1 to dir2, including subdirectories (/E). The /L flag makes it a “list only” operation – nothing is actually copied. The /NJH and /NJS flags suppress the job header and summary, making the output cleaner. If the command prints nothing except the header lines, the directories are identical.
To see which files differ in content, add /L and /E plus /B (backup mode) – but a simpler approach is to use fc.exe (file compare) in a loop, or use diff if you have Git installed.
Choosing the Right Tool for Your Situation
The choice depends on your operating system and what you need to know:
- Need to see the actual content differences in text files? Use
diff -r. - Need to verify a backup or sync? Use
rsync -avnc(Linux/macOS) orrobocopywith/L(Windows). - Need to compare huge trees quickly? Use
rsync -avn(size/timestamp) ordiff -rq. - Need to compare binary files?
diffwill tell you they differ, but not how. Usecmporsha256sumto verify they are identical.
For a one-off check, diff -rq is the fastest. For a script that needs to parse output, rsync --itemize-changes is more structured. For Windows users who want a native tool, robocopy is the answer.
Common Pitfalls and How to Avoid Them
- Trailing slashes matter. In
rsync,dir1/means “the contents of dir1”, whiledir1means “the directory itself”. This can cause unexpected results when syncing. - Hidden files.
diffandrobocopyinclude hidden files by default, but some shells may not expand wildcards for hidden files. Usefindorls -Ato check. - Symlinks.
diff -rfollows symlinks by default, which can lead to infinite loops or false differences. Use--no-dereferenceinrsyncordiff -r --no-dereferenceto compare the links themselves. - Timestamps vs. content.
rsyncwithout-ccompares size and modification time, not content. Two files with the same size and timestamp are considered identical, even if their bytes differ. If you need content comparison, always add-c. - Large output. For directories with many differences, the output can be overwhelming. Redirect to a file:
diff -rq dir1 dir2 > differences.txt.
Frequently Asked Questions
How do I compare directories on Windows without installing anything?
Use robocopy with the /L and /E flags. It is built into all modern Windows versions. For example, robocopy C:\folder1 C:\folder2 /L /E lists every file that would be copied, which effectively shows the differences.
What does “Only in” mean in diff output?
It means a file or subdirectory exists in one directory but not the other. For example, “Only in dir1: notes.txt” means notes.txt is present in dir1 but missing in dir2.
Can I compare directories by file size only?
Yes. On Linux/macOS, use find with -printf to list sizes, or use rsync -avn --itemize-changes which shows size differences. On Windows, robocopy shows files that differ in size when you add /L and /E.
Why does diff show a difference when the files look the same?
Often it is due to line endings (Windows CRLF vs. Unix LF) or trailing spaces. Use diff -w to ignore whitespace, or diff -b to ignore changes in the amount of whitespace. For line endings, convert files first with dos2unix or use diff --strip-trailing-cr.
Is there a way to compare directories and then sync them?
Yes. rsync is the standard tool. After a dry-run (-n), remove the -n flag to actually sync. On Windows, robocopy without /L will copy the differences. Always test with /L first to avoid accidental data loss.
Can I compare more than two directories at once?
Not directly with diff or rsync. You would need to compare them pairwise, or use a tool like fdupes for finding duplicate files across multiple directories. For a simple three-way comparison, run diff -r dir1 dir2 and then diff -r dir2 dir3.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Aquarium Gallons Calculator: Volume in US or Imperial
- Waves Later Codes? Meaning & Context
- Recover exFAT Partition: Full Guide
- DNS Over HTTPS: What It Is & How It Works
- Apex Legends Zoom In/Out: How to Change FOV
- Fix AI Agent Cascading Failures in Reliability Tests