How to Compare Two Files Side by Side in Linux Terminal Using sdiff

While the standard Linux diff utility provides a comprehensive line-by-line unified report of changes between files, reading unified diff chunks can be challenging when evaluating long configuration files or code refactorings. The sdiff command resolves this by generating a two-column, side-by-side comparison directly in your terminal window. It clearly contrasts added, deleted, and modified lines, and even includes an interactive merge mode to resolve differences between two files on the fly.

Understanding the sdiff Output Format

When you run sdiff without special arguments, it splits your terminal display into two equal columns: the first file appears on the left, and the second file appears on the right. Between the columns, sdiff prints a symbol to denote the relationship between the corresponding lines:

  • Blank space: The lines are identical in both files.
  • < (Left caret): The line exists only in the first (left-hand) file.
  • > (Right caret): The line exists only in the second (right-hand) file.
  • | (Pipe): The line exists in both files, but the contents differ between them.
  • ( / ): Used when one file has incomplete or non-matching blank lines in certain context modes.

Basic sdiff Command Syntax

To perform a straightforward side-by-side comparison between two text files, provide both filenames as positional arguments:

sdiff original.conf updated.conf

If the files contain identical text, sdiff outputs the identical lines side by side with no gutter markers between them. To suppress identical lines entirely and display only the differences, add the -s or --suppress-common-lines option:

sdiff -s original.conf updated.conf

This filters out unchanged lines, letting you focus immediately on divergent configuration values or modified code blocks.

Adjusting Output Width for Wider Terminals

By default, sdiff formats its output to fit an 80-column terminal display (40 characters per file). On modern widescreen terminals, this often truncates lines prematurely. You can specify a custom column width using the -w or --width option:

sdiff -w 160 original.conf updated.conf

To automatically adapt the column width to the exact dimensions of your active terminal window, pass the current environment width using tput:

sdiff -w $(tput cols) original.conf updated.conf

Ignoring Whitespace and Blank Lines

When comparing files transferred between different operating systems or edited in different text editors, minor indentation changes and trailing whitespace can clutter diff results. You can instruct sdiff to ignore cosmetic differences:

  • Ignore changes in the amount of white space: sdiff -b original.txt updated.txt
  • Ignore all spaces and tabs entirely: sdiff -W original.txt updated.txt
  • Ignore blank lines: sdiff -B original.txt updated.txt
  • Ignore case sensitivity: sdiff -i original.txt updated.txt

Combining these flags allows you to audit substantive logic changes while disregarding reformatting or tab-to-space conversions:

sdiff -s -b -B original.py updated.py

Interactively Merging Two Files with sdiff

One of the most powerful features of sdiff is its ability to interactively resolve conflicts and write a merged output file using the -o (output) flag:

sdiff -o merged.conf original.conf updated.conf

When sdiff encounters identical lines, it writes them directly to the destination file. Whenever it detects a discrepancy, execution pauses and displays a prompt (%) awaiting your decision. You can enter any of the following interactive commands:

  • l: Choose the left-hand file version.
  • r: Choose the right-hand file version.
  • e: Open the conflict block in your default text editor (configured via the EDITOR environment variable) to manually compose a custom resolution.
  • eb: Open both versions concatenated in the text editor.
  • el / er: Open the left or right version in your text editor before saving.
  • s: Silently skip printing identical lines in subsequent prompts.
  • v: Verbose mode (re-display the current diff chunk).
  • q: Quit the interactive merge session without saving further changes.

Once all discrepancies are addressed, sdiff saves the consolidated file, offering a lightweight alternative to graphical merge utilities like Meld or KDiff3 in headless server environments.

Get the best tech tips delivered straight to your inbox.

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