Linux Commands

Linux Diff Command

The difference is more likely to be a concept or feeling that can distinguish one thing from another. The “diff” command refers to variation. By analyzing the documents line by line, this tool demonstrates the distinctions between the files. It informs us which sections in one document need to be altered to create the documents similar, unlike its companion elements cmp and comm. The crucial aspect to keep in mind is that for the documents to be similar, diff utilizes specific special symbols and commands. You are given with directions on how to modify the first document so that it is consistent with the other document. Within this guide, we will discuss the use of the “diff” command in Ubuntu 20.04 using the Linux terminal.

We have been creating two text files in the working folder of our system.

It’s time to use the “diff” instruction in a query area to display the difference between both file contents. We try this “diff” instruction with both file names in a sequence, i.e. one.txt first and then two.txt file. The “diff” command execution shows us the difference between both files as:

  • Line numbers according to the first text file.
  • Special characters/symbols to let us know about the difference like change, add, delete.
  • Line numbers according to the second text file.

In our first illustration, we elaborate on the “a” symbol among the line numbers of the “diff” command output; “a” is used as the abbreviation of “Add”. The use of the “cat” instruction with both file names in the query area separately displays the content inside each text file, i.e. one.txt and two.txt. We have a total of 4 lines within the first file while the second file contains 5 lines. Line 1 is extra.

Then, we try the “diff” instruction on the terminal query area followed by the names of files – one.txt and two.txt. The execution of this instruction shows “0a1” as output along with line 1 from the second file which is an extra line in the second file. The numerical value “0” is used for line 0 or before line 1 of the first file, i.e. one.txt. The symbol “a” means “add”. Finally, the numerical value “1” means the first line of the second file. The mix output “0d1” means that the line 1 of the second file “two.txt” should be added at the top of the first file “one.txt” before the first line two make both files the same.

Now, we update both files to see another illustration. The first file “one.txt” contains 4 lines and the second file “two.txt” contains only 3 lines. The only difference for these files is the use of an extra line in the first file, i.e. line 1 = “Monday” which is not in the second file. After trying the “diff” instruction for the updated text files, we get “1d0” as the output. And the first line from the first file is displayed. In “1d0”, 1 means that the first line from the file “one.txt” should be deleted (as per the symbol “d”), and 0 means that the second file got no update. In the end, the line 1 from the first file “one.txt” is displayed for the clear difference.

Similarly, the “diff” command can display a difference between the lines of two files. Therefore, we update both text files once again. This time, the first file contains 5 lines while the second file contains only 4. The only difference is the use of an extra line in one.txt which is the line 3. The diff command used on both the files in the terminal displays an output “3d2” along with line 3 from the first file “one.txt”. This output shows that line 3 from the first file “one.txt” should be deleted so that we can sync both files at line 2 of the second file. The different line “3” is displayed so that we can get a clear idea about the difference.

Let’s have a clear and deeper look at the output. The 1,5c1,2 output from the following image shows that the lines 1 to 5 of a first file (one.txt) should be changed with lines 1 to 2 of a second file (two.txt). It means that the first 5 lines (1 to 5) of the one.txt file should be changed and replaced with the first two lines (1, 2) of the second file “two.txt” to make it the same. In the end, all 5 lines from the first file are displayed which should be replaced with the displayed 2 lines of the second file. This is how the symbol “c” tells a difference between two files – no line is the same in both files.

The “diff” command allows you to get its output in a context form. Imagine that you have the same two files with the following data in these files. The context around the output would be to display the main difference between the total simple files that are shown in the following image. Now, we have 5 lines in the first file “one.txt” and 4 lines in the second file “two.txt”.

To get the context-based output of the “diff” instruction, we need to use the “-c” option within the “diff” command. Using the previously-updated files in this “diff” command, we get the following output – date, day, along with the extra information regarding the text creation within the files. The 5 lines from the one.txt files are displayed. Most especially, its third line is highlighted using the “-“ character to just add it to another file.

Conclusion

We tried to briefly explain everything about the “diff” instruction. For this, we updated our text files every time and got an updated output for adding, removing, and changing the data in a file. The introduction explained the purpose of using the “diff” command in Linux and how to use it for Linux.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.