Today, in this article, you will learn how to use the diff command in Linux to compare the content of two different files or directories to find the changes that are required to make them identical.
Tutorial Details
Description | Comparing Files or Directories |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | diff |
Internet Required | No |
What is Diff Command in Linux?
The diff command in Linux is a built-in utility that comes along with most Linux distributions for the purpose of assisting users to find the differences between two different files specified as arguments and telling them the changes required to make both files identical.
Although it’s mostly highlighted for finding differences between two files, you can also use the same tool to find differences between the structures of two different directories specified as arguments.
If you are familiar with git versioning, then you can use this tool in the same way to find the changes made in the file or directory since the last backup and what they are.
Syntax of the Diff Command
The diff command takes three arguments: one is the option, and the other two are the file (or directory) names or their paths.
$ diff [OPTION] file1 file2
Now, before moving on to the explanation, take a look at the content of the following two files used as examples for this command.
++++++++++++++++++++++++++++++[ file1.txt ]++++++++++++++++++++++++++++++++
C
Python
Typescript
Golang
++++++++++++++++++++++++++++++[ file2.txt ]++++++++++++++++++++++++++++++++
C
C++
C#
Javascript
Python
Golang Rust
Now, let’s understand the crucial elements that you must know to interpret the output of the diff command.
- Output starting with (
<
) symbol indicates that the line is present in the first file but not in the second file. - While, output starting with (
>
) indicates that the line is present in the second file but not in the first file. - The following are the special symbols that assist you, what steps to be made to make the first file match the second file (vice-versa).
a
(add)c
(change)d
(delete)
Note that the diff command can display the output in multiple formats, with the most commonly used are normal (or standard), context, and unified format.
All of this output gives you the information you need to make changes to the files so that they become identical. If the files already match, then no output will be displayed.
Compare Files in Normal (or Standard) Format
When you use the diff command without any flags to compare the differences between two files, it produces the output in normal format.
$ diff file1.txt file2.txt
The following is an output of the above command.
If you are using the same files as me, then you will also be presented with the above output, so let’s understand how you can utilize this information to match both files.
Let’s start with the “1a2,4
“, and “3,4c6
“, which are also known as the change command. Each change command is formatted in the following way, from left to right:
- The line number or range of lines in the first file.
- A special change character (
a
= add), (c
= change), and (d
= delete). - The line number or range of lines in the second file.
The change command is followed by the (<
) symbol suggesting what to remove and the (>
) symbol suggesting what to add.
With this information, let’s understand our output:
- “
1a2,4
” – Add the line “2
“, “3
“, and “4
” in the “file1.txt
” (after the line “1
“)- “
> C++
“, “> C#
“, and “> Javascript
” are the suggested lines that needs to be added in the “file1.txt
” (after the “C
“)
- “
- “
3,4c6
” – Change the line “3
” and “4
” in the “file1.txt
” with the line “6
” on the “file2.txt
“.- “
< Typescript
“, “< Golang
” are the suggested lines that needs to be changed with the “> Golang Rust
” in the “file1.txt
“
- “
Compare Files in Context Format
The context format is more comprehensive in detail and provides a simpler explanation for the required modification to make both files identical.
Simply, use the “-c
” flag to produce the output in context format.
$ diff -c file1.txt file2.txt
The following is an output of the above command.
The first two lines in the output give you the files timestamp information and how they are represented. Here, lines displaying information about the first file will begin with “***
“, while lines indicating the second file start with “---
“.
The third line “***************
” is used just as a separator.
- After the separator, you will be given the line range for each file before listing them.
- Lines starting with double spaces are identical in both of the files.
- Lines starting with (
+
) symbol are the lines that are missing in the first file. - Lines starting with (
-
) symbol are the lines that are present in the first file but missing the second file. - Lines starting with (
!
) symbols are the lines that are changed between the two files.
Now, let’s understand the output using this information.
- “
*** 1,4 ***
” and “--- 1,6 ---
” tell the line range of the first and second files. - Now, “
C
” and “Python
” are the common lines between both of the files. - However, you need to add the “
+ C++
“, “+ C#
“, and “+ Javascript
” lines between the “C
” and “Python
” from the “file2.txt
” to “file1.txt
“. - And the last “
! Typescript
” and “! Golang
” from the “file1.txt
” needs to be changed with “! Golang Rust
” from the “file2.txt
“.
Compare Files in Unified Format
The unified format is an improved (or simplified) version of context format; it simplifies all the steps required to make both files identical.
Use the “-u
” flag with the diff command to produce the output in unified format.
$ diff -u file1.txt file2.txt
The following is an output of the above command.
The output begins in the same way as context format, displaying the file timestamp and symbol representing the file.
- Here, between the “
@@
” symbol the “-1,4
” and “+1,6
” represent the line range of each file. - Lines starting with two spaces are identical in both of the files.
- Lines starting with “
+
” symbol indicates the line needs to be added in the first file. - Lines starting with “
-
” symbol indicates the line needs to be removed from the first file.
Ignore Casing While Comparing the Files
By default, the diff command checks the lines with case-sensitivity. If you want to ignore the case, add the “-i
” flag option to the command.
The following are two files with the same line (except one) but with different casing.
++++++++++++++++++++++++++++++[ file1.txt ]++++++++++++++++++++++++++++++++
apple
banana
++++++++++++++++++++++++++++++[ file2.txt ]++++++++++++++++++++++++++++++++
Apple
BANANA
Cat
When you compare both files without using the “-i
” flag, you will get the following output:
$ diff -u file1.txt file2.txt
Output:
As you can see above, having the same line with different casing suggests that you to modify those lines. However, you can change this behavior by using the “-i
” flag, as shown.
$ diff -i -u file1.txt file2.txt
Output:
Now you need to only add the “+Cat
” line (that is different) from the “file2.txt
” to “file1.txt
“.
Compare Directory Structure with Diff Command
As I mentioned at the beginning of this article, using the diff command, you can compare the structure of two different directories.
For that, you can specify the directory name or absolute path; the following are the two directories in the tree structure that we will use to compare.
Now, when you execute the diff command specifying these two directory paths, you will get the following output:
$ diff dir1/ dir3/
Output:
As you can see, you got the difference between the two directory structures, but there is one problem: the above output does not include the differences in the nested directories (like “dir1/dir2
” and “dir3/dir2
“).
However, you can use the “-r
” flag to compare the directories that are nested, as shown.
$ diff -r dir1/ dir3/
Output:
Final Word
Comparing differences between two files or directories is a very common task that every Linux administrator does two or three times in a week.
You can execute the “diff --help
” or “man diff
” command to get more information about this command and its flags.
If you have any questions or queries related to this article, feel free to leave them in the comment section.
Till then, sayonara.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.