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.
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 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.
1d0
– The first line (1
) from the first file should be deleted (d
). If not, it would appear in line0
in the second file.< Apple
–The content you need to delete (as referred to with1d0
).2a2,3
– In line2
of the first file, you should add (a
) lines2
and3
(2,3
) from the second file.> Peach
,> Apple
– The content you need to add (as referred to with2a2,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.
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:
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.
However, if you add the -i
option, there is no output as the command doesn’t detect any differences.
--version Option
To check the version of diff
running on your system, run the command:
diff --version
--help Option
To output a summary of diff
usage run:
diff --help
Note: Learn how to use the diff --color command to change the color of the output.
Other diff Options
Other options that diff
supports include:
-a / --text | View files as text and compare them line-by-line. |
-b / --ignore-space-change | Ignore white spaces when comparing files. |
-B / --ignore-blank-lines<code> | Ignore blank lines when comparing files. |
--binary | Compare and write data in binary mode. |
-d --minimal | Modify the algorithm (for example, to find a smaller set of changes). |
-e / --ed | Make output a valid ed script. |
-E / --ignore-tab-expansion | Ignore tab extension when comparing files. |
-l / --paginate | Run the output through pr to paginate it. |
-N / --new-file | Treat a missing file as present but empty. |
-q / --brief | Output whether files differ without specifying details. |
-s / --report-identical-files | Output when the files are identical. |
-w / --ignore-all-space | Ignore 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.