linux lexicon comparing files in linuxIf you thought learning to work with files was interesting, wait till you read what we have in store for you today. Did you know that it was possible to compare two files in Linux using the Terminal? No? Well, let’s find out!

Linux is like a Pandora’s box of amazing stuff! Just when you would begin to think there is nothing else left to learn, some new command or concept always pops up.

Continuing with our tradition of sharing delightful stuff with our readers in the Linux Lexicon series, today, I am going to guide you on how to go about comparing files in Linux using the diff, sdiff, and vimdiff commands.

Though definitely not an instinctive way to do so (but then hardly anything in Linux can be termed as being instinctive), this method of comparing files will come in real handy once you get used to it.

Comparing Files using the diff command

The diff command is used for comparing files line-by-line and printing the lines that are different. The format for the diff command is:

diff file1 file2

I am going to explain how the diff command works through an example. Suppose we have two files “file1.txt” and “file2.txt” whose contents are as follows:

Comparing Files 0-content_of_files

Now let us use the diff command to display the difference between these files:

Comparing Files 1-diff_command_output1

The diff command’s output is aimed at helping you make the first file similar to the second file. It contains the following things:

(i) A number corresponding to a line in the first file (3 in this case)
(ii) A letter symbolizing the action which needs to be taken ( c in this case)
(iii)A number corresponding to a line in the second file (3 in this case)

The following symbols are used in the output of the diff command:
a: add
c: change
d: delete
<: indicates Line from the first file
>: indicates Line from the second file
: work as separators of line1 from line2

So, 3c3, in this case, means that the ‘third line in the first file needs to be changed in order to match the third line in the second file’.

Here are a few other examples:

Comparing Files 2-diff_command_output2

Here 5d4 means that the ‘fifth line from the first file needs to be deleted so that both files match up at line 4’.

Comparing Files 3-diff_command_output3

The diff command can also indicate if changes are required in multiple lines as shown in the example above. Here the output means that the ‘lines 3 to 4 from file1 need to be changed to match line 3 to 4 of the second file’.

Check out the man page of the diff command for even more information and associated options.

Comparing Files using the sdiff command

Files can also be compared using the sdiff command. The sdiff command provides a side by side comparison of the two files and uses the ‘|‘ symbol as a marker to indicate differing lines. The format for it is as follow:

sdiff file1 file2

Here is how it works:

Comparing Files 4-sdiff_command_output

As you can see that the sdiff command merges the two files and outputs the result. This is a bit more convenient as compared to interpreting the output produced by the diff command.

Also Read: 20 Quirky Things You Didn’t Know The Linux Terminal Could Do

Comparing File using the vimdiff command

Another method of comparing files is using the vimdiff command. This command uses the vim editor and displays the files in separate windows. The content of the files is displayed in these windows and the differences are highlighted appropriately. Here is the format of using the vimdiff command:

vimdiff file1 file2 [file3 file4]

The vimdiff command can be used to compare up to 4 files simultaneously as opposed to the diff and the sdiff command which can only be used to compare 2 files at once.

We will be discussing the vim editor in full detail in an article on ‘Text Editors in Linux’. But for now, knowing the following commands shall enable you to use the vimdiff command efficiently:

Ctrl-w+w- Use this key combination to toggle between windows
:q– Use this command to quit/close the current window
:qa- Use this command to quit/close all windows
:qa!- Use this command to forcefully quit/close all windows

Here is an example of how the vimdiff command works:

Comparing Files 5-vimdiff_command_output1 Comparing Files 6-vimdiff_command_output2

Opting for a graphical approach of comparing files by opening the files in separate windows and then comparing each line one by one is quite time-consuming and unnecessary. Now that you know how to compare files using the Terminal, you would have your work cut out for you easily. Linux is absolutely amazing, right?

Read Our Whole Linux Lexicon Tutorial Series Here

Got any doubts, queries of your own, or any suggestions on the topics that you would like us to cover? Drop them in the comments below.

Now Watch: