How to diff and merge files or directories on Linux

There are many cases where you want to compare and/or merge two files or directories. For example, you may want to compare two distinct backup snapshots; merge two different versions of a document; diff two configuration files for troubleshooting, etc. While version control systems can handle this kind of situations easily, it is probably an overkill for average Linux users who are looking for simple diff and merge solution.

In this tutorial, I describe how to diff and merge files and directories on Linux by using a GUI tool called Meld.

Meld is a visual diff and merge tool that can compare files, directories, and any version controlled projects. Meld offers two- and three-way diff and merge, and supports simple commands of major version control systems (e.g., SVN, Git, Mercurial, Bazaar).

Install Meld on Linux

To install Meld on Debian, Ubuntu or Linux Mint:

$ sudo apt-get install meld

To install Meld on CentOS or RHEL, first enable Repoforge repository, and then run:

$ sudo yum install meld

To install Meld on Fedora, simply run:

$ sudo yum install meld

After installation, you can start Meld as follows.

$ meld

To start a comparison, click on the comparison icon at the top. Then you can choose between file/directory comparison or version control browser modes like the following.

Compare and Merge Two Files

You can visually compare the difference between two files.

You can merge individual changes selectively in either direction, or apply all changes in one direction.

Three-Way File Comparison

You can compare three different files or directories side-by-side.

Compare and Merge Two Directories

You can compare two or three directories and apply the diff (i.e., copy or delete files) between them.

Customize Meld Filtering Rules

You can customize Meld so that it can filter out uninteresting differences. The filtering rule can be defined for files as well as directories by regular expressions as shown below.

Nautilus Integration

Meld provides Nautilus integration, so you can diff and merge files or directories directly in Nautilus file manager. To install Nautilus plugin for Meld on Ubuntu desktop, run the following commands.

$ sudo apt-get install meld nautilus-compare
$ nautilus -q

Once you have installed Nautilus plugin for Meld, you will see an additional context menu called "Compare" when you right-click two files. The following screenshots show how Meld works on Nautilus.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.
Your name can also be listed here. Write for us as a freelancer.

9 thoughts on “How to diff and merge files or directories on Linux

  1. Hmm the visual features look pretty boss. I think I'll stick with vimdiff though. I love stuff I can use without leaving the terminal.

  2. Another way to diff and merge is via diff command and using patch command to merge the changes. Vimdiff also can be used to view the differences in graphical mode.

Leave a comment

Your email address will not be published. Required fields are marked *

Current ye@r *