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

by on November 26, 2012 · 5 comments· LAST UPDATED November 26, 2012

in Command Line Hacks, Programming, Web Developer

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.

Installation

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
===============================================================================
Installing:
 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
 
Installed:
  colordiff.noarch 0:1.0.9-3.el6
 
Complete!
 

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:
  colordiff
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
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:

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

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!
  • Jalal Hajigholamali

    Hi,

    Thanks a lot, simple and useful command…

  • Mac

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

  • http://www.cyberciti.biz/tips/about-us nixCraft

    Try:

    sudo apg-get install grc
    man grc

    Go here and grab the source code. Read the README.txt.

  • dan sam

    also meld ¡great app!

    sudo apt-get install meld (ubuntu)

  • Reina

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

Previous post:

Next post: