Meld compares files and directories

Mix and Match


Your system abounds with plain text files - everything from password files, initialization scripts, configurations, and logfiles, to shell startup files and scripts. We'll show you how to manage these files with Meld.

By Martin Streicher

Maxim Malevich, Fotolia

For all its sophistication, Linux relies almost exclusively on plain text files for configuration. At first blush, the pervasive use of text files might seem an anachronism. However, "everything is a file" remains true to the original precepts of Unix. Moreover, the express lack of encoding simplifies administration. A text file can be edited in virtually any application; a plain text file is readily transmitted over common protocols and compresses to a fraction of its original size; and a text file is easily archived in a version control system, such as Git and Subversion. For example, you can keep configuration files - from the system-wide mysql.cnf to your personal .profile - in version control to track modifications and mitigate loss in the event of a wholesale system failure - assuming your repository is kept on a separate, dedicated machine.

Also, it is easy to compare, or "diff," two or more text files to find variances. Software developers often use the diff utility to appraise changes in source code. diff and its counterpart patch help developers merge work. But diff is generally useful. Whether you're modifying an INI file or editing a document, diff can quickly point to what's changed from one version to another.

Alternatively, you can compare three files at a time with the aptly named diff3. However, diff has one drawback: The output can be difficult to interpret, even with context (the -c option), which adds additional lines before and after each difference to show the difference in situ.

A better option is a graphical diff tool that shows file versions side by side. Many commercial editors feature a visual diff, but you do not need to spend a dime to enjoy the same benefits on Linux. Meld [1] is an open source package to compare and merge files visually. It can operate on two or three files at a time, compare entire directories, and compare a local, working copy of a file with a version in source control. The tool also supports a number of character sets, including French, Russian, and Japanese.

Figure 1 shows a screenshot of Meld comparing two versions of a Python source code file. One edition is shown at left, the other at right. A phrase in one version is correlated with its corresponding phrase in the alternate version with a blue band. The meters in the right and left margins of the window indicate where to find differences, with each variation colored blue and each addition colored green.

Figure 1: The Meld comparison and merge tool.

To reveal details, align the thumb of the scrollbar with a meter. Also, you can use the up and down arrows in the toolbar to jump to the previous and next variation, respectively. The thin black arrows at the center of the window patch code. If you click on a right-facing arrow, the code at right is patched to match the code at left, and vice versa for a left-facing arrow. If you apply a patch, the display immediately refreshes to reflect the new state.

Installing Meld

The latest version of Meld is 1.3.1, released August 2009. Meld is written in Python, and its user interface is based on Glade and Gtk+. If your system already runs Gnome or has its libraries, installation is simple: Download a tarball, unpack it, and go.

$ cd ~/src
$ wget http://ftp.gnome.org/pub/gnome/sources/meld/1.3/meld-1.3.1.tar.gz
$ tar xzf meld-1.3.1.tar.gz
$ cd meld-1.3.1
$ ./meld

If you don't have Gnome, but use Debian or Ubuntu, you can install Meld and all its dependencies with apt-get. Just make sure your sources are up to date and run apt-get install meld. On a new installation of Ubuntu 9.10, adding Meld and its prerequisites installed 38 new packages (Listing 1).

Listing 1: Installing Meld
01 $ sudo apt-get update
02 $ sudo apt-get --install-recommends --yes install meld
03 Reading package lists... Done
04 Building dependency tree
05 Reading state information... Done
06 The following extra packages will be installed:
07   doc-base gnome-doc-utils gnome-media gnome-media-common libecal1.2-7
08   libevdocument1 libevview1 libfreezethaw-perl libgda3-3 libgda3-bin
09   libgda3-common libgdl-1-0 libgdl-1-common libgnome-desktop-2-11
10   libgnome-media0 libgnomecups1.0-1 libgnomeprint2.2-0
11   libgnomeprint2.2-data libgnomeprintui2.2-0 libgnomeprintui2.2-common
12   libgtksourceview-common libgtksourceview1.0-0 libgtkspell0
13   libmetacity0 libmldbm-perl libnautilus-burn4 libpanel-applet2-0
14   librarian0 libtotem-plparser12 libuuid-perl metacity-common
15   python-gnome2 python-gnome2-desktop python-gnome2-extras
16   python-gnomecanvas xsltproc yelp
17 Suggested packages:
18   libgda3-mysql libgda3-postgres libgda3-odbc libgda3-sqlite
19   python-gnome2-desktop-doc python-gnome2-extras-doc
20   python-gnome2-extras-dbg python-gnomecanvas-dbg
21 The following NEW packages will be installed:
22   doc-base gnome-doc-utils gnome-media gnome-media-common libecal1.2-7
23   libevdocument1 libevview1 libfreezethaw-perl libgda3-3 libgda3-bin
24   libgda3-common libgdl-1-0 libgdl-1-common libgnome-desktop-2-11
25   libgnome-media0 libgnomecups1.0-1 libgnomeprint2.2-0
26   libgnomeprint2.2-data libgnomeprintui2.2-0 libgnomeprintui2.2-common
27   libgtksourceview-common libgtksourceview1.0-0 libgtkspell0
28   libmetacity0 libmldbm-perl libnautilus-burn4 libpanel-applet2-0
29   librarian0 libtotem-plparser12 libuuid-perl meld metacity-common
30   python-gnome2 python-gnome2-desktop python-gnome2-extras
31   python-gnomecanvas xsltproc yelp
32 0 upgraded, 38 newly installed, 0 to remove and 1 not upgraded.
33 Need to get 6115kB of archives.
34 After this operation, 54.9MB of additional disk space will be used.
35 ...

