Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux diff3 command

Linux diff3 command

Linux Command Reference Linux Command Reference


What is the diff3 command

diff3 is a utility in Linux for comparing the differences among three files. It can analyze three different versions of a file, find the points where they differ, and display those differences clearly.

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


Basic syntax



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

Parameter Description

  1. My file: The version containing your changes
  2. Original file: The base version before changes
  3. Their file: The version modified by someone else

Commonly used options

Option Description
-a Treat all files as text files when comparing
-A Output all conflicts, showing differences with conflict markers
-e Generate an editing script suitable for the ed editor
-E Similar to -A, but show conflicts with fewer markers
-3 Show only the parts where all three files differ
-x Similar to -3, but with a different display format
-m Merge the output, marking conflicting parts
-L label Output using specified labels instead of file names

Output format explanation

The output of diff3 contains the following key parts:

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

Practical Application Examples

Example 1: Basic comparison

Suppose there are three files:

Example

diff3

my_version.txt original.txt their_version.txt

The output might 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

Merged output marks the conflicting parts, making it easier to resolve conflicts manually.


Typical Use Case

Scenario 1: Resolving version conflicts

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

  1. What the original content was
  2. What you changed
  3. What someone else changed

Scenario 2: Automated merging

combined with -A or -E option, you can generate a file containing merge markers for further processing:

Example

diff3

-A

my.txt original.txt their.txt

merged.txt

Scenario 3: Generating an editing script

Use -e the option can generate an ed editor script for automated modifications:

Example

diff3

-e

my.txt original.txt their.txt

edit_script.ed


Practical Exercise

Exercise 1: Create test files

Create the original file base.txt:

Example

echo

"original content"

base.txt

Create your modified version my.txt:

Example

echo

"my changes"

my.txt

Create the other person's modified version their.txt:

Example

echo

"someone else's changes"

their.txt

Exercise 2: Perform the 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 the conflict markers.


Frequently Asked Questions

Q1: What is the difference between diff3 and diff?

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

Q2: How to resolve conflicts shown by diff3?

Conflicting parts are clearly marked, and you can:

  1. Edit the file manually and choose the appropriate changes
  2. Use the merge feature of a version control tool (such as git)

Q3: Can diff3 compare binary files?

By default, diff3 assumes the input is text. Using -a the option can force a binary comparison, but the result may be hard to understand.


Advanced Tips

  1. Integration with version control: In version control systems such as Git, the diff3 format is often used as the display format for merge conflicts
  2. Custom labels: When using -L option assigns a more meaningful label to 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

"their version"

my.txt base.txt their.txt


Through this article, you should have mastered the core usage of the diff3 command. This tool is very useful in collaborative and version-merging scenarios, helping you clearly understand the differences between versions and thus resolve conflicts more effectively.

Linux Command Reference Linux Command Reference