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:

aaa bbb ccc ddd eee fff ggg

and /tmp/2.txt:

c c

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
< aaa
< ccc
> c c
> 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:

< 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".

< ccc
> c c

Finally, this confirms that one new line appeared in the second file, it's "hhh" in the line number 7:

> 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

  • gustavo castro

    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

    It's pretty easy. Save the following script as file and run it — ./

    echo "Comparing original directory with another directory"
    for i in {1..4130} # 1 to 4130 files
    cmp /directory1/t$i /directory2/t$i

  • Pingback: Compare Directories in Unix using diff command | UNIX Tutorial: Learn UNIX()

  • JulianGomez

    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

    Thanks for the great tutorial.

    The example covers a lot of scenario

  • aiza

    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

    thanks man

  • Pingback: AllyRR (Root and Recovery Tool) WINDOWS - Android Forums()

  • Mohammed

    Saved my A$$ 🙂

  • Siva Prasad Addepalli

    The tutorial is very good, i understood the concept very easily, the example is good.

  • thanks, but give more example.

  • Tom

    Ah, short and concise. Just what i needed. ty!

  • Great work and nice explaination. Plz Continue ..

  • Pingback: Compare directories in Unix using diff command « Observe.Think.Touch Nature()

  • Vin

    Very nice, good job!~

  • Jonathan

    Now I understand the basics about diff, thank you!