Using diff command in Linux

The diff is a command used in Unix like operating systems which compares two files line by line and display differences between them. It tells which line is to be changed to make two files identical. You can also use it to compare the content of two directories.

In this article, we will discuss the usage of the diff command in Linux along with some examples.

How to use diff command in Linux

The syntax of using the diff command in Linux is given below-

diff [option] File(s)

You can get customized output by using different options with the diff command. You can find a detailed list of options on the man page of the diff command.

How does diff command works

Let us understand the working of the diff command with the given example.

Suppose we have two files file1.txt and file2.txt which contains country names you can see them below –

file1.txt

Australia
Canada
Russia
India
USA
France
South Africa

file2.txt

Russia
India
Australia
South Africa
France
USA
China

By using different options with diff command you can display the output in various formats. The following is the normal or simplest form of diff command in Linux.

diff file1.txt file2.txt

It will display the given output –

Where,

1,2d0, 5,6c3 and 7a5,7 are change commands. Each of them contains the following from left to right –

  • The line number or the range of lines in the first file
  • Special change symbol
  • The line number or the range of lines in the second file

The meaning of the change symbol is given below –

  • a – Add the lines
  • c – Change the lines
  • d – Delete the lines

After the change command, complete lines that are removed (<) and added(>) to the file are given.

Now let’s understand the output.

1,2d0 –  Delete lines 1 and 2 of file1.txt  so that it matches with file2.txt at 0

5,6c3 – Change line 5 and 6 of file1.txt with line 3 of file2.txt

7a5,7 – At line 7 of file1.txt add lines 5 to 7 of file2.txt

After making changes both the files will become identical.

Display the output of diff command in context format

To view the output of the diff command in context mode you need to use the option -c with it. Now see the full command which is given below.

diff -c file1.txt file2.txt

This will produce the given output-

In the above output first two lines show the name of files and timestamps. *** indicate the file1.txt and —-represents file2.txt.

***1,7*** and —1,7— shows the range of lines in file1.txt and file2.txt i.e. from line 1 to line 7.

You can also see some symbols at the starting of many lines of file1.txt and file2.txt.

Lines that start with - corresponds to nothing in the second file that means these lines are missing in the second file.

Lines that start with + correspond to nothing in the first file these are missing in the first file.

The lines starting with ! symbol are the lines changed between two files. Each line starting with ! has a match in the second file.

Display the output of the diff command in Unified format

Using the option -u with diff command produces the output in a unified format. The unified format is similar to the context format but it shows the information in a concise form.

Now see the full command below –

diff -u file1.txt file2.txt

This will produce the given output.

This output also starts with filenames and timestamps after that one or more sections are given that describe the difference between these two files. represents the first file and the +++ symbol represents the second file.

The next line starts with two @ symbols followed by the line range of the first file and the line range of the second file.

Lines starting with a minus symbol are the lines that are removed from the first file.

Lines starting with a plus symbol are the lines that are added to the first file.

By default, the output of the diff command is case sensitive to make it an insensitive use option -i with this command.

For example to display the difference in two files in a unified format which is insensitive we will use –

diff -ui file1.txt file2.txt

Now this will display the given output.

For more information, you can see the man page of the diff command in your terminal.

man diff

Conclusion

Finding differences between two files is one of the most common tasks. In this article, you have seen how to use the diff command to compare two files.

If you have any query then write us in the comments below.

Leave a Comment