1. Introduction

Comparing two directories is quite a common task. There are many reasons that make us interested in finding the difference. For example, we usually want to figure out what has changed compared to a previous state when something goes wrong.

In this tutorial, we’ll learn how to compare two directories on Linux. We can compare directories in several ways, depending on what means are available on the system.

2. Setup

For the purpose of this tutorial, we’ll prepare sample directories inside the /tmp folder:

Dir1                             Dir2    
├── client.log                   ├── client.log
├── file01                       ├── file01
├── file02                       ├── file02
├── file03                       ├── file03
│                                ├── file04
├── server.log                   ├── server.log
├── subdir1                      ├── subdir1
│   ├── file11                   │   ├── file11
│   └── file12                   │   └── file12
├── subdir2                      └── subdir2
│   ├── file21                       ├── file21
│   └── file22                       ├── file22
└── subdir3                          └── file23
    ├── file31
    └── file32

Sample directories have any kind of difference that supposed to be detected by comparison – identical items, different items, and items only present in one of the directories but not in the other. This way we’ll be able to follow the output of the comparison tools below and interpret the results easier.

3. Command Line Utility

First, we nearly always can use one of the oldest Linux utilities, diff to know how the directories differ. Usually aimed to compare file content, the diff utility is capable to compare directories as well. It has a lot of options, and two of them are most relevant for our case. These are: –brief to hide the details about the content of different files and –recursive to show the difference in all subdirectories:

diff --brief --recursive Dir1 Dir2
Files Dir1/client.log and Dir2/client.log differ
Files Dir1/file02 and Dir2/file02 differ
Files Dir1/file03 and Dir2/file03 differ
Only in Dir2: file04
Files Dir1/subdir1/file12 and Dir2/subdir1/file12 differ
Files Dir1/subdir2/file22 and Dir2/subdir2/file22 differ
Only in Dir2/subdir2: file23
Only in Dir1: subdir3

Another useful option of diff utility is –exclude, it allows filtering out items that we are not interested in within the scope of comparison. To exclude all the *.log  files from the example above, we add –exclude=’*.log’ to the command:

diff --brief --recursive Dir1 Dir2 --exclude '*.log'
Files Dir1/file02 and Dir2/file02 differ
Files Dir1/file03 and Dir2/file03 differ
Only in Dir2: file04
Files Dir1/subdir1/file12 and Dir2/subdir1/file12 differ
Files Dir1/subdir2/file22 and Dir2/subdir2/file22 differ
Only in Dir2/subdir2: file23
Only in Dir1: subdir3

One thing we should keep in mind here is that diff utility compares files by content, and that may result in a significant delay at large volumes of compared data.

4. Terminal File Managers

Directory comparison feature is also available in several file managers. In Midnight Commander we select Command/Compare Directories menu item or alternatively, Ctrl-x d shortcut to make a comparison of the directories showed in the manager’s file panels. Different items will be shown as selected:

MCdircomparison

The limitation is it doesn’t make it recursively, but we can choose between  Quick, Size only, and Thorough options based on timestamps, size, and content respectively.

Directory comparison in vifm file manager is more advanced and is recursive by default. To see the difference between the directories in the left and right panels, we use the internal :compare command of the file manager. It results in a view where different entries are highlighted and missed ones shown as lines of dots:

VIFMdircomparison

5. GUI Approach

When a  graphical desktop is available on the system, we can use even more advanced utilities. Meld is a feature-rich comparison tool. The result of the comparison is very visual – we can easily identify items that differ as well as missed ones from either side of the comparison:

MELDcomparison

Moreover, at this point, it is possible to follow the difference in the file content of each file by double-clicking on a specific file name. A filtering option is also available.

Last but not least, we can select between comparison by content or size and timestamps only, which may significantly improve the speed of comparison.

6. Conclusion

In this short tutorial, we learned several ways of how directories can be compared on Linux.

First, we showed the CLI approach applicable to all Linux systems. Then, we looked at more visual ways with advanced software. We also addressed factors that can influence the comparison performance.

Comments are closed on this article!