Minding Meld

When you are ready to launch Meld, you have a variety of choices.

Figure 2 shows an example comparing three versions of a Ruby source file. The files are small, with only a single line different. The first (from left to right) variation can be applied to the middle column and vice versa by clicking the appropriate arrow in the gutter between the two. The second set of arrows affects the middle and third variation. If you want to apply changes from the rightmost edition to the leftmost, change the order of the command-line arguments or use the Browse buttons to place a specific file in a specific column. When you apply a change, the modified file indicates it is "dirty," or unsaved, with a small disk icon to the left of the file name. If you apply a change inadvertently, choose Edit | Undo or press Ctrl+Z to reverse the action.

Figure 2: An example three-way file comparison.

Comparing a single file visually is immensely easier than interpreting diff output. But Meld really shines when you must compare a number of files or an entire hierarchy of folders and files. Meld compares two or three trees, pointing out new entries, missing entries, and modified entries. Additionally, you can "merge" trees by clicking a button to copy a file from one tree to another. If you often collaborate with others, you will find this feature invaluable. In Figure 3 you can see a comparison of two directories.

A file that differs is shown in bold red. A new file is shown in green, and a missing file is shown in gray decorated with a strikethrough. Toggling the buttons Same, New, and Modified hides and shows unchanged, new, and altered files, respectively. To see a detailed comparison of a modified file, choose the file and click the Compare icon.

Much like reading the diffs between two files, interpreting the comparison of two trees depends on the order of the operands. In Figure 3, the original tree is shown at left, the new tree at right. Thus, the content of the file Amber differs; Dusk and Eggplant are missing in the new tree; and Umber, Violet, Watermelon, and Yellow have been added to the new tree.

If you swap the order (or, reading from right to left, treating the right as the original and the left as new), Amber remains altered, Dusk and Eggplant have been added, but Umber, Violet, Watermelon, and Yellow are missing. Suffice it to say that the order of the operands is arbitrary, and interpretation depends on the task at hand. Keep in mind what was original and what is revised and what you intend to do.

Tree comparison omits the left and right arrows between the panes in the center of the window. Instead, you can use the Left, Right, and Delete buttons to copy a file from the right to the left, copy a file from left to right, and delete a file, respectively.

Figure 3: Meld can compare two or three trees of files.

Meld and Version Control

When files are kept under version control, the version of the file in the repository is considered canonical and all other copies are local or "working" versions. In a normal workflow, when an edit is complete, the working version is archived to the repository to become the new canonical edition. A typical comparison measures the local copy against the repository. Meld supports a number of version control systems, including Git, Subversion, and Mercurial. If you open a working version of code, Meld scans and compares each file against its repository and generates a working list of differences. Figure 4 shows two files that differ from the Git repository. Again, Meld contains useful buttons to progress through each revision, reconcile differences, and ultimately commit changes back to the repository. The Flatten button hides the complexity of the hierarchy, showing all modified files in a simple list. Modified is a toggle to hide and show altered files; Normal shows unaltered files. The Non VC button shows files that are not yet under version control.

Version control is as essential as backups, and Meld streamlines comparisons and merges. Meld is also an excellent tool to start with if you're adopting a new version control system for the first time. You are using version control, right? With Meld, it's a snap.

Figure 4: Meld can compare working files against a repository and supports many source control systems.
INFO
[1] Meld: http://meld.sourceforge.net