How to Use the Linux diff Command

Try this guide with our instant dedicated server for as low as 40 Euros

linux diff command

Working with files is a huge aspect of working with Linux systems. Whether you’re a developer or an administrator, you need to compare files (often line by line) to find discrepancies, troubleshoot code, and overall guarantee data integrity. You can do so by using Linux diff command.

You can try to compare files manually, but it is a tedious task that is prone to human error. That’s why we recommend using the diff command, an effective tool that makes this process easier. It is part of the GNU Diffutils package and is thus often available in all mainstream Linux distributions. 

In this tutorial, we will explain how to compare files line by line by using the diff command in Linux. In addition to the basic syntax, we will cover critical command options that you can use to leverage the diff command in your scripts and at the command line. 

Table Of Contents

  1. An Overview of the diff Command in Linux Environments
    1. The Syntax of the diff Command
  2. How to Use the diff Command in Linux
    1. The Prerequisites
    2. Step #1: Create the Sample Files
    3. Step #2: Use the diff Command to Compare the Files
  3. Popular Options for the Linux diff Command
  4. Important diff Command Options
  5. Conclusion
  6. FAQs

An Overview of the diff Command in Linux Environments 

diff is a short form for difference

The diff command is a flexible tool often pre-installed on most Linux distributions. You can use this tool to compare two files’ contents and see the differences highlighted for easy comparison. In practical terms, you can use this command to identify additions, deletions, and modifications in the files. 

This command can display the differences between the files by comparing them line by line. However, unlike similar commands like cmp and comm, it indicates which lines in one file need to be changed to make the two files similar.

It’s important to remember that diff is used to modify the first file to correspond with the second one. 

The Syntax of the diff Command

The basic diff syntax is as follows: 

# diff [option] file1 file2

This syntax produces the following output:

When using the diff command, it is crucial to understand how to read this output:

  • Content in the first file is referenced by output beginning with <.
  • The material in the second file is referenced by the output beginning with >.
  • Line numbers that relate to the first file.
  • A special symbol. The first file must be changed in a certain way to match the second file, as indicated by special symbols. These symbols include:
  1. A (add), 
  2. C (modify)
  3. and D (remove)

How to Use the diff Command in Linux

Now that you know the basic syntax, let’s see the diff command in action. 

The Prerequisites

Before trying out the steps in this demonstration, please make sure you have the following:

  • A system with a mainstream Linux distribution
  • The diff package is available on your system

Step #1: Create the Sample Files 

We will start by creating two sample files to demonstrate the capabilities of the diff command. 

  1. Launch the terminal and create a text file named rsfile1.txt in your preferred text editor. We will run the following command to create this file using Nano: 

# sudo nano rsfile1.txt

  1. Add the following lines to the file:

India

Indonesia 

Bangladesh

Bhutan

China

Japan

GNU nano 6.2

  1. Use Ctrl + X to exit. Press Y to confirm saving the file. 
  2. Next, run the following command to create the second file, rsfile2.txt file. As with the previous file, we will use the following command to create it using Nano: 

# sudo nano rsfile2.txt

  1. Add the following lines to this file:

India

USA

Bangladesh

Japan

China

UAE

Bhutan

  1. Save the file and exit the editor. 

Step #2: Use the diff Command to Compare the Files

  1. After setting up the two sample files, use the diff command to identify the differences and figure out how to remove these differences:

# diff rsfile1.txt rsfile2.txt

diff rsfile1.txt rsfile2.txt

 

The output includes step-by-step directions on how to change the first file’s content to match rsfile2.txt..

Popular Options for the Linux diff Command

When used without any options, the output of the diff command shows the differences between the two files. However, you can modify this default behavior with the following options. 

Option -c 

You can use the -c option to enable the “context format” for the lines that are different in the files. 

Use the following command to see the file differences in context form:

# diff -c file1 file2

Here’s the output of the command:

diff -c file1 file2

 

First of all, notice that the details about the first file start with ***, and the lines that show details about the second file begin with .

The file names and timestamps of both files are shown in the first two lines:

*** rsfile1.txt                            2024-02-19 11:32:56.334540374 +0530

--- rsfile2.txt                            2024-02-19 11:36:54.388882612 +0530

The only purpose of **************** is to act as a separator between the two sections of the output.

The second portion of the output starts with the line range of the files before listing the lines from each file:

*** 1,7 ****

