Open In App

diff3 command in Linux with examples

Improve
Improve
Like Article
Like
Save
Share
Report

diff3 command is used to compare the three files line by line. It internally uses the diff command to compare. When three files are compared then the following output may come which have their own meaning:

  • ==== : It means all the files are different.
  • ====1 : File 1 is different.
  • ====2 : File 2 is different.
  • ====3 : File 3 is different.

Syntax:

diff3 [OPTIONS]

Example: Let us consider three files as a.txt, b.txt and c.txt.

Now if diff3 command is used over these three files then following output will come:

Here, the very first line ‘====3’ tells that the file number 3 i.e. c.txt is different from the other two. This is visible in the output too.

Options:

  • ‘c’ signifies change while ‘a’ signifies append : ‘c’ signifies ‘change’ required in the lines/text. If the alphabet ‘a’ appears then it signifies a line needs to be appended.

    Example:

    Now if diff3 command is used over these three files :

    The output above signifies that the change in file3 (i.e c.txt) needs to be appended in file1 (a.txt) and file2 (b.txt) to make all the three file similar. Alternatively, the second line in b.txt can be changed(c) to make all the three files similar.

  • -x : It produces an editor script of only those changes where all three files differ.

    Example:

    Apply diff3 -x

    The output says that the third line of a.txt has to changed to ‘mahi’ in order to merge the changes that would turn b.txt to c.txt.

  • diff3 -X : It is similar to -x except that it also checks for overlapping ranges of lines in the changes. diff3 highlights these ranges in the output.

    Example:

    Apply diff3 -X

  • diff3 -e : It can be used to output non merged changes from b.txt to c.txt into a.txt.

    Example:

    Apply diff3 -e

    So we see that the output shows the non merged changes.

  • diff3 -E : It also checks for overlapping ranges of lines in the changes. diff3 highlights these ranges in the output.

    Example:

    Apply diff3 -E

  • diff3 -m : A merged file comprising of contents from all the three files can be produced using the -m option.

    Example:

    Applying diff3 -m

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

  • diff3 ‘-‘ : Accept one argument from stdin using ‘-‘. A ‘-‘ can be used in place of any one of the file names to accept input from stdin for that argument.

    Example:

    Output:

    As we can see, when the command above was run, the content of third file was given through stdin. Note that after entering the contents of file from stdin, one needs to press the key combination ctrl+d to get the output of diff3.

  • diff3 -a : To compare even non text files, an option -a can be used that tells diff3 to consider all input files as text files.

    Example:

    So the output above shows that diff3 tried to compare even an odt file treating it as a text file.


Last Updated : 15 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads