The diff3 command in Linux is used to compare three files line by line. It is mainly used when you want to identify differences between three versions of the same file, such as during file merging, conflict resolution, or version control tasks. Internally, diff3 uses the diff command to perform comparisons and present the differences in a structured format.
- Compares three files simultaneously
- Identifies which file differs from the others
- Helps resolve conflicts during file merges
- Can generate merged output or editor scripts
Example 1: Comparing Three Files
This example shows how diff3 compares three text files and reports which file differs. It helps users understand the basic output format of the command.
Command:
diff3 a.txt b.txt c.txtOutput:

- ====3: Indicates file 3 (c.txt) is different
- 3c: Change is required at line 3
- grapes: Content of the differing line in c.txt
Note: In the output, you may see these markers:
====: All three files are different at this line
====1: Only file 1 differs
====2: Only file 2 differs
====3: Only file 3 differs
This helps you quickly identify which file contains unique changes.
Syntax
diff3 [options] file1 file2 file3- diff3: The command used to compare three files.
- [OPTIONS]: Optional flags that control how differences are displayed or how output is generated (such as merged output or editor scripts).
- file1 file2 file3 : Files to be compared
- Files are compared line by line
Notes: All three files must exist unless one file is provided through standard input (-).
The order of files matters because diff3 reports which specific file differs.
By default, diff3 prints differences directly to the terminal.
Options Available in diff3 Command
The diff3 command provides several options that control how differences are displayed, how files are merged, and how conflicts are handled. Below are the most commonly used and meaningful options, explained with simple examples.
1. -m: Prozduce Merged Output
The -m option combines the contents of all three files and produces a merged output. When conflicts occur, diff3 inserts conflict markers so the user can manually resolve them. This option is commonly used during file merging.
Example: Merge Three Files and Show Conflicts
This example shows how diff3 merges three files and highlights conflicting lines using markers. It helps users understand how merge conflicts are represented.
Command:
diff3 -m a.txt b.txt c.txtOutput:

2. -e: Generate Editor Script (Non-Merged Changes)
The -e option outputs changes in the form of an editor script. Instead of merging files, it generates commands that can be applied to update one file based on differences.
Example: Generate Editor Commands for Changes
This example shows how diff3 outputs editing instructions rather than merged content. It is useful for automated or script-based editing.
Command:
diff3 -e a.txt b.txt c.txtOutput:

- 3c: Change required at line 3
- grapes: New content to replace the line
- .: End of change block
3. -x: Show Only Complete Conflicts
The -x option displays only those differences where all three files differ. It ignores cases where only one file is different.
Example: Display Only Full Conflicts
This example shows how diff3 filters output to display only major conflicts involving all three files. It helps reduce noise in complex comparisons.
Command:
diff3 -x a.txt b.txt c.txt- Shows differences only when all three files are different
- Partial matches are ignored
- Useful when reviewing large merge conflicts
Output:

4. -X: Detect Overlapping Conflicts
The -X option works like -x but also detects overlapping line ranges in conflicts. This provides more detailed conflict information.
Example: Highlight Overlapping Changes
This example helps identify conflicts that overlap across multiple lines, making it easier to understand complex edits.
Command:
diff3 -X a.txt b.txt c.txt- Highlights overlapping conflict ranges
- Makes conflict areas more visible
- Useful in long or heavily edited files
Output:

5. -a: Treat All Files as Text
The -a option forces diff3 to treat all input files as text files, even if they are binary or non-text.
Example: Compare Non-Text Files as Text
This example shows how diff3 attempts to compare non-text files by treating them as text. It is mainly useful for inspection or debugging.
Command:
diff3 -a file1.odt file2.odt file3.odt- diff3 will treat the .odt files as text.
Output:

6. -: Read File from Standard Input
Using - allows one of the files to be provided via standard input (stdin) instead of an actual file.
Example: Provide Third File Using stdin
This example shows how diff3 accepts file content from the terminal. It is useful in pipelines or dynamic input scenarios.
Command:
diff3 a.txt b.txt -Input
apple
banana
grapes
Ctrl + DOutput:

- - represents standard input
- Ctrl + D ends input
- diff3 compares stdin content as the third file
Real-World Use Cases of diff3 Command
The diff3 command is mainly used in situations where multiple versions of the same file exist and differences must be identified or resolved. Below are some practical and easy-to-understand real-world scenarios where diff3 is commonly used.
1. Resolve Merge Conflicts in Version Control
When two developers modify the same file differently, version control systems may create conflicts. The diff3 command helps compare the original file with the two modified versions to understand where conflicts occur.
Command:
diff3 -m original.txt user1.txt user2.txt- original.txt: Base version
- user1.txt, user2.txt: Modified versions
- -m produces merged output with conflict markers
- Helps manually resolve conflicts before committing changes
Note: This is similar to how Git internally handles three-way merges.
2: Compare Configuration Files from Different Systems
System administrators often maintain similar configuration files across multiple servers. diff3 helps identify which system’s configuration differs.
Command:
diff3 config_server1.conf config_server2.conf config_backup.conf- Compares three configuration files at once
- Quickly shows which file has changed
- Helps maintain consistency across systems
3: Verify File Changes After Manual Edits
If a file is edited multiple times manually and backups are kept, diff3 helps determine what changed and where.
Command:
diff3 report_old.txt report_edit1.txt report_edit2.txt- Identifies differences between multiple edit versions
- Prevents accidental data loss
- Useful for reviewing document revisions
4: Use diff3 in Scripts for Automated Comparison
The diff3 command can be used in shell scripts to automatically detect conflicts or differences.
Command:
diff3 -s file1.txt file2.txt file3.txt- -s suppresses output
- Exit status can be checked in scripts
- Useful for automation and CI pipelines
5: Compare Files When One Version Comes from a Pipe
Sometimes file content is generated dynamically. diff3 allows one file to be read from standard input.
Command:
cat new_version.txt | diff3 base.txt old_version.txt -- - represents stdin
- Useful in pipelines
- Avoids creating temporary files