Compare two files in Linux and find the differences
Follow @ggarronAs in GNU/Linux Operating System, the most if not all configuration files are text based, and you usually do changes to them, now if you are wise you should keep a backup of your configuration file before change it.
Well, I am talking about this, because this is the most frequent scenario where I use diff command, you may have some others.
Well let's now see how it works
I have these files.
numbers1.txt
1 2 3 4 5 6 7 8 9 0
numbers2.txt
0 2 3 4 5 7 6 8 9 1
Let's start with diff
diff numbers1.txt numbers2.txt
1c1 < 1 --- > 0 6d5 < 6 7a7 > 6 10c10 < 0 --- > 1
Let's now see what does that mean.
Those are instructions to make the files looks the same, and here is how to read them.
1c1 "Replace line 1 of the first file with the line 1 of the second file" And shows you which values to extract and add, in this case erase 1 and write 0, and the files will look the same. 6d5 "Delete line 6 of the first file" And the value to erase is 6 7a7 "Add line 7 to first file" And the value to add is 6 10c10 "Replace line 10 of the first file with the line 10 of the second file" And the value to erase is 0 and the value to add is 1
If you are not looking for instructions to make the files look the same, but rather to look for the differences, you may use diff with some options like these.
diff -u numbers1.txt numbers2.txt
--- numbers1.txt 2009-02-21 09:11:10.000000000 -0400 +++ numbers2.txt 2009-02-21 08:57:42.000000000 -0400 @@ -1,10 +1,10 @@ -1 +0 2 3 4 5 -6 7 +6 8 9 -0 +1
Now it is easier to read, the first file is identified with three dashes (---) and the second with three plus signals (+++)
And to read the output you should say, the first file has 1 where the second 0, then it is all the same until where the first file has 6 and the second not, then the second file has 6 where the first not, and at the end the first has 0 where the second 1.
You can also compare them side by side, and this is my favorite.
diff -y numbers1.txt numbers2.txt
1 | 0
2 2
3 3
4 4
5 5
6 <
7 7
> 6
8 8
9 9
0 | 1
The | (pipe) shows difference, and the greater than and less than symbols shows where one file has info that the other does not.