The “diff” command in Linux is used to compare two files to look up their differences. However, many people confuse this command with the “cmp” command. It is different from the “cmp” command because it also presents you with all the changes that can be made to both the files for making them identical. This article will show you a few examples of using the “diff” command in Linux.

“diff” Command Syntax and its Help Manual in Linux

The “diff” command in Linux can be used with the following syntax:

$ diff [option] File1 File2

Here, “option” can be replaced with the parameters that can be used with this command, whereas “File1” and “File2” represent the two files to be compared.

You can look through all the parameters that are available with this command by accessing its help manual with the command shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/Linux-diff-Command-Examples-01.png" data-lazy- height="27" src="data:image/svg xml,” width=”418″>

The help manual of the “diff” command is as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/Linux-diff-Command-Examples-02.png" data-lazy- height="510" src="data:image/svg xml,” width=”802″>

“diff” Command Examples in Linux

“diff” command can be combined with different parameters for comparing any two given files. We have created the following three examples to illustrate its usage. However, before going through these examples, we would like to show you the contents of the two files that we will use in all of these examples. We have simply used the “cat” command to display the contents of these two files on the terminal, as shown in the image below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/Linux-diff-Command-Examples-03.png" data-lazy- height="514" src="data:image/svg xml,” width=”575″>

Example 1: Using the “diff” Command without any Options

If you want to display the output of the “diff” command in a standard format, then you can use it without any options as follows:

We have replaced File1 with List.txt and File2 with List2.txt.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/Linux-diff-Command-Examples-04.png" data-lazy- height="25" src="data:image/svg xml,” width=”537″>

The differences between our two files, along with the changes that are needed to carry out to make them both identical, are shown in the output below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/Linux-diff-Command-Examples-05.png" data-lazy- height="183" src="data:image/svg xml,” width=”554″>

Example 2: Using the “diff” Command to Produce the Output in the Context Mode

The context mode of the “diff” command allows you to view additional information related to the specified files and the changes needed to make them identical. We can use this mode in the following manner:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/Linux-diff-Command-Examples-06.png" data-lazy- height="27" src="data:image/svg xml,” width=”568″>

You can visualize from the output of this command that the modification date and time of both the files are also displayed along with the changes that are needed to be made.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/Linux-diff-Command-Examples-07.png" data-lazy- height="375" src="data:image/svg xml,” width=”708″>

Example 3: Using the “diff” Command to Produce the Output in the Unified Mode

The unified mode of the “diff” command is very much similar to the context mode; however, the only difference is that it avoids displaying redundant information. To use this mode, we will have to execute the command shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/Linux-diff-Command-Examples-08.png" data-lazy- height="29" src="data:image/svg xml,” width=”572″>

You can visualize from the output of this command that only relevant and unique information from both the files is being displayed on the terminal. In contrast, all the redundant information present in both the files has been omitted. To confirm this difference, you can compare this output with the output of Example # 2.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/Linux-diff-Command-Examples-09.png" data-lazy- height="254" src="data:image/svg xml,” width=”575″>

Conclusion

This article threw light on using the “diff” command in Linux to compare two files and suggest all the changes that can be made to both the files to make them identical. Moreover, it also explained the difference between the “cmp” and the “diff” command in Linux.