Compare files in Linux with these tools
Whether you’re a programmer, a creative professional, or someone who just wants to browse the web, there are times when you find yourself finding the differences between files.
There are two main tools you can use to compare files in Linux:
- difference: A command line utility that comes pre-installed on most Linux systems. The diff command has a learning curve.
- merge: A graphical tool you can install to compare files and directories. It is easier to use, especially for desktop users.
But there are several other tools with different features to compare files. Here let me mention some useful GUI and CLI tools to check the differences between files and folders.
To note: The tools are not listed in any particular order. Choose what you find best for you.
1. Diff Command
Diff means difference (obviously!) and is used to find the difference between two files by scanning them line by line. It is a basic UNIX utility, developed in the 70s.
Diff will show you the lines that need to be changed in the compared files to make them the same.
Main features of Diff:
- Uses symbols and special characters to indicate the lines to modify to make the two files identical.
- Go line by line to provide the best possible result.
And, the best part is that diff comes pre-installed in every Linux distribution.
As you can see in the screenshot above, it is not easy to understand the output of the diff command at first try. Do not worry. We have a detailed guide on using the diff command it’s up to you to explore.
2. Colordiff Command
For some reason, if you find the Diff utility a bit bland in terms of colors, you can use color difference which is a modified version of diff command utility with enhanced color and highlighting.
- Syntax highlighting with attractive colors.
- Improved readability over the Diff utility.
- Licensed under GPL and has digitally signed source code.
color difference is available in the default repository of almost all popular Linux distributions and if you are using a Debian derivative you can enter the following:
sudo apt install colordiff
3. Wdiff Command
Wdiff is the CLI for the Diff utility, and it has a different approach to comparing files, i.e. it analyzes on a word for word.
It starts by creating two temporary files and will run
Diff above them. Finally, it collects the output of you encounter word mismatches between two files.
Main features of Wdiff:
- Support multiple languages.
- Ability to add colorized output by integrating with Colordiff.
Wdiff is available in the default repository for Debian derivatives and other distributions. For Ubuntu-based distributions, use the following command to install it:
sudo apt install wdiff
4. Vimdiff Command
Main features of Vimdiff:
- Ability to export results to an HTML web page.
- Can also be used with Git.
- Personalization (of course).
- Ability to use it as a CLI and GUI tool.
This is one of the most powerful features you get with the Vim editor. Whether you are using Vim in your terminal or the GUI version, you can use the vimdiff ordered.
Vimdiff works in a more advanced way than the usual diff utility. To begin with, when you enter
vimdiff command, it starts the vim editor with your usual diff. However, if you know how to move around Vim and its commands, you can perform various tasks at the same time.
To use Vimdiff, you must have Vim installed on your system. We also have a tutorial on how to install the latest Vim on Ubuntu.
You can use the command below to install it (if you’re not worried about which version you’re installing):
sudo apt install vim
5. Gitdiff Command
As its name suggests, this utility works on a Git repository.
This command will use the
diff command we talked about earlier and will run on git datasources. It can be anything from commits and branches to files and more.
Gitdiff key features:
- Ability to determine changes between multiple git data sources.
- Can also be used with binary files.
- Supports highlighting with colors.
Gitdiff does not require any separate installation unless Git is not installed on your system. And if you’re looking for the latest version, we have a tutorial on how to install the latest version of Git on Ubuntu.
Or, you can simply follow the given command to install Git on your Ubuntu-based distribution:
sudo apt install git
Are you looking for a graphical tool that not only differentiates files, but also allows you to create and apply patches to them?
Then Kompare by KDE will be an interesting choice!
Mainly it is used to display source files to compare and merge. But, you can get creative with it!
Komapare can be used on multiple files and directories and supports multiple Diff formats.
Main features of Kompare:
- Provides statistics on the differences found between compared files.
- The Bézier-based connection widget displays the source and destination of files.
- Source and destination can also be changed with commands.
- Easy to navigate user interface.
- Allows you to create and apply patches.
- Support for various Diff formats.
- Appearance can be customized to some extent.
Being part of the KDE family, Kompare can be easily found on popular Linux distributions default repository and software center. But, if you prefer the command line, here is the command:
sudo apt install kompare
Tools like Kompare can overwhelm new users as they offer a plethora of features, but if you’re looking for simplicity, Meld is a good choice.
Meld provides up to three-way comparison for files and directories and has built-in support for version control systems. You can also refer to a detailed guide on how to compare files using Meld to learn more about this.
Main features of Meld:
- Supports up to 3 file comparisons.
- Syntax highlighting.
- Support for version control systems.
- Simple text filtering.
- Minimal and easy to understand user interface.
Meld is popular software and can be found easily on the default repository of almost all Linux distributions. And for installation on Ubuntu, you can use this command:
sudo apt install meld
Additional: Sublime Merge (non-FOSS)
From the developers of the famous Sublime text editor, Sublime Merge is aimed at programmers who constantly deal with version control systems, especially Git, because having the best workflow with Git is its main goal.
From command line integration, powerful search and flexibility to Git flow integration, everything that powers your workflow comes with it.
Like Sublime Text, Sublime Merge is also not open source. Likewise, it is also free but encourages you to purchase a license for continued use. However, you can continue to use it without buying the license forever.
What is your choice ?
There are a few other tools like Sublime Merge. P4Merge and Beyond Compare come to mind. They are not open source software but they are available for the Linux platform.
In my opinion, the diff command and the Meld tools are sufficient for most of your file comparison needs. Specific scenarios like managing Git could benefit from specialized tools like GitDiff.
What do you find best for your use case? Share your thoughts in the comments below.