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


> Email:

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

$ cat file2.txt


Welcome to nixCraft!

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

Ph: 555-11112223

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:                                         | URL:
                                                              > Email:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 8 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
8 comments… add one
  • Sergio Morstabilini Aug 3, 2011 @ 16:05

    for who prefers a visual tool, I recommend meld (

  • aceleron May 9, 2013 @ 13:04

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

  • Johnutz Dec 4, 2013 @ 15:16

    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

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

    • Matt Jun 4, 2014 @ 20:16

      Yes. Just give the absolute path of the files.

    • CRACKBOY Dec 14, 2016 @ 8:25

      thank you Ashish

  • Ravi May 3, 2016 @ 8:55

    Thanks it works.

  • terratronix Jun 19, 2016 @ 7:55

    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 Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum