How to Compare Text Files Using diff

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:

unix-power-tools
Unix Power Tools