Quick Tip

How to Colorize Diff Output for Better View

Get a better look at the diff output with the colored output. The colorized diff makes it easier to distinguish the changes.

Abhishek Prakash
Abhishek Prakash

Table of Contents

Diff is the go-to command when you want to see the difference between two files in Linux command line.

Understanding the diff command output could be complicated if you are new. Highlighting the changes in different color could help you notice the changes easily.

You can colorize the diff output with the --color option in the following manner:

diff --color file1.txt file2.txt

This will print the output of diff color in different colors based on the colors of your terminal palette.

Here's an example:

Permanently use colored diff output

Typing --color every time you run the diff command is tiresome. If you want to always see the colorized diff command output, you can easily use alias in Linux.

alias diff='diff --color'

To make this alias permanent, add the above alias command to your ~/.bashrc file (assuming that you are using bash command).

If you are feeling lazy, you can use the echo command to append the alias to the end of the .bashrc file:

echo "alias diff='diff --color' " >> ~/.bashrc

However, you should use a terminal-based text editor like Vim or Nano to edit the file and add all the custom alias at one place.

Once you have added the alias to the ~/.bashrc file, use the source command to bring the changes into effect immediately:

source ~/.bashrc

Don't have --color option with diff? Here's what you can do!

The --color option was included in the diff command in version 3.4. If you see an error message using thi option with diff command, you should check the diff command version first:

diff --version

It should show an output like this:

[email protected]:~$ diff --version
diff (GNU diffutils) 3.7
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Eggert, Mike Haertel, David Hayes,
Richard Stallman, and Len Tower.

If you have diff command version 3.3 or older, you'll have to resort to other ways to get colored output with diff.

You can use a handy utility called colordiff. It is essentially a wrapper for the diff command written in Perl.

You should find colordiff in your Linux distribution's package manager. On Debian/Ubuntu, you can use the following command to install it:

sudo apt install colordiff

Once installed, you can use it the same way you use the diff command.

colordiff file1.txt file2.txt

Not that difficult, not that different. Isn't it?

There is a handy utility called ydiff that lets you view the difference between the file in a side-by-side view.

I hope you find this quick little Linux tip helpful. Stay tuned and stay subscribed for more.

Join the conversation.