5+ “diff3” Command Usage Examples in Linux

«
»

This tutorial explains Linux “diff3” command, options and its usage with examples.

diff3 – Compares three files.

DESCRIPTION

advertisement

The diff3 command compares three files and writes to standard output the ranges of text that differ, flagged with the following codes:

====   All three files differ.
====1   File1 differs.
====2   File2 differs.
====3   File3 differs.

The type of change needed to convert a given range of a given file to match another file is indicated in one of these two ways in the output:

File:Number1 -> Text is to be added after line number Number1 in File, where File is 1, 2, or 3.
File:Number1[,Number2]c -> Text in the range line Number1 to line Number2 is to be changed. If Number1 is the same as Number2, the range may be abbreviated to Number1.

advertisement
advertisement

The contents of the range follows a “c” indication. When the contents of two files are identical, the diff3 command does not show the contents of the lower-numbered file, although it shows the location of the identical lines for each.

Note: Edit scripts produced by the -e flag cannot create lines consisting of a . (period).

SYNOPSIS

diff3 [ -e | -x | -E | -X | -3 ] File1 File2 File3

advertisement

OPTIONS :

-3
Produces an edit script to incorporate only changes flagged ====3.
-E, -X
These are similar to -e and -x respectively, but treat overlapping changes (that is, changes that would be flagged ==== in the normal listing) differently. The overlapping lines from both files are inserted by the edit script, bracketed by <<<<<< and >>>>>> lines. The -E option is used by Revision Control System (RCS) Merge to ensure that overlapping changes in the merged files are preserved and brought to someone’s attention.
-e
Creates an edit script for use with the ed command to incorporate into File1 all changes between File2 and File3 (that is, the changes that normally would be flagged ==== and ====3).
-x
Produces an edit script to incorporate only changes flagged ====.

EXAMPLES

1. If fruit.a, fruit.b, and fruit.c contain the following data

fruit.a fruit.b fruit.c
banana apple grape
grape banana grapefruit
kiwi grapefruit kiwi
lemon kiwi lemon
mango orange mango
orange peach orange
peach pear peach
pare

advertisement

To list the differences among three files:

$ diff3 fruit.a fruit.b fruit.c
====            All three files are different.
1:1,2c          Lines 1 and 2 of the first file, fruit.a
  banana
  grape
2:1,3c          Lines 1 through 3 of fruit.b
  apple
  banana
  grapefruit
3:1,2c          Lines 1 and 2 of fruit.c
  grape
  grapefruit
====2           The second file, fruit.b, is different.
1:4,5c          Lines 4 and 5 the same in fruit.a and fruit.c.
2:4a            To make fruit.b look same, add after line 4.
3:4,5c
  lemon
  mango
====            The first file, fruit.a, is different.
1:8c
  pare
2:7c            fruit.b line 7 and fruit.c line 8 are the same
  pear
3:7a

2. If two out of three files are similar

For this example, assume that the files mine.txt and parent.txt exactly same.

$ cat parent.txt
Hello
This is a file.
 
$ cat your.txt
Hello,
This is your file.
 
$ cat mine.txt
Hello
This is a file.
$ diff3 mine.txt parent.txt your.txt
====3
1:1,2c
2:1,2c
  Hello
  This is a file.
3:1,2c
  Hello,
  This is your file.

The very first line ‘====3′ signifies that this time the file number 3 ie your.txt is different from the other two. This is visible in the output too.

advertisement

3. Output overlapping changes using -x option

$ diff3 -x mine.txt parent.txt your.txt

The flag -x can be used to output the changes that cause overlaps. The order of file names given as argument to diff3 matters here. This command means that you want to merge in mine.txt the changes that would turn parent.txt into your.txt

Lets take an example :

$ cat mine.txt
Hi,
This is your file.
hello
 
$ cat parent.txt
Hi,
This is your file.
 
$ cat your.txt
Hi,
This is your file.
bye

Execute diff3 now:

$ diff3 -x mine.txt parent.txt your.txt
3c
bye
.

So we see that the output says that the third line of mine.txt has to changed to ‘bye’ in order to merge the changes that would turn parent.txt to your.txt

4. Output non merged changes using -e option

The option -e can be used to output non merged changes from parent.txt to your.txt into mine.txt.
Considering the same files as used the example above :

$ diff3 -e mine.txt parent.txt your.txt
3c
bye
.
.

5. Output merged file using -m option

A merged file comprising of contents from all the three files can be produced using the -m option. For example, if following are the three files:

$ cat mine.txt
Hi,
This is your file.
Hello
 
$ cat parent.txt
Hi,
This is your file.
 
$ cat your.txt
Hi,
This is your file.

Now if diff3 is run using the -m option :

$ diff3 -m mine.txt parent.txt your.txt
Hi,
This is your file.
Hello

So the above output shows contents of merged file that has merged contents from all the three files supplied as arguments to diff3.

6. Understanding conflicts using -m option

Producing a merged file (from the three files) is not always an easy task (as shown in the above example).

Consider the input file contents below :

$ cat parent.txt
Hi,
This is your file.
hi
 
$ cat your.txt
Hi,
This is your file.
hello
 
$ cat mine.txt
Hi,
This is your file.
Hello

If diff3 is run over the files above, the following output is displayed :

$ diff3 -m mine.txt parent.txt your.txt
Hi,
This is your file.
<<<<<<< mine.txt
Hello
||||||| parent.txt
hi
=======
hello
>>>>>>> your.txt

Here the first two lines are undisputed part of the merged file but the actual conflict is shown between <<<<<<< and >>>>>>> along with file names and contents.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.