How to Compare Files Side by Side in Linux Terminal Using sdiff and colordiff

Auditing configuration files, reviewing source code modifications, or verifying server environment variables in Linux is frequently accomplished using the classic diff utility. However, standard unified diff output interleaves removed and added lines vertically, which can be disorienting when inspecting long lines or subtle formatting differences. Side-by-side file comparison presents both versions simultaneously across two columns, highlighting alterations with intuitive visual markers.

Using the native sdiff utility alongside colordiff allows sysadmins and software developers to spot character-level discrepancies instantly directly within any command-line environment.

Comparing Files Side by Side Using sdiff

The sdiff utility is included by default as part of the GNU diffutils package on virtually all Linux distributions, requiring no installation.

  1. To perform a basic side-by-side comparison between two files (for example, an original configuration file and an updated draft):
sdiff config.conf config.conf.new
  1. The terminal splits into two equal columns: the left column represents the first file, and the right column represents the second file.
  2. Observe the comparison indicators rendered between the columns:
    • | (Pipe): Indicates that the lines differ between the two files.
    • < (Left arrow): Indicates that the line exists only in the first file.
    • > (Right arrow): Indicates that the line exists only in the second file.
    • Blank space: Indicates that the lines are identical across both files.

Customising Terminal Width and Suppressing Identical Lines

By default, sdiff wraps output to 130 columns, which may truncate long code statements or configuration parameters on modern wide terminal displays:

  1. To expand the comparison width to match an ultra-wide terminal window (e.g. 180 columns):
sdiff -w 180 config.conf config.conf.new
  1. When comparing large files spanning thousands of lines where only two or three lines differ, reading identical text is counterproductive. Use the -s (suppress common lines) flag to isolate only the changed regions:
sdiff -s config.conf config.conf.new

Adding Syntax Colour Highlighting with colordiff

While sdiff provides clean column layout, its monochrome text can strain the eyes. You can pipe sdiff output through colordiff to render additions in green, deletions in red, and modifications in cyan:

  1. Install colordiff using your package manager:
# Ubuntu / Debian
sudo apt install -y colordiff

# RHEL / Fedora / Rocky Linux
sudo dnf install -y colordiff
  1. Run colordiff with the -y (side-by-side) option:
colordiff -y -w 160 config.conf config.conf.new | less -R
  1. Piping to less -R ensures colour escape codes render properly while allowing you to scroll through large comparisons using standard vim navigation keys.

Interactively Merging File Changes with sdiff

Beyond passive visual inspection, sdiff features an interactive merge mode that lets you build an updated file line by line:

sdiff -o merged_output.conf config.conf config.conf.new

Whenever sdiff encounters a discrepancy, it pauses with a % prompt. Type l to accept the left version, r to accept the right version, or e to open the lines in your default text editor for custom manual synthesis.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.