There may be a time, in your life, when you need to compare two text files on a linux server, and all you have is a CLI (Command Line Interface).  How can this be done? Well, it’s a pretty easy task (when you know how to do it!)

All you need is the diff command, and this is how you use it:

diff [first-file] [second-file]

Is it that easy?!? The answer is: YES!

So, for example, let’s say we have two style sheets on our webserver, style1.css and style2.css, and we want to compare them. We will use the following command:

diff style1.css style2.css

and this is what we’ll get in return:

140c138
< color: #999;
---
> color: #666;

What is diff trying to tell us? That line 140 of our first file (style1.css) contains the text color: #999; while line 138 of the second file (style2.css) is different and contains color: #666;.
Did you notice the < and > characters at the beginning of the lines? They indicate which file they refer to: < for the first file, > for the second.

If you have the chance to widen the window of your terminal (at least 126 columns), you may like the -y option, that will display a two column output, with the two files side by side:

diff -y [first-file] [second-file]

Output example of diff with option -y

Output example with option -y