diff Command: Colorize Output On The Unix / Linux Command Line

Posted on in Categories Command Line Hacks, Programming, Web Developer last updated November 26, 2012

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

  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.

Say hello to colordiff

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.


First, turn on the EPEL repo and type the following yum command to install colordiff utility:
# yum install colordiff
Sample outputs:

Loaded plugins: product-id, protectbase, rhnplugin
0 packages excluded due to repository protections
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package colordiff.noarch 0:1.0.9-3.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
 Package            Arch            Version                Repository     Size
 colordiff          noarch          1.0.9-3.el6            epel           23 k
Transaction Summary
Install       1 Package(s)
Total download size: 23 k
Installed size: 43 k
Is this ok [y/N]: y
Downloading Packages:
colordiff-1.0.9-3.el6.noarch.rpm                        |  23 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : colordiff-1.0.9-3.el6.noarch                                1/1 
Installed products updated.
  Verifying  : colordiff-1.0.9-3.el6.noarch                                1/1 
  colordiff.noarch 0:1.0.9-3.el6                                               

Debian / Ubuntu / Mint Linux users type the following apt-get command to install colordiff utility:
$ sudo apt-get install colordiff
Sample outputs:

[sudo] password for vivek: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 11 not upgraded.
Need to get 14.4 kB of archives.
After this operation, 65.5 kB of additional disk space will be used.
Get:1 http://debian.osuosl.org/debian/ squeeze/main colordiff all 1.0.9-1 [14.4 kB]
Fetched 14.4 kB in 1s (12.8 kB/s)                     
Selecting previously deselected package colordiff.
(Reading database ... 281091 files and directories currently installed.)
Unpacking colordiff (from .../colordiff_1.0.9-1_all.deb) ...
Processing triggers for man-db ...
Setting up colordiff (1.0.9-1) ...

How do I use colordiff command?

The syntax is:
colordiff file1 file2
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:

Fig.01: colordiff in action
Fig.01: colordiff 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

Use the following syntax to read man page for information about colordiff and diff command:
man diff
man colordiff

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 6 comments/add one below):

6 comment

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

Comments are closed.