compare folders in linux

How to Compare Two Directories in Linux

Sometimes you may need to compare two directories in Linux. Linux provides many tools for comparing files & directories. The most common and popular tool for this purpose is diff. In this article, we will look at how to compare two directories in Linux using diff tool. It is easy to use and is pre-installed in every Linux distribution. It can also be used to compare two files line by line.


How to Compare Two Directories in Linux

By default, the syntax of diff command to compare two files is

$ diff [options] file1 file2

Now if you want to compare two directories instead then you have to use -q option and use directory path instead of file paths.

$ diff -q /path/to/directory1 /path/to/directory2

Here is an example.

$ sudo diff -q /home/ubuntu/directory1 /home/ubuntu/directory2

The output will be shown as below.

Only in directory2 : data.txt
Only in directory2 : data10.txt
Only in directory1 : data2.txt
Only in directory1 : data3.txt

The above command will only compare the files in directory, but will not compare the contents of their subdirectories. It lists files that are present only in either of the directories. If you want to compare the subdirectories also, recursively, then you need to include -r option in your diff command.

$ sudo diff -qr /home/ubuntu/directory1 /home/ubuntu/directory2

In this article, we have learnt how to compare two folders in Linux using diff command. If you want to learn how to compare two files using diff command, you can refer to our detailed article here.

Also read:

How to Find Files Larger than 1GB
How to Compare Two Files in Linux
How to Use Strace Command in Linux
Shell Script to Check Disk Space & Send Email Alerts
Shell Script to Check Memory & Send Email Alerts

2 thoughts on “How to Compare Two Directories in Linux

  1. –Should it say “files” instead of “directories”?

    How to Compare Two Directories in Linux
    Here is how to compare two “directories” in Linux. By default, the syntax of diff command to compare two files is…
    $ diff [options] file1 file2

Leave a Reply

Your email address will not be published. Required fields are marked *