Comparing files in Linux with diff
Comparing files
Suppose you have a file 1 with the following content
a
And a file 2 whose first line is empty
a
you can compare them with the diff command
diff 1 2
0a1 >
The output of the diff command contains instructions on how to turn the first file into the second.
0a1 means:
After the zeroth line of the first file you need to add the first line of the second file.
In other words: at the beginning of the first file you need to insert the first (empty) line of the second file
and then the files will become identical
> means the second file. After it there should be the content of the line that needs to be inserted, but it
in our case is empty, so there is nothing.
a - add - to add
c - change - to change
d - delete - to delete
Let's make sure that we understood everything correctly - open the file 2 and write some text into the first line that was empty. Nothing else needs to be changed.
cat 2
www.topbicycle.ru
a
Let's compare the files again 1 and 2 . We expect that nothing will change, only after > our text will appear
diff 1 2
0a1 > www.topbicycle.ru
As you can see, that is exactly what happened, because to get the second file from the first you need at the very beginning of the first file to add a line with the text www.topbicycle.ru
If we now try to compare not the file 1 with 2 but the other way round 2 with 1 . What should change: now the file 2 is taken as the basis and the diff command will tell us what needs to be done to get from the file 2 the file 1
diff 2 1
1d0 < www.topbicycle.ru
1d0 means that you need to delete the first line of the first file and then we arrive at the beginning of the second file
< www.topbicycle.ru is the content of the line of the first file in question
Consider file 3 in which the first and second lines are empty and the third contains the character a
Think for yourself what will happen if you compare the files
1 and 3.
And also the other way round 3 and 1
vi 1
a
vi 3
a
diff 1 3
0a1,2 > >
0a1,2 means that at the beginning of the file 1 you need to add the first and second lines of the file 3
If we compare in the opposite direction
diff 3 1
1,2d0 < <
1,2d0 means that in the file
3
you need to delete lines 1 and 2 and then we arrive at the beginning of the file 1
0 at the end can be understood as the line by which the files will be aligned.
As an example, let's create file 4 in which the first line is the character a, the second line the character b
vi 4
a
b
Let's add three empty lines in the middle and save as file 5
vi 5
a
b
Let's compare 4 and 5
diff 4 5
1a2,4 > > >
To get from file 4 the file 5 you need after the first line of file 4 to add the lines from the second to the fourth of file 5
Everything is simple here, let's try the reverse
diff 5 4
2,4d1 < < <
To get from file
5
the file
4
you need to delete the lines from the second to the fourth in file 4 and then we
will land on line 1 of file
4
That is, in this example, after deleting, we aligned by line 1 and
not by the beginning of the file as in the previous example.
Suppose you have a file 1 with the following content
a
b
c
And an empty file 2 you can compare them with the diff command
diff 1 2
1,3d0 < a < b < c
Let's decipher the result: you need to delete the first three lines in the first file and then the files will align at the beginning of the files.
If we now add a to the first line of file 2 and compare again
diff 1 2
2,3d1 < b < c
Let's decipher the result: you need to delete the second and third lines in the first file and then the files will align after the first line of the files.
Suppose you have a file websites.txt with a list of sites
cat websites.txt
https://www.urn.su https://www.heihei.ru https://www.topbicycle.ru
Copy it with the command
cp websites.txt newsites.txt
It is easy for now to make sure that the files are identical, you can even run
cat newsites.txt
https://www.urn.su https://www.heihei.ru https://www.topbicycle.ru
And understand that nothing has changed.
If the file is too large you can use the command diff
diff websites.txt newsites.txt
While the files are identical the output will be empty
Add a new entry to the file newsites.txt
echo https://www.aredel.com >> newsites.txt
cat newsites.txt
https://www.urn.su https://www.heihei.ru https://www.topbicycle.ru https://www.aredel.com
The diff command will now show the place in which the files differ
diff websites.txt newsites.txt
3a4 > https://www.aredel.com
3a4 means that the difference was found between
the third line of the first file and the fourth line of the second.
In this case the fourth line of the first file simply does not exist
> https://www.aredel.com these are the characters that are not in the first file
Make finer changes. For example, let's delete www. before heihei.ru in the second file
vi newsites.txt
https://www.urn.su https://heihei.ru https://www.topbicycle.ru https://www.aredel.com
diff websites.txt newsites.txt
2c2 < https://www.heihei.ru --- > https://heihei.ru 3a4 > https://www.aredel.com
< means - in the first file
> means - in the second file
diff finds all lines with differences and outputs them. So if
you make some more changes in the same line, for example add at the end /Spain/
fundamentally nothing will change
vi newsites.txt
https://www.urn.su https://heihei.ru/Spain/ https://www.topbicycle.ru https://www.aredel.com
diff websites.txt newsites.txt
2c2 < https://www.heihei.ru --- > https://www.heihei.ru/Spain/ 3a4 > https://www.aredel.com
If we add several empty lines
https://www.urn.su https://www.heihei.ru https://www.topbicycle.ru https://www.aredel.com
Ranges with empty lines will be shown
1a2,5 > > > > 2a7,13 > > > > > > > 3a15 > https://www.aredel.com
a2,5 means that from the second to the fifth lines
a7,13 means that from the seventh to the thirteenth
If we delete several lines
vi newsites.txt
https://www.urn.su https://www.aredel.com
diff websites.txt newsites.txt
2c2 < https://www.heihei.ru --- > https://heihei.ru/Spain/ 3a4 > https://www.aredel.com
Article author: Andrei Olegovich