How to Compare Text Files Using diff

Hi! If you're new here, you may want to subscribe to the Unix Tutorial RSS feed to get regular tips & tricks for all flavors of Unix. Thanks for visiting!

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!

Please share: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • BlinkList
  • Fark
  • Furl
  • kick.ie
  • Netscape
  • Reddit
  • StumbleUpon
  • Technorati
  • YahooMyWeb

3 comments ↓

#1 gustavo castro on 06.07.08 at 5:24 pm

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

#2 Syed Shariyar Murtaza on 06.08.08 at 11:59 pm

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

#3 Compare Directories in Unix using diff command | UNIX Tutorial: Learn UNIX on 06.09.08 at 1:36 pm

[...] Hi! If you're new here, you may want to subscribe to the Unix Tutorial RSS feed to get regular tips & tricks for all flavors of Unix. Thanks for visiting!Certain situations require you to quickly confirm which files between two directories are different, and while your particular requirements may suggest writing a script for this task, I want to make sure you're familiar with the basics first - majority of directory comparisons can be done using diff command (yes, that's right - the same one used for comparing files). [...]

Leave a Comment