File Merging Made Easy: Using the diff3 Command in Linux

watch 4m
views 2

10:26, 09.04.2026

Article Content
arrow

  • Understanding the diff3 Command
  • Overview of diff3 Syntax
  • Frequently Used Options
  • How to Identify File Differences on Linux
  • Combining Files with diff3 in Linux
  • Integrating Changes from Multiple Files Using diff3
  • Handling Conflicts During diff3 Merges
  • Final Thoughts

File merging can become so much easier with helpful tools such as the diff3 command. System admins and programmers who are constantly working with several versions of the file and want to speed up this process can use diff3. This command is necessary for specifying the differences in various versions of the same file.

Thanks to our practical knowledge, we decided to explain how the basics of diff3 works, and share some helpful samples so that you can clearly understand this tool and its usage case on the Linux system.

Understanding the diff3 Command

Diff3 command functions by comparing the files, determining the differences between them, and outputting this information in a fairly easy form for the user to understand.

The major usage cases for this tool are the following:

  • Merge from various files can be done automatically.
  • The diversity between the files can be easily found.
  • Dealing with conflicts that happen during the merging of various versions.

 There are also available diff and sdiff commands which can be used for the merging of versions, but the peculiarity of diff3 is that you can work with 3 various versions and can easily merge them in one specific file.

Overview of diff3 Syntax

Here is how the standard usage of the command looks like:

diff3 [options] f1 f2 f3

In this line, the f1 specifies the initial version of the file, f2 – 2nd version, and f3 – the 3rd one.

Frequently Used Options

Here is the list of the most used options for this command, some of them are the following:

  • -m: file merging that is done automatically.
  • -e: this option is needed for the ed script creation that is necessary for the applying changes.
  • -3: displays only differences between the 3 files.
  • -A: add all the available changes from the 3 files.
  • -E: the merging of the files even in case there is a specific conflict.  

How to Identify File Differences on Linux

In case, you have 3 versions of the same file and the data there is slightly different, you can easily compare these versions. Once done, you will get an output with information about each line in every version, in case a new line will be added somewhere you will see this info, or in case a specific line is changed in one of those versions, you will also get this data.

For the comparison of these files use the following command:

diff3 f1.txt f2.txt f3.txt

Once you use this command, you will get an output with some changes that were made in all 3 versions. Usually, the data will be shown for every version with such figures:

  • 1:2c: means that the first version has changes in the 2nd line and you will also see the content of this line.
  • 2:3c: means that the second version of the file has changes in the 3rd line and you will see this specific line.
  • 3:2,3c: means that version 3 has changes in lines 2 and 3.

Combining Files with diff3 in Linux

To combine all these 3 versions of one file, you can use -m option. This means a new file with all the changes will be created.

diff3 -m f1.txt f2.txt f3.txt

The output of this command will show where the are conflicts in the lines. For instance, in case all 3 versions have changes in the same line you can manually edit and save the information which is actually needed.  

Integrating Changes from Multiple Files Using diff3

To integrate the changes from all the versions and create ed script, you will need to use -e option as follows:

diff3 -e f1.txt f2.txt f3.txt > scriptfile

By using this line, you will create a file scriptfile that includes the ed script, which can be used as follows:

ed f1.txt < scriptfile

This will change the first version of the file, according to the modifications made in scriptfile, and you can check whether the changes have been made with the following command:

cat f1.txt
This will show what lines are in the f1 version so it becomes much easier to merge the files automatically.

Handling Conflicts During diff3 Merges

The conflict during merge occurs when there is a difference in the same location of the file. Such conflicts are in the output so you can check where it occurred and manually change everything. Here is a step-by-step process that should be done to handle the conflict:

  • Open the file where the conflict occurred.
  • Change the file in the needed way, you can edit/remove the lines.
  • Don’t forget to save the file.

Final Thoughts

The diff3 is a great method with the help of which you can work with various versions of the same file. You can easily detect the differences between the versions and in case there is a necessity, you can also merge all 3 versions.

Share

Was this article helpful to you?

VPS popular offers

-15.6%

CPU
CPU
3 Xeon Cores
RAM
RAM
1 GB
Space
Space
20 GB SSD
Bandwidth
Bandwidth
30 Mbps
DDoS Protected SSD-KVM 1024 Linux

38 /mo

/mo

Billed annually

sale

-20%

CPU
CPU
6 Xeon Cores
RAM
RAM
8 GB
Space
Space
200 GB HDD
Bandwidth
Bandwidth
300 Gb
KVM-HDD HK 8192 Linux

20.46 /mo

/mo

Billed semiannually

-15%

CPU
CPU
6 Xeon Cores
RAM
RAM
8 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
80 Mbps
DDoS Protected SSD-wKVM 8192 Windows

101 /mo

/mo

Billed annually

sale

-20%

CPU
CPU
4 Epyc Cores
RAM
RAM
4 GB
Space
Space
50 GB NVMe
Bandwidth
Bandwidth
Unlimited
aiKVM-NVMe 4096 Linux

16.68 /mo

/mo

Billed monthly

-16.3%

CPU
CPU
4 Xeon Cores
RAM
RAM
2 GB
Space
Space
30 GB SSD
Bandwidth
Bandwidth
40 Mbps
DDoS Protected SSD-KVM 2048 Linux

48 /mo

/mo

Billed annually

sale

-20%

CPU
CPU
3 Xeon Cores
RAM
RAM
1 GB
Space
Space
40 GB HDD
Bandwidth
Bandwidth
Unlimited
KVM-HDD 1024 Linux

6.1 /mo

/mo

Billed monthly

sale

-20%

CPU
CPU
6 Epyc Cores
RAM
RAM
16 GB
Space
Space
150 GB NVMe
Bandwidth
Bandwidth
Unlimited
wKVM-NVMe 16384 Windows

54.49 /mo

/mo

Billed monthly

sale

-20%

CPU
CPU
6 Xeon Cores
RAM
RAM
16 GB
Space
Space
150 GB SSD
Bandwidth
Bandwidth
Unlimited
KVM-SSD 16384 Linux

49.99 /mo

/mo

Billed monthly

sale

-20%

CPU
CPU
8 Epyc Cores
RAM
RAM
32 GB
Space
Space
200 GB NVMe
Bandwidth
Bandwidth
Unlimited
wKVM-NVMe 32768 Windows

74.49 /mo

/mo

Billed monthly

sale

-20%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
Unlimited
MT5 KVM 4096 Windows

19.99 /mo

/mo

Billed monthly

Other articles on this topic

cookie

Accept cookies & privacy policy?

We use cookies to ensure that we give you the best experience on our website. If you continue without changing your settings, we'll assume that you are happy to receive all cookies on the HostZealot website.