Beginners Guide for Diff Command in Linux

Linux TLDR
Last Updated:
Reading time: 5 minutes

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

DescriptionComparing Files or Directories
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesdiff
Internet RequiredNo

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.

Normal format in the diff 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.

Context format in the diff 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.

Unified format in the diff 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:

Diff command with case sensitive

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:

Diff command without case-sensitivity

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.

Directory structure used for diff command example

Now, when you execute the diff command specifying these two directory paths, you will get the following output:

$ diff dir1/ dir3/

Output:

Comparing directories using the diff command

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:

Comparing directory recursively using diff command

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.