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:
< 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:
Jammu and Kashmir
file2.txt has the following lines:
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.
If you want to see its output in column, then -y option can be used.
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.