How to Use the Linux diff Command

December 29, 2021

Introduction

The Linux diff command is used to compare two files line by line and display the difference between them. This command-line utility lists changes you need to apply to make the files identical.

Read on to learn more about the diff command and its options with easy-to-follow examples.

How to use the Linux diff command.

diff Syntax

The syntax for using the diff command is:

diff [option] file1 file2

Output Syntax

When working with diff, it is crucial to know how to interpret the output, which consists of:

  • Output starting with < refers to the content in the first file.
  • Output starting with > refers to the content in the second file.
  • Line numbers corresponding to the first file.
  • A special symbol. Special symbols indicate how the first file needs to be edited to match the second file. The output may display:
    • a (add)
    • c (change)
    • d (delete)
  • Line numbers corresponding to the second file.
Diff command output

diff Example

To show how the diff command works, we created two sample files and compared their content.

Create Two Sample Files

1. First, using the terminal, create a Linux file named example1.txt. We use the Nano text editor, but you can use a text editor of your choice.

sudo nano example1.txt

2. Once the text editor creates and opens the file, add the following lines to it:

Apple
Orange
Banana
Watermelon
Chery

3. Save and exit the file – hold Ctrl + X and confirm by pressing Y.

4. Next, create an example2.txt file by running:

sudo nano example2.txt

5. Add the following content to the file:

Orange
Peach
Apple
Banana
Melon
Cherry

6. Save the changes and exit.

Note: To display the contents without opening the file for editing, use the cat command.

Compare the Files with the diff Command

1. With the two sample files in place, use the diff command to see how they differ and how to make them identical:

diff example1.txt example2.txt 

The output lists instructions on how to modify the first file to have the same content as in example2.txt. Let’s look at the output for the sample files and decode the instructions.

Example of Linux diff command.
  • 1d0 – The first line (1) from the first file should be deleted (d). If not, it would appear in line 0 in the second file.
  • < Apple –The content you need to delete (as referred to with 1d0).
  • 2a2,3 – In line 2 of the first file, you should add (a) lines 2 and 3 (2,3) from the second file.
  • > Peach, > Apple –  The content you need to add (as referred to with 2a2,3).
  • 4c5 –  The fourth line (4) from the first file should be changed (c) to the fifth line (5) from the second file.
  • < Watermelon – The content you need to change.
  • > Melon – What you need to change it to.

Note: Once you have the output instructions, you can use the patch command to save the output and apply the differences.

diff Options

Without additional options, diff displays the output in the default format. There are ways to modify this output to make it more understandable or applicable for your use case. Read on to learn more about diff command options.

-c Option

The context format is a diff command-line utility option that outputs several lines of context around the lines that differ.

To display the difference between the files in context form, use the command:

diff -c file1 file2

Take a look at the output for the sample files in the context form in the image below.

How to use the diff command with the -c option.

Lines displaying information about the first file begin with ***, while lines indicating the second file start with ---.

The first two lines display the name and timestamp of both files:

*** example1.txt             2021-12-27 10:53:30.700640904 +0100
--- example2.txt             2021-12-27 10:54:41.304939358 +0100

**************** - is used just as a separator.

Before listing the lines from each file, the output starts with the line range of the files:

***  1,5 ****
---  1,6 ----

The rest of the lines list the content of the files. The beginning of each line instructs how to modify example1.txt to make it the same as example2.txt. If the line starts with:

- (minus) – it needs to be deleted from the first file.
+ (plus) – it needs to be added to the first file.
! (exclamation mark) – it needs to be changed to the corresponding line from the second file.

If there is no symbol, the line remains the same.

Therefore, in the example above, you should delete Apple from the first line, replace Watermelon with Melon in line four, and add Peach and Apple to lines two and three.

-u Option

The unified format is an option you can add to display output without any redundant context lines. To do so, use the command:

diff -u file1 file2

Now, let's examine the output for the sample files in the unified format:

How to use the diff command with the -u option.

Lines displaying information about the first file begin with ---, while lines indicating the second file start with +++.

The first two lines display the name and timestamp of both files:

*** example1.txt             2021-12-27 10:53:30.700640904 +0100
--- example2.txt             2021-12-27 10:54:41.304939358 +0100

@@ -1,5 +1,6 @@ - shows the line range for both files.

The lines below display the content of the files and how to modify example1.txt to make it identical to example2.txt. When the line starts with:

- (minus) – it needs to be deleted from the first file.
+ (plus) – it needs to be added to the first file.

If there is no symbol, the line remains the same.

In the example above, the output instructs that Apple and Watermelon should be removed, whereas Peach, Apple, and Melon should be added.

-i Option

By default, diff is case sensitive. If you want it to ignore case, add the -i option to the command:

diff -i file1 file2

For example, if we create one file with the following lines:

Apple
Orange
Banana
Watermelon
Cherry

And another file with the content:

Apple
orange
Banana
watermelon
Cherry

The output with no additional options shows there are differences between the files and gives instructions how to modify them.

Sample of diff command output showing its case sensitivity.

However, if you add the -i option, there is no output as the command doesn’t detect any differences.

diff command showing no output.

--version Option

To check the version of diff running on your system, run the command:

diff --version
Command for checking diff version.

--help Option

To output a summary of diff usage run:

diff --help

Other diff Options

Other options that diff supports include:

-a / --textView files as text and compare them line-by-line.
-b / --ignore-space-changeIgnore white spaces when comparing files.
-B / --ignore-blank-lines<code>Ignore blank lines when comparing files.
--binaryCompare and write data in binary mode.
-d--minimalModify the algorithm (for example, to find a smaller set of changes).
-e / --edMake output a valid ed script.
-E / --ignore-tab-expansionIgnore tab extension when comparing files.
-l / --paginateRun the output through pr to paginate it.
-N / --new-fileTreat a missing file as present but empty.
-q / --briefOutput whether files differ without specifying details.
-s / --report-identical-filesOutput when the files are identical.
-w / --ignore-all-spaceIgnore white space when comparing files.

Note: Learn also about the comm command, a simple Linux utility for comparing files with focus on the common content.

Conclusion

The diff command helps you compare files and instructs how to modify them. This article showed you how to interpret its instructions to make the compared files identical.

To learn more about other Linux commands, check out our Linux commands cheat sheet.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
The pwd Linux Command
December 22, 2021

The pwd command outputs your current working directory in Linux. This tutorial shows how...
Read more
How to Use the ulimit Linux Command
December 9, 2021

The ulimit shell command allows you to view or limit the amount of resources a user can consume...
Read more
How To Use shred Linux Command
March 31, 2021

Deleting a file in Linux or any other OS does not actually remove the data from the drive...
Read more
How to Use Linux nohup Command
December 16, 2021

Learn how to use the nohup command to continue running commands and processes...
Read more
  • © 2022 Copyright phoenixNAP | Global IT Services. All Rights Reserved.