--- 1,7 ----

The content of the files is listed underneath the appropriate head. Each line begins with instructions on changing rsfile1.txt to be identical to rsfile2.txt. 

Here’s a brief explanation of these instructions:

The entries with the minus (-) sign should be removed from the original file.

The entries with the plus (+) sign should be added to the original file.

The entries with the exclamation mark (!) should be replaced with the matching line from the second file.

Important: You should make sure that the lines without any symbol should not be modified. 

Option -u 

You can choose to apply the unified format to show output in a way that removes unnecessary context lines. The syntax of this command is as follows:

# diff -u file1 file2

Let’s look at the output for the diff -u command:

diff -u file1 file2

Note that the details about the first file start with , and lines that show details about the second file begin with +++.

The file names and timestamps of both files are shown in the first two lines:

--- rsfile1.txt       2024-02-19 11:32:56.334540374 +0530

+++ rsfile2.txt    2024-02-19 11:36:54.388882612 +0530

Next, the line @@ -1,7 +1,7 @@ displays the line ranges in the files. 

The contents of the files are shown in the lines below, along with instructions on how to change rsfile1.txt (the first file) to look exactly like rsfile2.txt (the second file). 

Here’s what the symbols in front of each line mean:

The entries with the minus (-) sign should be removed from the first file.

The entries with the plus (+) sign must be added to the first file.

Option -i 

By default, the diff command in Linux environments is case-sensitive. As such, it includes case differences in the output. If you wish the command to ignore these differences, use the -i option. The syntax is as follows:

# diff -i file1 file2

The output indicates file differences and provides modification advice when no other options are selected.

diff -i rsfile1.txt rsfile2.txt

To give you an idea of the impact of the -i option, here is how the comparison looks without it:

diff rsfile1.txt

Option –version 

Use the following command to see which version of diff is active on your system:

# diff --version

diff --version

Usually, the GNU Diffutils package is updated on your system as soon as the maintainers push a new stable version.

Option –help

Finally, you can use the –help option to get help about the diff utility. The syntax would be: 

# diff --help

The output of this syntax would be as follows:

diff --help

Important diff Command Options

We recommend the following options to use with the diff command:

important diff command options

Conclusion

To sum up, learning to use the Linux diff command gives you a strong tool for comparing and identifying file discrepancies. The many uses of diff have been illustrated in this book, ranging from straightforward file comparisons to patch command creation and change detection across directories. Users can manage changes to their files and directories by knowing the output format, options, and integration with other commands. Developers, system administrators, and anybody else working with textual data find the diff command helpful since it provides a simple yet reliable way to recognize and manage content variations.

Experience hosting perfection with RedSwitches. We offer unparalleled dedicated hosting solutions for seamless performance and global reach. Our product lineup has the best dedicated server pricing for instant dedicated servers. These servers are usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What is the primary purpose of the Linux diff command?

The primary purpose of the diff command is to compare and highlight differences between two files or directories in a Unix/Linux environment.

Q. How can I use diff to compare two files and display the differences?

Use the command diff file1 file2 to compare two files and display the lines that differ between them.

Q. Can diff be used to compare binary files?

No, by default, diff is designed to compare text files. To compare binary files, consider tools like cmp or specialized binary comparison utilities.

Q. How can I create a patch command file using diff?

To create a patch file, use the command diff -u oldfile newfile > patchfile. This creates a unified diff format that can be applied later.

Q. Can I use diff to compare the contents of two directories?

Yes, diff can be used to compare the contents of two directories using the command diff -r dir1 dir2.

Q. How do I ignore white spaces while comparing files with diff?

Use the -b or -w option with diff to ignore changes in the amount of white space.

Q. What is the purpose of the –side-by-side option in diff?

The –side-by-side option displays the differences between files in a side-by-side format, making it easier to compare the content visually.

Q. Can I use diff to compare remote files or directories?

Yes, you can compare remote files or directories using tools like SSH to connect to the remote server and then apply the diff command.

Q. How can I apply a patch file created with diff to update a file?

Use the command patch -p1 < patchfile to apply a patch file created with diff and update the corresponding file.

Q. Are there graphical interfaces available for diff on Linux?

Yes, several graphical diff tools, like Meld and Kompare, provide a user-friendly interface for file and directory comparisons on Linux.

Try this guide with our instant dedicated server for as low as 40 Euros