Programmers, web developers and even writers often need to compare different versions of the same code or text to keep track of changes. An efficient way to do this is with a diff tool – an application that compares files or folders, shows differences between them and helps you create a new file by merging or patching its versions. Generally, you can compare files on Linux either with a command-line tool or using a standalone application.
Diff, colordiff and wdiff
The command-line utility called
diff is a part of the “diffutils” package present in the repositories of every major Linux distribution. It’s a simple tool that compares files line by line and can also compare files in two folders. If selected files are identical, diff won’t print any output, and you can tweak its options (for example, to ignore white space or letter case).
The general syntax is
The results can be shown in two columns with
--side-by-side option. Get the full list of diff options by typing:
The “diffutils” package also contains
diff3 – a tool that can compare three files and merge three versions of a file. A great guide to making the most of diff can be found on the official website.
Colordiff is technically a “wrapper” or a script that modifies the output of diff by coloring it according to your preferences in the configuration file (“/etc/colordiffrc” or “/home/user/.colordiffrc”). The colordiff package is available in the repositories of most distributions.
Colordiff has the same options as diff, and you can use it either directly by typing:
or by piping the output of diff to it:
Another similar and a bit more specialized tool is
wdiff which can compare files word per word (it defines a word as anything surrounded by white space). Wdiff can ignore case (
-i), provide statistics (
-s) about changes in given files, and emphasize differences with formatting (
-p). You can also combine colordiff and wdiff to get a color-coded overview of word differences between files:
Note that you have to enter the full path if files are not in the active folder. To find out more about wdiff, visit the official website.
Diff modes in Vim and Emacs
Popular text editors Vim and Emacs both have a built-in diff mode. To activate it in Vim, you can start the program with the
vimdiff command and add up to four files to compare:
Vim opens each file in a separate window and highlights the differences. You can configure how to split each window for a better overview.
Emacs users can choose between a diff extension (called “major mode”) or Ediff, a slightly more advanced mode that lets you interactively change compared files. Similarly to Vim, Emacs can show files in separate windows, side by side, and highlight the differences. You can also use it to merge files, and it works with compressed files, too.
Both text editors have relatively complex syntax that requires some learning, but it pays off since they’re very powerful and thoroughly customizable.
Kdiff3 is a graphical frontend to diff, meaning that it relies on diff and offers all its options. It lets you compare two files to a base file, and merge, split and join entire folders, files or just selected parts of text. Kdiff3 integrates well with KDE (supports Dolphin service menus), but it will work on all Linux distributions, Windows and Mac OS X. The “Settings -> Configure Kdiff3” dialog lets you set parameters for file comparison in folders (by date, size, binary comparison, or full contents) and between files (ignore case, numbers and white space). You can toggle word wrap and line numbers for better overview and adjust color settings to mark the differences.
The great thing about Kdiff3 is that it lets you paste text from any file directly into the main window, eliminating the need for creating files when you just want to compare text “on-the-fly.” A detailed Kdiff3 handbook is available online.
At first sight it seems simple, but Diffuse is as equally powerful as other tools on this list. It can compare an unlimited number of files in a side-by-side view and summarize the differences. You can edit files directly from Diffuse and perform merges and line matching. It also supports (limited) syntax highlighting and offers integration with several version control systems – including Git, Mercurial and Subversion – to help you keep files in sync. Diffuse works on Linux, Windows and Mac OS X, and you can download the installation packages from the project website.
This application works on Linux, Windows and OS X, integrates well with Windows Explorer, and offers installer packages for some distributions. You can edit files within Diffmerge, automatically merge file versions and export differences to a new file. It can compare up to three files and two folders and show if the files are identical, which can be useful when checking if an rsync backup was performed correctly.
The settings dialog lets you define custom rules for handling various file types. Diffmerge is free, but it will occasionally ask for a paid registration key. This might turn away some users; luckily, they can choose another tool from this list since they all offer more or less the same options.
Meld packs features from other diff tools into a Python-built interface. You can download the source and the Windows version, and find the package in the repositories if you’re a Ubuntu, Fedora or SUSE user. A cool thing about Meld is that you don’t have to install it – just run the executable and use it as a portable app.
Meld supports file editing, filtering with regular expressions, comparing and merging three files, and comparing two or three folders and their contents, and also provides helpful visualization of differences using arrows and color-coding. If you use a version control system like Git, Mercurial or SVN, Meld can manage your files, check for changes and perform commits and updates. Meld is regularly updated, so we can hope for new features in future versions.
If you don’t feel like installing a new application for this purpose and you don’t need advanced file comparison features, you can try some of the many online diff tools that are free and only require a web browser.
Do you use a diff tool that’s not on this list? Tell us about it in the comments.
Image credit: Teaser image source