Linux diff Command

Ankur Gupta
Ankur Guptahttps://antarjaal.in
Web developer by profession, Over 10 years experience of building websites and web applications. Currently working on Epaper CMS Cloud (Software As Service based product). Special interest in computer and technology. After writing on the blogger platform for years, started his own tech portal Antarjaal.In.

On the Linux operating systems, the diff command analyzes two files and prints the lines that are different. In short, it outputs a set of instructions to make one file the same as another file.

The diff software does not actually change the files it compares. However, it can optionally produce a script (if the -e option is specified) for the program ed or ex that can be used to implement the changes.

For example, let’s discuss about two files, file1.txt and file2.txt

file1.txt has the following four lines:

I need to buy apples.
I need to run the laundry.
I need to wash the dog.
I need to get the car detailed

… and file2.txt has these four lines:

I need to buy apples.
I need to do the laundry.
I need to wash the car.
I need to get the dog detailed.

Then we can use the diff command which will show us which lines different between the two files.

diff file1.txt file2.txt

… and the result will be:

2,4c2,4
< I need to run the laundry.
< I need to wash the dog.
< I need to get the car detailed.
---
> I need to do the laundry.
> I need to wash the car.
> I need to get the dog detailed.

When diff shows these differences to you, it is telling you how to change the first file to match the second file. It is shown as follows.

The following line will appear in the first line:

  • Line number corresponding to the first file,
  • A letter (a for adding, c for change or d for deleting), and
  • Line number corresponding to the second file.

In our output above, “2,4c2,4” means: replace the second to fourth line of file1.txt with the second to fourth line of file2.txt

The lines that are preceded by < are those of the first file, and the lines that are preceded by > are from the second file.

(—) These three dashes separate the rows of both files.

Let us take another example of diff command

file1.txt has the following lines:

Gujarat
Uttar Pradesh
Kolkata
Bihar
Jammu and Kashmir

file2.txt has the following lines:

Tamil Nadu
Gujarat
Andhra Pradesh
Bihar
Uttar pradesh

Now if you give diff command then output will be:

$ diff file1.txt file2.txt
0a1
> Tamil Nadu
2,3c3
< Uttar Pradesh
 Andhra Pradesh
5c5
 Uttar pradesh
  • 0a1 is telling that the first line of file2.txt has to be “added” to file1.txt at the very beginning (denoted by zero). Here ‌a means to add.
  • 2,3c3 is telling us to replace the second and third lines of file1.txt with the third line of file2.txt. Here c means change.
  • 5c5 means to replace the fifth line of file1.txt with the fifth line of file2.txt.

By the way diff command output is not for humans. It is for reading by machines. The results shown above are the default output of the diff command.

Read More: Check Internet Connection Speed using Linux Command Line

If you want to see its output in column, then -y option can be used.

Linux diff command

It is very easy to understand this output. Here > means add, | Means to change and < means delete.

In normal state, this command is “case sensitive”. If you want to make it “case insensitive”, you can use the -i option.

Use the –version option to find out which version of ‌ diff is installed on your computer.

username@Ubuntu-PC:~/Documents$ diff --version
diff (GNU diffutils) 3.7
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later https://gnu.org/licenses/gpl.html.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Paul Eggert, Mike Haertel, David Hayes,
Richard Stallman, and Len Tower.

To know about all the options of diff command, you can read its man page by giving “man diff” command.

Read More: Run Visual Studio Code in Cloud using Code Server

1 COMMENT

Comments are closed.

Reality of Unlimited Web Hosting

You must have often noticed that many web hosting companies offer unlimited shared hosting. Does Unlimited really mean Unlimited?...

More Articles Like This