Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

How to compare three files in Linux using diff3 tool


In one of our earlier tutorials, we discussed a Linux command line utility – dubbed diff – that allows you to compare two files line by line. But what if the requirement is to compare not two, but three files? Yes, there exists a Linux command line utility for this as well, and it’s appropriately called diff3.

If you’ve been looking for such a tool, then look no further, as in this tutorial, we’ll be explaining the usage of diff3 through easy to understand examples.

But before we move forward, we would like you to know that all the examples mentioned in this tutorial have been tested on Ubuntu 14.04 with bash version 4.3.11(1), and the version of diff3 that we’ve used is 3.3.

Install diff3

The diff3 tool will likely be installed by default on your system – on Ubuntu it is, at-least. But if that’s not the case, then don’t worry as you can easily do it by installing the GNU Diffutils package. For instructions on how to download and install the package on your system, head here.

Diff3 Usage

As the diff3 utility compares three files, so for our usage examples, we’ll have to take three files that we’ll pass as input to the tool.

Here’s the file1:

This is line1 with some change
This is line2
This is line3

Here’s the file2:

This is line1
This is line2
This is line3

And here’s the file3:

This is line1
This is line2
This is line3

Clearly, if we see the content of these three files, the only change is in the first line of the first file. So now let’s see how diff3 reports this change.

Here’s how we can use diff3 in this case:

diff3 file1 file2 file3

And here’s the output it produces:

====1
1:1c
 This is line1 with some change
2:1c
3:1c
 This is line1

You can comprehend the output in the following way:

  • The first line that starts with four ‘=’ signs and ends with number ‘1’ says the change is in the first file.
  • In the next line, the first ‘1’ means file1; the ‘1c’ after colon ‘means first line contains’. The actual line is displayed in the next line of the output.
  • Similarly, 2:1c and 3:1c mean ‘the first line of file2 and file3 contain’. And as content of the line is same in both files, the actual line is displayed only once, at the end of the output.

To make things more clear, let’s take another case where the change is in file3.

Here’s the file1:

This is line1
This is line2
This is line3

Here’s the file2:

This is line1
This is line2
This is line3

And here’s the file3:

This is line1 with some change
This is line2
This is line3

The command remains the same:

diff3 file1 file2 file3

And here’s the output:

====3
1:1c
2:1c
 This is line1
3:1c
 This is line1 with some change

So the output in this case can be comprehended as:

  • The change is in file3
  • Both file1 and file2 contain “This is line1” as their first line
  • While the file3 contains “This is line1 with some changes” as its first line.

That was all about how diff3 reports change in lines. Now let’s see how diff3 behaves if one of the files contains an extra line:

While the file1 and file2 remain same, here’s the file3:

This is line1
This is line2
This is line3
This is line4

As clear from its contents, file3 now contains an extra line at the end.

Let’s run the same diff3 command:

diff3 file1 file2 file3

And here is the output the command produces:

====3
1:3a
2:3a
3:4c
 This is line4

The output begins with saying that the change is in the file3. The change is in the fourth line of file3 (3:4c), which is a new line altogether when compared to file1 and file2.

What we discussed so far were some straightforward cases. Let’s take a couple of slightly complex situations. For example, consider file1, file2, and file3 as containing the following:

This is line1
This is line2 with some change
This is line3
This is line1
This is line2
This is line3
This is line1
This is line2
This is line3 with some change

So now both file1 and file2 contain changes, and that too in different lines. Let’s see what diff3 produces in this case:

====
1:2,3c
 This is line2 with some change
 This is line3
2:2,3c
 This is line2
 This is line3
3:2,3c
 This is line2
 This is line3 with some change

As you can see in the output shown above, the first line doesn’t contain any number. That’s because not one, but multiple files contain changes now. For diff3, the line 2 and 3 of all the three files are now different. That’s the reason why instead of a single number after colon (:), the output now contains a range – ‘2,3’, which means line 2 to line 3.

Let’s take one more example. Suppose this time the three files contain the following information:

This is line1
This is line2
This is line3
This is line4
This is line1
This is line2
This is line3
This is line1
This is line2
This is line3
This is line4
This is line5

Now when the diff command is run, it produces the following output:

====
1:4c
 This is line4
2:3a
3:4,5c
 This is line4
 This is line5

Here, if you try to understand, the diff3 command says that the changes are after line 3 in all the three files. While file1 contains the line “This is line4” as its fourth line, file3 contains “This is line4” and “This is line5” as its fourth and fifth lines, respectively. The file2, on the other hand, only has 3 lines.

So that was all about the basic working on diff3. For what it’s worth, here’ what the official documentation says about the tool: “You can use the diff3 command to show differences among three files. When two people have made independent changes to a common original, diff3 can report the differences between the original and the two changed versions, and can produce a merged file that contains both persons’ changes together with warnings about conflicts.”.

Conclusion

We’ve just scratched the surface here, Diff3 is capable of doing much more. For example, as the official documentation also points out, you can use it to merge changes as well. Plus there are many command line options that let you customize the command’s output as well as access some other features it offers. It’s all there on the man page, do go through it.

Comments

comments