How to Compare and Merge Text Files on Linux

On this page

  1. Diffuse
  2. LibreOffice

Comparing text files requires time, effort, a large monitor, and a good amount of patience, or simply just the right tools to do it without any of the aforementioned. If you are an editor that has to deal with multiple versions of a text file or a developer that wants to check the various code revisions of a file, then the following tutorial is for you.

Diffuse

Diffuse is a handy little text comparison and merging app that was built with the developer in mind. It features an easy-to-use graphical interface that positions the two text files side by side. The key element of this software tool is its highlighting abilities that automatically detect the differences between the two files and highlight them. Moreover, Diffuse can detect the format of the file and utilize another set of highlighting methods. For example, in the following picture, you can see two different versions of an XML file. Diffuse is showing the different “parts” with orange, as well as the exact points of difference with red.

You may change the format through the “Format” options menu located on the top panel. The “Merge” options menu allows for a set of merging or copying options from left to right or the other way around. For easier and quicker browsing of the differences, especially in longer files you may use the ctrl+up arrow or ctrl+down arrow hotkeys that correspond to “view previous difference” and “view next difference” respectively.

There are many text codecs supported (all that I know of), and you can even set up a local repository directory through the “Edit/Preferences/Version Control” settings menu to allow Diffuse to import files from your Git, CVS, Bazaar or Mercurial folder. You can of course import simple text files as well by pressing the “open a file” button located on the top left of each document area.

While this is perfectly doable with Diffuse, I would suggest that editors use LibreOffice comparison features when they deal with written content files.

LibreOffice

The first thing you need to do is to open the first version of the file with LibreOffice. Then you go to the top panel and choose the “Edit/Compare Document” option. This will open up a file manager dialog that will allow you to choose the updated version of the file and upon its selection, you will be given the differences and a control dialog.

You may choose every change entry from the control window and the corresponding line will be highlighted on the document.

For even better control, you can use the “Filter” tab. This allows you to show edits from a particular editor, choose specific actions (insertion, deletion, format changes, table changes), or even specify a certain time period and only show changes that happened during this period. This is especially helpful for chief editors who want to check the results of collaborative editing work.

If you are searching for a command line tool, check out sdiff.

Here is part 2 of this article that covers Meld and Vim: https://www.howtoforge.com/tutorial/compare-merge-text-file-linux-part-2/

Share this page:

Suggested articles

10 Comment(s)

Add comment

Comments

By: Drag at: 2015-08-03 14:30:18

Meld is also a good tool to compare files :)

By: renfri at: 2015-08-03 18:00:19

My favourite tool is kompare -> http://www.caffeinated.me.uk/kompare/

By: IVS at: 2015-08-03 18:20:48

kdiff3, xxdiff, and vimdiff are probably more popular than the above. I'd usually opt for the kdiff3. As the previous poster mentioned meld is also an option, but like diffuse, it depends on python. kdiff3/xxdiff depend on Qt, and are written in C++. For real development, I found merging in meld to be less reliable than kdiff3 and xxdiff, although meld is nicer looking, and has syntax highlighting. In kdiff3, they deliberately choose not to implement syntax highlighting, apparently due to a belief that it isn't a good thing in a merge tool for clarity. I'd personally disagree, but otherwise it would be my favourite of these options.

By: Don Morse at: 2015-08-03 19:56:33

I use Meld.  simple click and you can sync the files.

By: ElectricPrism at: 2015-08-03 23:02:52

Or you could use Meld, haven't used Diffuse - but maybe I will.

By: Erik at: 2015-08-04 08:11:36

Libreoffice? Come on… It's nice to know, but this is for an entirely different task.

This "article" sorely misses the best tools for the job: vim, emacs, kdiff3, xxdiff, meld.

I usually use vimdiff, or vim with the DirDiff plug-in, or kdiff3. I found meld less capable than others, but the UI is well designed. xxdiff would be good, but its UI is terrible.

By: Ademeion at: 2015-08-05 03:51:48

I'm also a Meld user. It's easy to use and has all the features I have ever needed.

By: Doug at: 2015-08-05 12:24:36

Or even Beyond Compare. 

By: Dave at: 2015-08-05 12:55:57

Can't believe nobody has mentioned grep/diff. Sad but true, sometimes these gui apps just don't have the power for the heavy lifters, hopefully oneday!

By: PJ at: 2015-08-13 09:20:00

Here is a handy incantation for comparing file differences between two nearly-identical directories, image1 and image2:"For scrutiny, the command:

  vimdiff <(cd image1; find . | sort) <(cd image2; find . | sort)

run from /cm/images/ shows the changed files for image directories image1 and image2, with uninteresting parts folded away."    (Bright Cluster Manager Administrator Manual)

Plain old diff and grep just isn't the right tool for this sort of result.