While working with Linux, users encounter low disk space issues due to duplicate files. The diff command compares all files to find out which files differ by content in two directory trees on Linux. It does that by reading all the lines. This is, so far, the easiest approach for comparing the files in the directories. Now, the question is how to find out which files differ by content in the given two directory trees.
This guide sheds light on how users can find out which files differ by content in given two directories on Linux.
- View Files in the Directories
- Method 1: Using the diff Command.
- Method 2: Using the Meld Tool.
How to View Files in the Directories on Linux?
The “ls” is a command line utility that allows users to view the files and directories at the specified path. It has various options waiting for you to explore in this guide. Let’s view the contents of the directories called “folder1” and “folder2” using the “ls” command:
$ ls folder1
The above image displays the contents of folder1. To view the contents of folder2, use this command:
$ ls folder2
The above image displays the contents of folder2. Let’s find out if they differ by content.
Method 1: Using the diff Command.
To find out the files that differ by content in two directory trees, the diff command can be used in this format:
$ diff -rq directory1/ directory2/
In the above command:
- -r flag of the diff command is used to compare directories recursively.
- -q specifies to only report if files differ.
Here, the -r option tells diff to compare directories recursively, and the -q option tells it only to report if files differ (and not show the actual differences). The directory1/ and directory2/ arguments are the two directories you want to compare.
Let’s execute the above command on actual directories in which a few files are the same, and some are not (can be seen in the above section):
$ diff -rq folder1/ folder2/
In the above output, it is showing the files only found in folder2 and does not show the common files among them.
If you want to exclude (by extension) and view the different files within the different directories, use the “-x” flag like this:
$ diff -rq -x '*.txt' folder1/ folder2/ $ diff -rq -x '*.sh' folder1/ folder2/
In the above image, the flag “-x” excludes the files that are not to be checked.
Method 2: Using the meld Tool
Meld is a GUI-based tool used to compare the contents of the directories in Linux. It is not pre-installed like the diff utility. To install Meld on Linux, use these commands:
$ sudo apt install meld #Ubuntu/Debian $ sudo dnf install meld #Fedora $ pacman -S meld #Arch Linux
The above image shows the installation of the meld tool on Ubuntu 22.04 LTS.
Let’s take it for a ride and view the content difference in two directories (folder1 and folder2):
$ meld folder1/folder2
The files which are not present in folder1 but are in folder2 are displayed in green. This tool has more options with different filters to choose from and to be explored by you.
To find out which files differ by content in the given two directory trees, the diff command is used, and if you want a GUI-based tool, use the meld tool. Both are available on all Linux distros, but using the diff command is recommended due to the wide variety of available options.
This guide answered how to find out which files differ by content in given two directory trees on Linux.