File Merging Made Easy: Using the diff3 Command in Linux
10:26, 09.04.2026
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.