diff3 - Linux


diff3 is a command in Linux used for comparing three files line by line, highlighting the differences between them. It is primarily used to merge changes made to a file from two different sources. This command is particularly useful in version control situations, enabling the user to manually or automatically reconcile differences between multiple versions of files.


The basic syntax for the diff3 command is as follows:

diff3 [options] file1 file2 file3
  • file1, file2, and file3 are the three files you want to compare.

Common Variations:

  • Verbose Output:
    diff3 -m file1 file2 file3


  • -e, --ed: Generate an ed script capable of converting file1 to match file3.
  • -E, --show-overlap: Similar to -e, but include commands to tell about overlaps.
  • -m, --merge: Output the merged result of the three files, based on non-conflicting changes between file1 to file2 and file1 to file3.
  • -x: Like -e, but only output sections that are different in all three files.
  • -X: Like -E, but only output sections that are different in all three files.
  • -3: Only output conflicts.
  • --help: Display a help message and exit.
  • --version: Show version information.


  1. Basic Comparison:

    diff3 oldfile myfile yourfile

    This command will show differences between oldfile, myfile, and yourfile.

  2. Generating Merge Output:

    diff3 -m oldfile myfile yourfile > mergedfile

    If oldfile, myfile, and yourfile are versions of the same document, this merges non-conflicting changes into mergedfile.

  3. Extracting Conflicts:

    diff3 -3 oldfile myfile yourfile

    Outputs only the conflicting sections among the files.

Common Issues

  • Permission Denied: Ensure you have read permissions for all files involved and write permissions for your output file if redirecting output.
  • File Encoding: Differences in file encoding can cause unexpected outputs. Make sure all files use the same encoding.
  • Large File Performance: diff3 can perform slowly on large files. Consider breaking large files into smaller segments if performance is an issue.


Combine with patch to apply changes:

diff3 -m oldfile myfile yourfile | patch -p0 > newfile

This command sequence merges changes and applies them to create a newfile.

  • diff: Compare two files line by line.
  • cmp: Compare two files byte by byte.
  • patch: Apply a diff file to an original.

For further reading and more detailed information, check the official GNU documentation: GNU diffutils.