How to Color diff Output?

In Linux, the  “diff(difference)” command compares the files line by line and provides instructions like add, delete, and change to make them identical. However, the simple “diff” command does not provide a syntax highlighting feature that improves the readability of the output. For this purpose, the “–color” flag of the “diff” utility is beneficial in the form of the “diff –color” command. It displays the colorized output for better visual repression as compared to the “diff” command.

This guide lists possible ways to get the output of the diff command: 

Sample Files:

A sample file named “File.txt” is taken whose content is displayed in the terminal with the “cat” command followed by “-n(numbering)” flag:

$ cat -n File1.txt

Another file “File2.txt” is displayed in the terminal that also contains 5 number of lines:

$ cat -n File2.txt

Use the “diff –color” Command 

This section carries out the various practical examples that demonstrate the working of “diff –color” command for colorizing the “diff” output.

Example 1: Highlight Different Context

The “diff –color” command without any argument scans the difference between two files. It also gives suggestions to add, remove, or change the lines that are different:

$ diff --color File1.txt File2.txt

The above command has highlighted the different lines of “File1” and “File2”. The “4c4” instructs to change(c) the fourth line of File1 with the fourth line of File2.

Example 2: Print Output in Unified Format

The “diff –color” command is also compatible with the “-u(unified format)” flag. It displays the output in a unified format and prevents unnecessary lines i.e the same lines of compared files:

$ diff --color -u File1.txt File2.txt

In the above output:

  • The “File1.txt” name along with its timestamp is marked with “—” characters. Whereas the “+++” characters are used for “File2.txt”.
  • The “@@” denotes the range of both files i.e “-1,5” of File1.txt and “+1,5” of File2.txt.
  • The “” symbol tells the user to delete the “red” line of File1.txt and “+” instructs that add the “green” line of File2.txt into FIle1.

Example 3: Display Colorized Output

Apart from the unified format the “diff –color” command can also colorize the complete output with the “-c” flag in this way:

$ diff --color -c File1.txt File2.txt

The output is a little bit different from the unified format:

  • The ******* acts as a separator between files content and the timestamp.
  • The ***1,5**** shows the “File1.txt” range and the —1,5 —- represents the “File2.txt”.
  • Red lines show the “File1.txt” content and green lines display “File2.txt” data.
  • The “!” symbol denotes the different lines of File1 and File2

Example 4: Change the Default Colour Scheme

By default, the “diff –color” highlights the output in the “RGB” color pattern. In order to change the default color selection, the “palette” flag is used with the ANSI color code:

$ diff --color --palette=':ad=36:de=33:ln=31' File1.txt File2.txt

In the above command, the “ad(add lines)” ANSI color code is “36(for Cyan)”. The “de(delete lines)” uses “33(yellow)” color code, and “ln(line number)” uses “31(red)”:

The output shows the specified new color scheme.

Example 5: Create Alias for “diff –color” Temporarily

If the user wants to always display the colorized output of the “diff” command, then create an alias for it:

$ alias diff='diff --color'

The basic syntax follows for creating an alias is “alias [name]=[command]”. For instance, the “diff” alias is set to run the single-quoted “diff –color” command:

The alias has been created successfully.

Now, run the alias “diff” instead of the “diff –color” command in this format:

$ diff File1.txt File2.txt

The above command has displayed the colorized output in the terminal.

Example 6: Create Alias for “diff –color” Permanently

The above “alias” is created for temporarily basic that automatically deletes after the system shuts down. It can be created permanently by following this set of instructions.

Open the “.bashrc” File

To make the permanent alias open the “.bashrc” file in the “nano” text editor:

$ nano .bashrc

Scroll down the opened “.bashrc” file and add the following lines to the end of it:

$ alias diff='diff --color'

Press “Ctrl+S” to save the file and “Ctrl+X” to exit the text editor.

Update the “.bashrc” File

Update/reload the file to make the changes effective by using the “source” command:

$ source ~/.bashrc

All the new changes have been made in the “.bashrc” file.

Run the “diff” alias

Now execute the “diff” alias with the “-c(colorized)” flag to display the colorized output of compared files:

$ diff -c File1.txt File2.txt

The newly created permanent “diff” alias has done its job successfully.

Alternative: Use the “colordiff” Command

The “–color” option comes in the “3.4” version of the “diff” command. If you are using an older version then the “diff –color” command may generate an error. In such situations use an external “colordiff” utility that performs the same as the “diff –color” command.

Install the “colordiff” Tool

First install the “colordiff” command line tool using the default package managers as per Linux distributions:

$ sudo yum install colordiff                              # For RHEL/CentOS
$ sudo dnf install colordiff                              # For Fedora
$ sudo apt install colordiff                              # For Ubuntu/Debian

The installation of “colordiff” tool has been completed.

Use the “colordiff” Command

The “colordiff” command without any argument displays the same output as “diff –color”. Let’s see its practical implementation:

$ colordiff File1.txt File2.txt

The “colordiff” command has scanned the different lines of File1 and File2.

For getting the options list of the “colordiff” utility executes its “–help” command in the terminal:

$ colordiff --help

Scroll down the page for more flags/arguments.


In Linux, the “diff” color command line tool offers the “–color” option that scans the difference between compared files and prints the colorized output. The user can also use the “colordiff” command line tool for the older versions of the “diff” command to colorize its output.

This guide has described the complete procedure to color diff output.