≡ Menu

How do I Compare two files under Linux or UNIX?

You need to use diff command to display line-by-line difference between two files. The general syntax of diff command as follows:
diff FILE1 FILE2

FILE1 FILE2: Diff command will examine both file1 and file2 and tells you what changes need to be made for file1 and file2 to match. Please note that diff command point to which lines need be:

  1. Added (a)
  2. Deleted (d)
  3. Changed (c)
  4. Further lines in file1 identified with a less than () symbol and lines in file2 with a grater than (>) symbol.


diff file1.txt file2.txt


 URL: www.nixcraft.in
> Email: support@nixcraft.in

The contents of both files:

$ cat file1.txt


Welcome to nixCraft!

If undeliverd return to nixCraft
#404, DC bay area, 2nd phase,

Ph: 555-11112223
URL: www.nixcraft.com

$ cat file2.txt


Welcome to nixCraft!

If undeliverd return to nixCraft
#404, DC bay area, 2nd phase,

Ph: 555-11112223
URL: www.nixcraft.in
Email: support@nixcraft.in

Side-by-side merge of file differences
You can get a clear-cut visual difference between two text files using the command sdiff:

$ sdiff file1.txt file2.txt


Welcome to nixCraft!                                            Welcome to nixCraft!

If undeliverd return to nixCraft                                If undeliverd return to nixCraft
#404, DC bay area, 2nd phase,                                   #404, DC bay area, 2nd phase,
Pune.                                                           Pune.

Ph: 555-11112223                                                Ph: 555-11112223
URL: www.nixcraft.com                                         | URL: www.nixcraft.in
                                                              > Email: support@nixcraft.in
Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 8 comments… add one }
  • Sergio Morstabilini August 3, 2011, 4:05 pm

    for who prefers a visual tool, I recommend meld (http://meld.sourceforge.net/)

  • aceleron May 9, 2013, 1:04 pm

    One more time, cyberciti help me a lot. Thanks and nice site. I became an user site.

  • Johnutz December 4, 2013, 3:16 pm

    Also vimdiff can be used, for the guys who prefer a visual tool and are also familiar with vim.

  • Ashish May 26, 2014, 6:50 am

    What if files are present in two different directories? can we compare that too with sdiff?

    • Matt June 4, 2014, 8:16 pm

      Yes. Just give the absolute path of the files.

    • CRACKBOY December 14, 2016, 8:25 am

      thank you Ashish

  • Ravi May 3, 2016, 8:55 am

    Thanks it works.

  • terratronix June 19, 2016, 7:55 am

    Used the first method before scrolling down. Thought: “damn my files have 1000 lines, i’d rather see the diff side by side. I bet someone already build something for this.” *scrolls down* Not disappointed! Thanks! and Linux rocks!

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: