Comparing directories in Linux might seem like a light and dismissive task for a user just starting to explore the milestones associated with using Linux as a primary operating system.
Directory comparison is everything for matured users who have thoroughly explored the Linux file management spectrum. For instance, you might have two directories that are very similar in terms of the content stored in them.
In this case, you might want to get rid of one of these directories but you are unsure if they are perfect duplicates. Such a scenario calls for the Linux directory comparison expertise.
Also, note that two directories having the same directory structure like file names do not make them identical since factors like file size also count.
This article will walk us through the correct approach to comparing two directories under a Linux operating system environment.
Problem Statement
Let us create two directories with slightly similar directory structures which we will be referencing throughout this article. We will name them dir1 and dir2 respectively.
These directories will contain some files and other sub-directories to make this article more interesting. Let us use the Linux tree command to view the final directory structures of these two directories we have created.
$ tree dir1 $ tree dir2
We can now start looking at an effective way of comparing these two directories in a Linux operating system environment.
Comparing Directories Using the diff Command
The primary role diff command is to make a line-by-line comparison between two files and its usage syntax is as follows:
$ diff [OPTION]... FILES
The diff command comes pre-installed on all major Linux operating system distributions hence no need to consult your package manager about its existence.
As specified in the above command syntax, the diff command is effective when comparing files in Linux. Therefore, in order to use it to compare two directories, we will modify the above syntax to resemble the following:
$ diff -q /path/to/directory1 /path/to/directory2
Comparing Files at Root Directory Level
To implement the above command in favor of the two directories we just created, the final implementation of our diff command will look like the following:
$ diff -q dir1 dir2
The command option -q
reports on differing files within the directories.
Please note that we are implementing the diff command at the directory level hence no need to key in the absolute paths of dir1 and dir2.
As per the above output, the diff command thoroughly analyzes the differences and commonalities in the two directories.
Comparing Files on All Directory Levels
The command output above does not analyze the contents of the subdirectories we created. For it to dig into these subdirectories, we need to include the command option -r
for the diff command to be recursive.
$ diff -qr dir1 dir2
Modifying the diff command has provided a full comparative view of files and files within subdirectories of the directories we earlier created.
If you want the diff command to also report on identical files on all directory levels, our modified diff command implementation will look like the following:
$ diff -qrs dir1 dir2
We have seen how effective the diff command is in comparing two directories on a Linux OS environment. I hope you enjoyed the article and feel free to leave a comment or feedback.