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



See the changes between one version of a file. Compare two configuration or program files. 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



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



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% 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



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:



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



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.