How to Find Difference Between Two Directories Using Diff and Meld Tools

In an earlier article, we reviewed 9 best file comparison and difference (Diff) tools for Linux and in this article, we will describe how to find the difference between two directories in Linux.

Normally, to compare two files in Linux, we use the diff – a simple and original Unix command-line tool that shows you the difference between two computer files; compares files line by line and it is easy to use, comes with pre-installed on most if not all Linux distributions.

The question is how do we get the difference between two directories in Linux? Here, we want to know what files/subdirectories are common in the two directories, those that are present in one directory but not in the other.

The conventional syntax for running diff is as follows:

$ diff [OPTION]… FILES
$ diff options dir1 dir2 

By default, its output is ordered alphabetically by file/subdirectory name as shown in the screenshot below. In this command, the -q switch tells diff to report only when files differ.

$ diff -q directory-1/ directory-2/
Difference Between Two Directories
Difference Between Two Directories

Again diff doesn’t go into the subdirectories, but we can use the -r switch to read the subdirectories as well like this.

$ diff -qr directory-1/ directory-2/ 

Using Meld Visual Diff and Merge Tool

There is a cool graphical option called meld (a visual diff and merge tool for the GNOME Desktop) for those who enjoy using the mouse, you can install it as follows.

$ sudo apt install meld         [On Debian, Ubuntu and Mint]
$ sudo yum install meld         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
$ sudo emerge -a sys-apps/meld  [On Gentoo Linux]
$ sudo apk add meld             [On Alpine Linux]
$ sudo pacman -S meld           [On Arch Linux]
$ sudo zypper install meld      [On OpenSUSE]   
$ sudo brew install meld        [On macOS]  

Once you have installed it, search for “meld” in the Ubuntu Dash or Linux Mint Menu, in Activities Overview on Fedora or CentOS desktop and launch it.

You will see the Meld interface below, where you can choose file or directory comparison as well as a version control view. Click on directory comparison and move to the next interface.

Meld Comparison Tool
Meld Comparison Tool

Select the directories you want to compare, note that you can add a third directory by checking the option “3-way Comparison”.

Select Comparison Directories
Select Comparison Directories

Once you selected the directories, click on “Compare”.

Listing Difference Between Directories
Listing Difference Between Directories

In this article, we described how to find the difference between two directories in Linux. If you know any other command line or gui way don’t forget to share your thoughts to this article via the comment section below.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

21 Comments

Leave a Reply
  1. This is not correct; the command `diff -q` is taking excessively long. This is not just comparing two directories; it is comparing the content of all files within the directories.

    Reply
    • @Bob,

      You can limit comparison to file names and sizes by combining find and diff:

      find dir1 -type f -exec stat --format='%s %n' {} + | sort > dir1_files.txt
      find dir2 -type f -exec stat --format='%s %n' {} + | sort > dir2_files.txt
      diff -q dir1_files.txt dir2_files.txt
      
      Reply
  2. Helpful!

    You can also perform the comparison through rsync using the --dry-run parameter so that it only shows the differences, but without doing anything.

    Reply
    • @Luis,

      Yes, that’s correct. The rsync --dry-run option allows you to run a trial run of rsync without actually transferring any files. This is a great way to compare two directories and see which files are different.

      The syntax for the rsync --dry-run command is as follows:

      # rsync --dry-run [options] source_directory destination_directory
      

      For example, to compare the contents of the /home/user/source directory to the /home/user/destination directory, you would use the following command:

      # rsync --dry-run /home/user/source /home/user/destination
      
      Reply
      • rsync has an extra plus: the directories to be compared do not necessarily have to be on the same system. Comparison can be done between different computers.

        Reply
  3. I got great value from this post as I currently find myself archiving files onto a backup drive. I installed meld using homebrew on the macOS command line and it worked!

    $ brew install meld 
    

    That’s all I needed to do. Thank you so much for the great tutorial. Much appreciated.

    Reply
    • @Edwin,

      I’m glad to hear that you found value from this article, and it’s great to know that you were able to successfully install Meld using Homebrew on macOS!

      Meld is a powerful file comparison and merging tool that can be handy for various tasks, including archiving and managing files.

      Reply
  4. There is a great tool called Beyond Compare here https://www.scootersoftware.com/.

    It is a GUI tool with versions for Linux, Mac & Windows. It can also connect to a remote server using sftp.

    Reply
    • @Brian,

      Absolutely! Beyond Compare is indeed an exceptional tool for file and folder comparison and synchronization, but it is not free. While it offers a 30-day trial period during which users can evaluate its features.

      Reply
    • @Kepoks,

      Thank you for bringing up KDiff3, which is indeed a great alternative to Meld and an excellent graphical file and directory comparison tool.

      Reply
    • @Kepoks,

      You are correct, Kompare is another excellent file comparison tool, but it is good for code comparison and small-scale file merging tasks.

      Reply
  5. Can I use the diff command with the remote host, assuming the second folder is on the B server then how to use the diff command?

    Reply
    • @Aashish

      You can find difference between two files, one local and another remote, like this:

      $ diff /path/to/local/file  < ( ssh user@remotehost 'cat /path/to/remote/file')
      
      Reply
      • @Diego,

        Meld and rsync are two distinct tools that serve different purposes, but they can complement each other in certain scenarios, particularly when it comes to file management, comparison, and synchronization tasks.

        Meld is a visual file and directory comparison tool and Rsync, on the other hand, is a powerful and versatile file synchronization and transfer tool.

        Reply
    • @Aashish,

      Yes, you can use the diff command to compare files or directories on a remote host (B server) with the local files or directories on your current machine (A server).

      To achieve this, you can use the ssh command along with diff to run the comparison remotely.

      Assuming you want to compare the contents of two directories: /path/to/dir1 on A server and /path/to/dir2 on B server, you can use the following command:

      $ ssh user@B_server "diff -r /path/to/dir2 /path/to/dir1"
      
      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.