How to Diff Command Colorize Output on Linux

diff colorize

Diff is a command-line utility that helps users to visually compare differences between 2 files line by line on a Linux/Unix system. Colordiff utility is a Perl script that is an improved version of diff. It colorizes the output so that the differences between the files are clearly visible on the terminal.

In a nutshell, the diff command tool can be used to perform the following

1. View changes that exist between 2 versions of a file.

2. Draw comparisons between 2 program files

In this tutorial, you will learn how to colorize diff output on Linux using the help of colordiff commands.

How to install Colordiff  utility

Before we get to the ins and outs of using d colordiff, let's first install it.

Install Colordiff on Ubuntu/Debian/Mint

To install the colordiff utility on Debian-based systems such as Ubuntu and Mint, open your terminal and run the command:

$ sudo apt install colordiff

Install Colordiff on RHEL/CentOS

To install colordiff on rpm distributions such as Hat and CentOS, run:

$ sudo yum install colordiff

Install Colordiff on Arch Linux

For Arch Linux and derivatives of Arch Linux such as Manjaro, run:

$ sudo pacman -S colordiff

Install Colordiff on FreeBSD and OpenBSD

To install  Colordiff  on FreeBSD, run the command:

$ sudo pkg install  colordiff

For OpenBSD execute:

$ pkg_add -v colordiff

How to diff colorize

The syntax for using the colordiff and diff commands is quite simple and straightforward.

Use the syntax

$  color diff file1 file2

For example,  I have created 2 files as shown

create two files to compare using colordiff

To check the differences that exist between the two files, run the command:

$  colordiff file1.txt file2.txt

compare two files using colordiff command

Additionally, you can use the diff command and pipe the output to colordiff as shown:

or

$ diff -u file1.txt  file2.txt | colordiff

diff command colorized

Alternatives to diff color

Another handy way you can use to compare files is by use of the grc command as shown:

$ grc diff file1.txt file2.txt

Combine grc with diff

Another cool tool I found is Icdiff which is an improved colored diff.

You can install Icdiff using pip command, run:

$ pip install git+https://github.com/jeffkaufman/icdiff.git

Conclusion

In this tutorial, you learned how to colorize diff output with help of colordiff command-line utilities to compare files on the terminal in a colorized format. If the two files are identical, then no output is printed on the screen. To get more information about how those two utilities are used, simply visit the man diff and man colordiff.

Give us a shout and let us know how your experience went. Thank you.

Jamie Arthur 12:50 am

Comments

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

2 Comments

  1. I tried to install it on debian 8 Jessie but it’s throwing dependency package issue.
    Does it require any dependency package ?