Compare two directories in Linux?


Introduction

It's quite common for people to compare two directories. Many different factors make us want to find out whether there really is a difference between two things. For example, we normally want to figure out what's different from a previous situation when something goes wrong.

We’ll learn how we can use the command line to perform directory comparisons. There are different methods we can use to compare directory listings. We'll also see some of the most commonly used commands and their options.

Setup

We'll create some sample directories inside the /temp directory for this tutorial.

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

Samples must contain at least two kinds of files: identical ones (same file name, size, etc.) and different ones (different file names, sizes, etc.). So we can easily compare the results from different comparison tools.

Command Line Utility

We almost always can use an old Unix utility called diff to see how two files (or directories) are different. Used for comparing files, the diff utility is able to compare directories as well as files. There are lots of options, but two of them are most useful for our case. Here are some examples of commands that help you view the contents of different types of file formats and directories recursively −

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 for comparing two lists is −−exclude, which lets us filter out elements from one list that we're not interested in. To exclude all .log files from the example shown above, we would run the following 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

We should remember that diff utility compares files using their contents, which could cause a significant delay when comparing large amounts of files.

Terminal File Managers

File manager directory comparison features are also available in some file managers. To compare two directories in Midnight Commander, use either the command/compare directory menu option or the ctrl+x d keyboard combination. Different options will be displayed when they're selected −

It doesn't make it recursive, but we can choose from quick, size only, and thorough options based on time stamps, size, and contents respectively.

File comparisons in Vifm are more advanced than directory comparisons. They're recursive by default. If you want to see the differences between the files in the left and right panel, you can simply run the −filemanager internal :comparedir command. It shows a list of entries, highlighting some of them and showing others as lines of dashes.

GUI Approach

If there is a graphical desktop available on the system, then we can use even more powerful tools. Meld is one such example. The results of the comparison are very clear – we can easily see which items differ as well as missing items from both sides of the comparison.

Furthermore, at this point, you can view the differences between the contents of each file by double clicking on a specific file name in Windows Explorer. You can also filter by category −

Finally, we can choose between comparing by content or just timestamps, which might significantly improve the speed of comparisons.

Conclusion

We've seen several different methods for comparing directory contents on Linux.

We first demonstrated the CLI approach for all Linux operating system. Next, we examined some more visual ways with advanced tools. We also discussed some things that can affect the comparison performance.

Updated on: 26-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements