How to Compare Two Text Files Side-by-Side in Ubuntu Using sdiff

System administrators and developers frequently need to compare two versions of a script, configuration file, or log output on an Ubuntu server. While the standard diff command is powerful, its unified output format can be difficult to interpret when comparing complex code blocks or configuration parameters across extensive documents.

Instead of installing graphical comparison utilities like Meld or VS Code on headless servers, you can utilise sdiff. The sdiff utility is a native command-line tool built into the standard GNU diffutils package that formats differences into two clean, side-by-side columns directly in your terminal.

Basic Syntax and How sdiff Works

The standard syntax for running a side-by-side comparison is straightforward:

sdiff file1.txt file2.txt

The output splits your terminal into two equal vertical columns. file1.txt is displayed on the left, and file2.txt is displayed on the right. In the gutter between the two columns, sdiff prints special comparison markers:

  • Blank space: The lines on the left and right are completely identical.
  • | (pipe): The lines differ between the two files.
  • < (left arrow): The line exists only in the first file (left side).
  • > (right arrow): The line exists only in the second file (right side).

Step-by-Step Example: Comparing Configuration Files

Consider two versions of an Nginx server configuration: nginx_old.conf and nginx_new.conf. To view differences side-by-side, run:

sdiff nginx_old.conf nginx_new.conf

A typical output might look like this:

worker_processes 2;                           worker_processes 4;                 |
worker_connections 1024;                      worker_connections 1024;
keepalive_timeout 65;                         keepalive_timeout 30;               |
gzip on;                                      gzip on;
                                            > gzip_comp_level 6;
access_log /var/log/nginx/access.log;         access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log warn;    <

Notice how easy it is to identify that worker_processes and keepalive_timeout were modified (marked by |), gzip_comp_level was added to the new file (marked by >), and error_log was deleted from the new file (marked by <).

Filtering and Customising sdiff Output

Depending on the width of your terminal and the nature of the files being evaluated, you can tailor sdiff with useful flags:

1. Adjusting Column Width

By default, sdiff formats output to 130 columns. If you are using a wider terminal or inspecting files with long lines, expand the width using -w or --width:

sdiff -w 180 file1.txt file2.txt

2. Suppressing Identical Lines

When comparing large configuration files that share hundreds of identical lines, reading through unchanged text is inefficient. Use the -s (suppress common lines) flag to display only differences:

sdiff -s file1.txt file2.txt

3. Ignoring Whitespace Differences

To avoid flagging changes caused solely by spaces, indentation, or blank lines, use -b (ignore changes in the amount of whitespace) and -B (ignore blank lines):

sdiff -s -b -B script_v1.py script_v2.py

Interactively Merging Files with sdiff

One of the most powerful features of sdiff is its interactive merging mode. By supplying the -o flag followed by an output filename, sdiff prompts you to choose which line to keep whenever a difference is detected:

sdiff -o merged_config.conf file1.txt file2.txt

When a conflict occurs, sdiff pauses and displays a prompt (%). You can input any of the following commands:

  • l: Accept the left line.
  • r: Accept the right line.
  • b: Keep both lines (left first, then right).
  • eb: Open a text editor to manually edit both lines.
  • q: Abort the merge session without saving.

Using sdiff provides a fast, lightweight, and visual terminal-based workflow for comparing configurations and auditing code changes without requiring heavy desktop dependencies in Ubuntu.

Get the best tech tips delivered straight to your inbox.

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