Linux

Linux : how do I compare 2 files or 2 folders with the diff command?

I. Introduction

Under Linux, there's a very simple and effective command for comparing two files: diff. In this tutorial, we'll learn how to use it to identify the difference between two files, which can be useful, for example, when comparing 2 versions of a script or configuration file.

If you're ready to discover some examples of how to use the "diff" command under Linux, just read on. For my part, I'm going to use a machine running Ubuntu, but this command works with other distributions.

II. diff command syntax

Let's start with the syntax of the diff command:

diff file1 file2 [options]

This command will return a result that should be interpreted as follows:

  • Lines beginning with the symbol "<" symbol refer to the contents of the first
  • Lines beginning with the symbol ">" symbol refer to the contents of the second file.
  • The letters indicate how the first file should be modified to match the second file. The output can display three types of status:
    • a: add
    • c: edit
    • d: delete

If this seems a little vague to you at the moment, don't panic, you'll understand better when you carry out an initial comparison test.

III. Using the diff command

First of all, you should know that the diff command can compare any text file, be it configuration files, Bash scripts or simple text files. As long as the contents are accessible in plain text mode, the tool will be able to perform the comparison.

Note : you cannot compare 3 files (or more) with the diff command.

A. Getting started with diff

Start by creating a file named "file1.txt" with the following contents:

##########
IT-Connect
##########
www.it-connect.fr

Then, create a file named "file2.txt" with the following contents:

##########
IT-Connect
##########
> Démo Tuto Linux

The difference between these two files is in the last line.

To compare these two files, use this command:

diff file1.txt file2.txt

The following result is returned:

4c4
< www.it-connect.fr
---
> > Démo Tuto Linux

Let's decrypt it line by line:

  • 4c4 :

This means that there is a change ("c" for "change") on line 4 in both files. Line 4 in file1.txt has been changed and is different from line 4 in file2.txt.

  • < www.it-connect.fr :

The symbol "<" indicates content present in file1.txt, but not present in file2.txt. So line 4 in file1.txt contains "www.it-connect.fr".

  • --- :

This line separates the differences between the two files.

  • > > Linux demo tutorial:

The symbol ">" indicates corresponding content in file2.txt, but not present in file1.txt. So line 4 in file2.txt contains "Linux tutorial demo".

In summary, we obtain the following:

Example of using the diff command on Linux

B. What happens with identical files?

When we perform a comparison with diff and the two files are identical, there's no return to the console ! Well, let's just say that this is the default behavior of the command. If we add the "-s" option, there's a message clearly indicating that these 2 files are identical.

diff file1.txt file2.txt -s

We obtain the following result:

The files file1.txt and file2.txt are identical.
Compare 2 identical files with diff on Linux

C. The diff command and case

By default, the diff command is case-sensitive, i.e. it will consider two files to be different if they are case-sensitive. To disregard case, use the "-i" option. Here's an example:

diff file1.txt file2.txt -i
# voire même :
diff file1.txt file2.txt -is

Which gives:

Command diff - Do not case-sensitive

D. Disregard line breaks

By default, the diff command is particularly sensitive, since it takes case and line breaks (empty lines) into account. To prevent empty line breaks from being considered as differences, we can use the "-B" option, as follows:

diff file1.txt file2.txt -B

The image below shows the difference in results, depending on the use of the "-B" option. You can, of course, combine these different options.

Command diff - Ignore line breaks

E. Change display mode

The diff command supports several display modes that will have an impact on the formatting of the result in the console. The "-c" option allows you to specify differences more precisely by placing a symbol in front of the lines concerned.

Simply specify the option as follows:

diff file1.txt file2.txt -c

With file1.txt having this content :

##########
IT-Connect
##########
Rendez-vous sur IT-Connect :
www.it-connect.fr

And, the file2.txt with this content :

# Démo
##########
IT-Connect
##########
# Tuto
# Linux

We obtain this result:

*** file1.txt	2025-01-16 16:18:32.817171261 +0100
--- file2.txt	2025-01-16 16:19:44.665177050 +0100
***************
*** 1,5 ****
  ##########
  IT-Connect
  ##########
! Rendez-vous sur IT-Connect :
! www.it-connect.fr
--- 1,6 ----
+ # Démo
  ##########
  IT-Connect
  ##########
! # Tuto
! # Linux

Here's what you need to know:

  • The first two lines indicate thetimestamp of the two files being compared.
  • The line "*** 1.5 ****" indicates the range of lines analyzed (from line 1 to line 5).

Then there is a symbol in front of the lines that represent a difference between the two files:

  • Symbol"-": the line must be deleted from the first file.
  • +" symbol: the line must be added to the first file.
  • Symbol "!": the line must be replaced by the corresponding line in the second file.

For example, this means that the line"# Demo" must be added to the first file, for there to be an exact match. This is explained by the presence of the "+" character: "+ # Demo".

Here's the complete example in pictures:

For a more compact output that follows the same logic, you can use the "-u" option:

diff file1.txt file2.txt -u

F. Compare 2 files with diff

In addition to comparing 2 files with diff, you can also compare the contents of 2 folders. So you can identify files and folders present in directory 1, but not in directory 2, and vice versa. To do this, simply use the "-q" option.

diff doc1/ doc2/ -q
Seulement dans doc1/: file2.txt
Seulement dans doc1/: file3.txt
Seulement dans doc2/: file5.txt

Here's the complete example:

Comparing 2 folders with diff under Linux

IV. Conclusion

After reading this tutorial, you'll know how to use the diff command under Linux, whether to compare the contents of 2 files or 2 directories! If you'd like to know even more, take a look at the help for this command:

diff --help
author avatar
Florian Burnel Co-founder of IT-Connect
Systems and network engineer, co-founder of IT-Connect and Microsoft MVP "Cloud and Datacenter Management". I'd like to share my experience and discoveries through my articles. I'm a generalist with a particular interest in Microsoft solutions and scripting. Enjoy your reading.

Leave a Reply

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.