If you need to compare two text files in Unix, you're mostly likely to use the diff command.
Today I'll talk about the simplest scenario: you want to compare two files and understand if there are any differences.
Suppose you have two files in /tmp directory:
/tmp/1.txt:
aaa bbb ccc ddd eee fff ggg
and /tmp/2.txt:
bbb c c ddd eee fff ggg hhh
I have deliberately created them so short and simple – this way it's easier to explain how the comparison works. If there are no differences between the files, you will see no output, but if two text files are indeed different, all the text mismatches will be highlighted using the standard diff output:
$ diff /tmp/1.txt /tmp/2.txt 1d0 < aaa 3c2 < ccc --- > c c 7a7 > hhh
Lines like "1d0" and "3c2" are the coordinates and types of the differences between the two compared files, while lines like "< aaa" and "> hhh" are the differences themselves.
Diff change notation includes 2 numbers and a character between them. Characters tell you what kind of change was discovered:
d – a line was deleted
c – a line was changed
a – a line was added
Number to the left of the character gives you the line number in the original (first) file, and the number to the right of the character tells you the line number in the second file used in comparison.
So, looking at the two text files and the diff output above, you can see what happened:
This means that 1 line was deleted. < aaa suggests that the aaa line is present only in the original file:
1d0 < aaa
And this means that the line number 3 has changed. You can see how this confirms that in the first file the line was "ccc", and in the second it now is "c c".
3c2 < ccc --- > c c
Finally, this confirms that one new line appeared in the second file, it's "hhh" in the line number 7:
7a7 > hhh
That's all you need to know to start playing with text comparisons yourself. Stay tuned for more!
Useful command line options for diff
Here are just some of the options, I won't demonstrate them but simply wanted you to know what's possible:
-i option – allows you to ignore case when comparing lines (aaa will equal AaA, etc)
-r option – recursively compare directories (and any files found there)
-s option – report identical files (you can script around this if your task is to confirm which files are identical)
Related books
If you want to learn more, here's a great book:
gustavo castro says
Is there any way to compare files in tow directories for example you have dir A and dir B and both are with the same file names but i want to know wich of these files are diferents from each other (if this is not posible for the comun way you may please sugest me a script or maybe a server like CVS) thanks
Syed Shariyar Murtaza says
It's pretty easy. Save the following script as xyz.sh file and run it — ./xyz.sh.
echo "Comparing original directory with another directory"
for i in {1..4130} # 1 to 4130 files
do
cmp /directory1/t$i /directory2/t$i
done
JulianGomez says
Suppose you have two files A and B, each with 3 columns $1 ,$2 ,$3. How can I print the lines, of both files, that share the first two values of each line ?
Ludwig says
Thanks for the great tutorial.
The example covers a lot of scenario
aiza says
Suppose u have 2 directories A and B, B is supposed to feed files into A on a daily bases but B can contain files that it had already fed A the previous day,how do u prevent B from duplicating the files that are in A?
jai says
thanks man
Mohammed says
Saved my A$$ 🙂
Siva Prasad Addepalli says
The tutorial is very good, i understood the concept very easily, the example is good.
neetu says
thanks, but give more example.
Tom says
Ah, short and concise. Just what i needed. ty!
Thiyagu says
Great work and nice explaination. Plz Continue ..
Vin says
Very nice, good job!~
Jonathan says
Now I understand the basics about diff, thank you!