diff Command Colorize Output On the Unix / Linux Command Line

The diff command compares files line by line and displays a list of changes between two files. You can use diff command to:

ADVERTISEMENTS

  1. See the changes between one version of a file.
  2. Compare two configuration or program files.
  3. Create a patch file which can be applied with the Linux / Unix program patch.

diff Command Colorize Output On the Unix / Linux Command Line

colordiff is a wrapper for diff and produces the same output as diff but with coloured syntax highlighting at the command line to improve readability. colordiff has been tested on various flavours of Linux and under OpenBSD, but should be broadly portable to other systems.

How to install colordiff

The colordiff command packaged for many Linux distro, UNIX-like systems and *BSD distributions and other operating systems. Let us see how to install colordiff command.

Installing colordiff on a CentOS/RHEL

First, turn on the EPEL repo on RHEL6/CentOS6 (see how to turn on EPEL repo on a CentOS/RHEL 7 here) and type the following yum command to install colordiff utility:
# yum install colordiff
Install colordiff on a CentOS RHEL Oracle Linux

Fedora Linux install colordiff

Type the following dnf command:
$ sudo dnf install colordiff

Debian/Ubuntu/Mint Linux install colordiff to colorize output of diff

Debian / Ubuntu / Mint Linux users type the following apt command or apt-get command utility:
$ sudo apt-get install colordiff
Install colordiff on a Debian Ubuntu Mint Linux with apt-get

OpenBSD install colordiff

Simply type the pkg_add command:
# pkg_add -v colordiff
Sample outputs:

Update candidates: quirks-2.414 -> quirks-2.414
quirks-2.414 signed on 2018-03-28T14:24:37Z
colordiff-1.0.18: ok
Extracted 31057 from 31305

FreeBSD install colordiff

Run the pkg command as follows:
$ sudo pkg install colordiff
Sample outputs:

Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 1 package(s) will be affected (of 0 checked):
 
New packages to be INSTALLED:
	colordiff: 1.0.18
 
Number of packages to be installed: 1
 
14 KiB to be downloaded.
 
Proceed with this action? [y/N]: y
[rsnapshot] [1/1] Fetching colordiff-1.0.18.txz: 100%   14 KiB  14.7kB/s    00:01    
Checking integrity... done (0 conflicting)
[rsnapshot] [1/1] Installing colordiff-1.0.18...
[rsnapshot] [1/1] Extracting colordiff-1.0.18: 100%

macOS install colordiff

Use the brew command:
$ brew install colordiff
macos Unix install colordiff command using brew

How do I use colordiff command?

The syntax is:
colordiff file1 file2
OR
diff -u file1 file2 | colordiff
You can pipe the output to less command, using the -R or -r option which keeps the color escape sequences, otherwise displayed incorrectly or discarded by less:
diff -u file1 file2 | colordiff | less -R
Sample outputs:

diff Command Colorize Output On the Unix / Linux

colordiff command in action

Alternatives to colordiff command

Use remark command as follows:
diff file1 file2 | remark /usr/share/regex-markup/diff
You can also use the grc command:
grc diff file1 file2
GNU/Linux user can use the following syntax to colorize the output without install colordiff:
diff --color file1 file2
diff --color resolv.conf resolv.conf.saved

gnu linux diff with --color option
Another option is to use the vimdiff command to edit two or more versions of a file with Vim and show differences:
vimdiff file1 file2
viff resolv.conf resolv.conf.saved

Conclusion

You just learned how to use diff command to colorize output on the Unix / Linux, macOS and *BSD like systems. Use the following syntax to read man page for information about colordiff and diff command:
man diff
man colordiff

You can grab the source code of colordiff here.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
6 comments… add one
  • Jalal Hajigholamali Nov 27, 2012 @ 6:46

    Hi,

    Thanks a lot, simple and useful command…

  • Mac Nov 27, 2012 @ 13:18

    Can someone explain the ‘grc’ command what it does I am not finding it to install or the man page?

  • dan sam Dec 14, 2012 @ 13:55

    also meld ¡great app!

    sudo apt-get install meld (ubuntu)

  • Reina Dec 6, 2013 @ 19:34

    Piling the diff to a file then opening with vim can also give color if you have good vim syntax.

  • hannibalyang Dec 14, 2016 @ 4:01

    Can you share how u highlight the command in the terminal??
    I have try many ways, but all of them failed…..

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.