How to Use the colordiff Command in Linux Terminal to View Colorized and Side-by-Side File Diffs

The standard UNIX diff utility has been a cornerstone of file comparison and code review across Linux distributions for decades. However, comparing large configuration scripts, software source code, or system log files using standard diff produces monochrome terminal output where additions, deletions, and line modifications blend together. When reviewing intricate multi-hundred-line files, missing a leading plus (+) or minus (-) character can lead to oversights during critical troubleshooting.

To eliminate this visual fatigue, Linux administrators use colordiff. Designed as a lightweight, drop-in replacement wrapper for diff, colordiff parses comparison outputs and injects ANSI colour sequences into the terminal stream. Deletions appear in vivid red, additions in crisp green, and header metadata in bold magenta. This guide details how to install colordiff across major Linux distributions, use side-by-side comparisons, customise colour palettes, and integrate it seamlessly with shell pipelines and version control systems.

Installing colordiff on Major Linux Distributions

Because colordiff is written in Perl and relies on standard core utilities, it is packaged natively in nearly all Linux distribution repositories:

  • Ubuntu, Debian, and Linux Mint:
    sudo apt update && sudo apt install colordiff
  • Fedora, RHEL, and Rocky Linux:
    sudo dnf install colordiff
  • Arch Linux and Manjaro:
    sudo pacman -S colordiff
  • openSUSE:
    sudo zypper install colordiff

Basic Usage: Comparing Files with Colorized Output

Because colordiff supports identical command-line flags and arguments as standard GNU diff, you can use it immediately without learning new syntax:

Unified Comparison (Default)

To perform a standard unified comparison between an original configuration and a revised edition, use the -u flag:

colordiff -u config.orig.conf config.new.conf

In the terminal output:

  • File paths and hunk header lines (e.g., @@ -1,5 +1,6 @@) are highlighted in magenta or cyan.
  • Lines removed from the original file are prefixed with - and displayed in bold red.
  • Lines added to the new file are prefixed with + and displayed in vibrant green.

Side-by-Side Comparison

When comparing tabular configuration files or dense code routines, side-by-side diffs make differences immediately apparent. Pass the -y (side-by-side) and -W (width) parameters:

colordiff -y -W 150 config.orig.conf config.new.conf

The terminal splits into two synchronized columns displaying the original file on the left and the new file on the right. Matching lines remain uncoloured, while modified lines are highlighted with coloured separator pipes (|), additions with >, and deletions with <.

Piping Command Output Through colordiff

One of colordiff‘s greatest strengths is its ability to operate as a filter in standard UNIX pipelines. You can pipe the output of any tool that outputs diff-formatted text directly into colordiff:

Highlighting Git Diffs

If you are working on a machine where Git pager colours are unconfigured, you can pipe raw diff streams directly into colordiff:

git diff | colordiff

Highlighting Patch Files and Remote SSH Comparisons

To inspect a downloaded patch or compare a local file against a remote server file over SSH:

diff -u local.conf <(ssh user@remote "cat /etc/app.conf") | colordiff

Viewing Long Diffs with less Paging

When a comparison spans hundreds of lines, the terminal output will quickly scroll past your buffer. Because standard less strips ANSI colour codes by default, pipe the output to less with the -R (raw control characters) flag:

colordiff -u file1.txt file2.txt | less -R

This allows you to navigate forward and backward using standard less keybindings (j, k, /search) while preserving full terminal syntax colouring.

Customising the colordiff Colour Palette

If you use a light terminal theme or have specific visual accessibility preferences, you can customise the exact colours assigned to diff elements.

  1. Copy the system-wide configuration template into your user home directory:
    cp /etc/colordiffrc ~/.colordiffrc
  2. Open the configuration file in a terminal editor:
    nano ~/.colordiffrc
  3. Locate the colour mapping directives and adjust them to your preferred ANSI colours:
    # Available colours: white, yellow, green, blue, cyan, magenta, black, red
    plain=white
    file_old=magenta
    file_new=cyan
    diff_stuff=yellow
    diff_old=red
    diff_new=green
  4. Save and exit the file (Ctrl + O, then Ctrl + X). Future colordiff invocations will automatically honour your personal colour scheme.

Creating a Permanent Shell Alias

To automatically use colordiff whenever you type diff in your terminal sessions, add an alias to your shell configuration:

  1. Open your shell rc file (e.g., ~/.bashrc or ~/.zshrc):
    nano ~/.bashrc
  2. Append the following line to the bottom of the file:
    alias diff='colordiff'
  3. Reload your shell environment:
    source ~/.bashrc

Adopting colordiff in your Linux terminal workflow streamlines code reviews, prevents deployment mistakes, and makes terminal-based file comparisons intuitive and effortless.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.