Linux diff3 Command

Linux 命令大全Linux Command Reference


What is the diff3 command?

diff3 is a utility in the Linux system used to compare differences among three files. It can analyze three different versions of files, find the differences between them, and display these differences in a clear manner.

Unlike the standard diff command (which can only compare two files), diff3 is specifically designed for comparing three files, and is especially suitable for the following situations:

  • Three-way merging in version control systems
  • Difference analysis after multiple people collaboratively edit the same file
  • Compare the original file, your modifications, and others' modifications

Basic syntax format

diff3 [选项] 我的文件 原始文件 他人文件

Parameter description

  1. My file: Contains the version you modified
  2. Original file: The base version before modification
  3. Other person's file: The version modified by someone else

Common option parameters

Option Description
-a Compare all files as text files
-A Output all conflicts, displaying differences with conflict markers
-e Generate an edit script suitable for the ed editor
-E Similar to-A, but shows conflicts with fewer markers
-3 Only show parts where all three files differ
-x Similar to-3, but the display format is different
-m Merge output results, marking conflict sections
-L label Output using specified labels instead of file names

Output format explanation

The output of diff3 contains the following key parts:

  1. **====**: Separates different comparison sections
  2. 1:n: Represents line n of the first file
  3. 2:n: Represents line n of the second file (original file)
  4. 3:n: Represents line n of the third file
  5. a: Add (append)
  6. c: Change

Practical application examples

Example 1: Basic comparison

Suppose there are three files:

  • original.txt (original file)
  • my_version.txt (my modification)
  • their_version.txt (others' modification)

Example

diff3 my_version.txt original.txt their_version.txt

The output may look like:

====1
1:1,2c
  这是我的修改
2:1,2c
  这是原始内容
3:1,2c
  这是他人的修改

Example 2: Merged output

Example

diff3 -m my_version.txt original.txt their_version.txt

The merged output marks conflict sections, making it easier to resolve conflicts manually.


Typical use cases

Scenario 1: Version conflict resolution

When multiple people modify the same file, using diff3 lets you clearly see:

  1. What the original content is
  2. What you modified
  3. What others modified

Scenario 2: Automated merging

Combined with-Aor-Eoption, you can generate a file containing merge markers for subsequent processing:

Example

diff3 -A my.txt original.txt their.txt > merged.txt

Scenario 3: Generating edit scripts

Using-eoption can generate an ed editor script to achieve automated modification:

Example

diff3 -e my.txt original.txt their.txt > edit_script.ed

Hands-on exercises

Exercise 1: Create test files

Create the original filebase.txt:

Example

echo "Original content" > base.txt

Create your modified versionmy.txt:

Example

echo "My modification" > my.txt

Create someone else's modified versiontheir.txt:

Example

echo "Others' modification" > their.txt

Exercise 2: Perform comparison

Example

diff3 my.txt base.txt their.txt

Observe the output and understand the meaning of each part.

Exercise 3: Try merging

Example

diff3 -m my.txt base.txt their.txt > merged.txt
cat merged.txt

View the merged file content and understand conflict markers.


FAQ

Q1: What is the difference between diff3 and diff?

diff can only compare two files, while diff3 is specifically designed to compare three files, making it particularly suitable for resolving merge conflicts.

Q2: How to resolve conflicts displayed by diff3?

Conflict sections are clearly marked, and you can:

  1. Manually edit the file and choose the appropriate modification
  2. Use the merge functionality of version control tools (such as git)

Q3: Can diff3 compare binary files?

By default, diff3 treats input as text files. Using-aoption can force comparison of binary files, but the result may not be easy to understand.


Advanced tips

  1. Combine with version control: In version control systems like Git, the diff3 format is often used as the display format for merge conflicts
  2. Custom labels: Use-Loption to specify more meaningful labels for each file
  3. Script automation: Redirect diff3 output to a file, then process the result with a script

Example

diff3 -L "My version" -L "Original version" -L "Others' version" my.txt base.txt their.txt

After studying this article, you should have mastered the core usage of the diff3 command. This tool is very useful in scenarios involving multi-person collaboration and version merging. It helps you clearly understand the differences between versions, allowing you to resolve conflicts more effectively.

Linux 命令大全Linux Command Reference

Other extensions