diff3

file managementLinux/Unix
The diff3 command is one of the most frequently used commands in Linux/Unix-like operating systems. diff3 Compare three files line by line

Quick Reference

Command Name:

diff3

Category:

file management

Platform:

Linux/Unix

Basic Usage:

diff3 mine.txt original.txt yours.txt

Common Use Cases

  • 1

    Three-way file comparison

    Compare three files to identify differences and common elements

  • 2

    Conflict resolution

    Resolve merge conflicts when two people modify the same file

  • 3

    File merging

    Merge changes from multiple sources into a single file

  • 4

    Version control

    Compare original, modified, and third-party versions of a file

Syntax

diff3 [OPTION]... MYFILE OLDFILE YOURFILE

Options

Option Description
-A, --show-all Show all changes, bracketing conflicts
-e, --ed Output an ed script to incorporate all changes
-E, --show-overlap Show conflicts with simpler bracketing
-m, --merge Output merged file with conflicts bracketed
-x, --overlap-only Show only overlapping changes
-3, --easy-only Show only non-overlapping changes
-L, --label=LABEL Use LABEL instead of filename in output headers
-T, --initial-tab Line up tabs properly in output
--help Display help information
--version Display version information

Examples

How to Use These Examples

The examples below show common ways to use the diff3 command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

diff3 my-file original-file your-file
Compare three files and show differences.
diff3 -m my-file original-file your-file
Merge the files and output the result with conflict markers.

Advanced Examples:

diff3 -A my-file original-file your-file
Show differences in a more concise format.
diff3 -e my-file original-file your-file > ed-script Generate an ed script to incorporate changes from my-file to your-file. diff3 -E my-file original-file your-file Create a more readable version of conflicts. diff3 -L "Mine" -L "Original" -L "Yours" my-file original-file your-file Use custom labels in the output. diff3 -m my-file original-file your-file > merged-file Merge files and save the result to a new file.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

The diff3 command is specialized for three-way file comparison, which is particularly useful in version control scenarios and conflict resolution. The standard order of files in the command is: diff3 MINE ORIGINAL YOURS In this context: - MINE: Your version of the file (with your changes) - ORIGINAL: The common ancestor file - YOURS: The other version of the file (with someone else's changes) This order is important, especially when generating merge scripts or resolving conflicts. Merge Output Format: When using the -m option, conflicts are bracketed like this: <<<<<<< mine your content ||||||| original original content ======= their content >>>>>>> theirs The -E option provides a simpler conflict format without the original version. Ed Script Generation: The -e option creates an ed script that can be used to incorporate changes from the first file to the third file. Common Use Cases: - Resolving merge conflicts in version control systems - Comparing different versions of configuration files - Merging changes from multiple contributors - Reviewing document edits from different reviewers Tips: - Use -L options to provide meaningful labels for each file - Use -A for a comprehensive view of all changes - Use -T to properly align tabs in the output - The merge output can be piped to a new file to save the results Relationship to diff: While diff compares two files, diff3 extends this to three files, making it ideal for scenarios where you have an original file and two different modified versions.

Tips & Tricks

1

Use the -m option to merge the files

2

Use the -E option to ignore changes made in either file1 or file2

3

Use the -A option to treat all files as text

4

Use the -i option to ignore case differences

5

Use the -x pattern option to exclude files matching a pattern

Common Use Cases

Three-way file comparison

Compare three files to identify differences and common elements

Conflict resolution

Resolve merge conflicts when two people modify the same file

File merging

Merge changes from multiple sources into a single file

Version control

Compare original, modified, and third-party versions of a file

Collaborative editing

Reconcile different edits to the same document

Related Commands

These commands are frequently used alongside diff3 or serve similar purposes:

Use Cases

1

Three-way file comparison

Compare three files to identify differences and common elements

2

Conflict resolution

Resolve merge conflicts when two people modify the same file

3

File merging

Merge changes from multiple sources into a single file

4

Version control

Compare original, modified, and third-party versions of a file

5

Collaborative editing

Reconcile different edits to the same document

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the diff3 command works in different scenarios.

$ diff3
View All Commands