Compare two folders in Linux and find differences

In this article, we will learn how to compare two folders in Linux and find differences.

Table Of Contents

Using the diff command

The diff is one of the most widely used Linux command to compare contents of two files. It compares the contents of the files line by line. But sometimes we get a requirement to compare two directories instead of files. In that case also we can use diff command to find out differences in two folders. Lets looks at various ways in which we can compare contents of two folders.

Syntax:

diff [options] dir1 dir2

Options:

Advertisements
-r --recursive              : Recursively compare any subdirectories found.
-q --brief                  : Output only whether files differ.
-y --side-by-side           : Output in two columns.
-b --ignore-space-change    : Ignore changes in the amount of white space.
-w --ignore-all-space       : Ignore all white space.
-B --ignore-blank-lines     : Ignore changes whose lines are all blank.
-i--ignore-blank-lines      : Ignore changes whose lines are all blank.
--ignore-case               : Ignore case differences in file contents.
--ignore-file-name-case     : Ignore case when comparing file names.
--no-ignore-file-name-case  : Consider case when comparing file names.

Let’s see some examples,

Example 1 :

diff -qr ./test ./test1

It will compare recursively in both the directories (test and test1) and display all the file names that are different. In the above command as we have given -q it will display briefly. That’s why it will display only filenames.

Output

Files ./test/policy.js and ./test1/policy.js differ

Example 2 :

diff -r ./test ./test1

Compare recursively in both the directories (test and test1) and display the comparison.

Output:

1,3c1,3
< Mango
< apple
< banana
\ No newline at end of file
---
>  Mango
> pineapple
> carrot
\ No newline at end of file

Lets take a look at what is output means. The first line of diff output contains the following information

  • line numbers related to first file
  • a letter (a for add, c for change and d for delete)
  • line numbers related to the second file

In our output “1,3c1,3” means like 1-3 is changed in first file when compared to the second file. Then it also displays the lines that got changed in each file.

  • Lines preceded with a < are the lines from the first file
  • Lines preceded with a > are the lines from the second file
  • the dashes (“—“) will separate file1 and file2

Example 3 :

diff -rw ./routes/test.js ./services/test.js

-w ignore all the spaces and compare recursively and display the comparison

Output :

2,3c2,3
< apple
< banana
\ No newline at end of file
---
> pineapple
> carrot
\ No newline at end of file

Example 4 :

diff -c ./routes/test.js ./services/test.js

The -c is the context mode of the “diff” command allows you to view additional information related to the specified files.

Output:

*** ./routes/test.js    Sun Aug 28 22:31:10 2022
--- ./services/test.js  Sun Aug 28 22:42:32 2022
***************
*** 1,3 ****
! Mango
! apple
! banana
\ No newline at end of file
--- 1,3 ----
!  Mango
! pineapple
! carrot
\ No newline at end of file

Lets understand the output of this command. In the output, first two lines will show us information related to file1 and file2. It also lists modification date and modification time of each file along with the filename. The first file will be indicated by three asterisks and second file will be indicated by (“—“). The line with fifteen asterisks is a separator.

The next line has three asterisks(“***”) followed by the changed lines from first file and then it will be followed by four asterisks. Following is the meaning of (“!,+,-“) that is displayed on subsequent lines.

  • “!” – indicate that this line is part of a group of one or more lines that needs to change
  • “+” – Indicate that second file has a line that needs to be added in first file
  • “-” – Indicate that first file has a line that needs to be deleted

Example 5 :

diff -u ./routes/test.js ./services/test.js

The -u is the unified mode of the “diff” command is very much similar to the context mode; however, the only difference is that it avoids displaying redundant information

Output:

--- ./routes/test.js    2022-08-28 22:31:10.000000000 +0530
+++ ./services/test.js  2022-08-28 22:42:32.000000000 +0530
@@ -1,3 +1,3 @@
-Mango
-apple
-banana
\ No newline at end of file
+ Mango
+pineapple
+carrot
\ No newline at end of file

Using the meld tool

Till now we explored various options in diff command that will help us to identify the differences between two folders. But if the directory has too many files, then we will be difficult to view the differences. In those cases we can use the meld GUI tool to compare two folders. We can use the following command to install that software in linux operating systems

Installation

Debian/Ubuntu etc.

sudo apt install meld

RHEL/CentOS etc.

sudo dnf install meld

Summary

Hence, we learnt how to compare two folders and find differences in Linux. Thanks.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top