How to colorize the output of commands on Linux

Diff is a command line utility that can help users visually compare the differences between 2 files line by line on Linux/Unix systems. The Colordiff utility is a Perl script, which is an improved version of diff. It colorizes the output so that the differences between files are clearly visible on the terminal.

In short, the diff command tool can be used to perform the following operations

1. View the changes that exist between the two versions of the file.

2. Compare between two program files

In this tutorial, you will learn how to use the colordiff command to color diff output on Linux.

How to install Colordiff tool

Before starting to use the ins and outs of d colordiff, let’s install it first.

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 following 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 Arch Linux derivatives (such as Manjaro), please run:

$ sudo pacman -S colordiff

Install Colordiff on FreeBSD and OpenBSD

To install Colordiff on FreeBSD, run the following command:

$ sudo pkg install  colordiff

For OpenBSD, execute:

$ pkg_add -v colordiff

How to color difference

The syntax for using the colordiff and diff commands is very simple and clear.

Use grammar

$  color diff file1 file2

For example, I created 2 files as shown

Create two files to compare using colordiff

To check the differences between the two files, run the following command:

$  colordiff file1.txt file2.txt

Use colordiff command to compare two files

Alternatively, you can use the diff command and pipe the output to colordiff as follows:

Either

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

diff command coloring

Alternatives to Different Colors

Another convenient way you can use to compare files is to use grc The command is as follows:

$ grc diff file1.txt file2.txt

Combine GRC and diff

Another great tool I found is Edith This is an improved color difference.

You can install Icdiff using the pip command, run:

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

in conclusion

In this tutorial, you learned how to color diff output with the help of the colordiff command line utility to compare files on the terminal in color format. If the two files are the same, no output will be output on the screen. To get more information on how to use these two utilities, just visit man diff and man colordiff.

Leave us a message and let us know your experience. Thank you.

Sidebar