LinuxCommandLibrary

sdiff

Compare and merge two files side-by-side

TLDR

Compare 2 files

$ sdiff [path/to/file1] [path/to/file2]
copy

Compare 2 files, ignoring all tabs and whitespace
$ sdiff [[-W|--ignore-all-space]] [path/to/file1] [path/to/file2]
copy

Compare 2 files, ignoring whitespace at the end of lines
$ sdiff [[-Z|--ignore-trailing-space]] [path/to/file1] [path/to/file2]
copy

Compare 2 files in a case-insensitive manner
$ sdiff [[-i|--ignore-case]] [path/to/file1] [path/to/file2]
copy

Compare and then merge, writing the output to a new file
$ sdiff [[-o|--output]] [path/to/merged_file] [path/to/file1] [path/to/file2]
copy

SYNOPSIS

sdiff [OPTION]... FILE1 FILE2

Compares FILE1 and FILE2 side-by-side, displaying their differences.

PARAMETERS

-o FILE, --output=FILE
    Output differences interactively into FILE. This enables a merging mode where you can choose which version of a line to keep, or edit it directly.

-s, --suppress-common-lines
    Do not output common lines; only show lines that differ between the two files.

-l, --left-column
    Output only the left common column. This is useful when you only care about the content of FILE1 but want to see where it aligns with FILE2.

-W NUM, --width=NUM
    Set the output width to at most NUM columns. The default width is typically 130 columns.

-t, --expand-tabs
    Expand tabs to spaces in the output. This ensures consistent alignment, especially when files contain varying tab widths.

-w, --ignore-all-space
    Ignore all white space characters when comparing lines. Differences due to varying amounts of spaces or tabs will be ignored.

-B, --ignore-blank-lines
    Ignore changes whose lines are all blank. This means sequences of blank lines will be treated as identical, regardless of their count.

-i, --ignore-case
    Ignore case differences in file content. For example, 'Text' and 'text' would be considered identical.

--diff-program=PROGRAM
    Use a specified PROGRAM (e.g., 'diff -Nru') to perform the underlying diff comparison instead of the default internal algorithm.

DESCRIPTION

sdiff (side-by-side diff) is a utility that displays the differences between two files in a side-by-side format. Unlike diff, which presents differences in a line-by-line, unified, or context format, sdiff aligns corresponding lines from both files next to each other. Common lines appear in both columns, while differing lines or unique lines are indicated with specific markers (e.g., '|' for differing, '<' for left-only, '>' for right-only).

It's particularly useful for visual comparison and has an optional interactive merge mode (via the -o option) allowing users to choose which version of a line to keep, edit it, or use both, making it a simple tool for manual merging tasks. sdiff is part of the GNU Diffutils package.

CAVEATS

The side-by-side display can become challenging to read with very long lines or extensive differences, especially if the terminal width is limited.

The interactive merge mode, while useful for simple cases, is basic and less powerful than dedicated graphical merge tools (e.g., kdiff3, meld) for complex three-way merges or advanced conflict resolution in version control systems.

It is primarily a visual comparison tool; for programmatic difference analysis or generating patch files, the diff command is generally preferred.

INTERACTIVE MERGE MODE

When used with the -o FILE option, sdiff enters an interactive merge session. For each set of differing lines, sdiff prompts the user with various options such as l (take left version), r (take right version), b (take both versions), e (edit the lines), s (silently skip), or q (quit). This makes it a convenient command-line tool for performing simple and direct code merges or resolving minor conflicts.

HISTORY

sdiff is an integral part of the GNU Diffutils package, which originated in the early days of Unix to provide robust tools for comparing files. While diff focused on generating patchable difference formats, sdiff emerged to cater specifically to the need for a human-readable, side-by-side presentation of file differences. This visual approach became particularly valuable for code review and manual merge operations. Its later addition of an interactive merge capability further enhanced its utility by allowing users to make on-the-fly decisions about changes directly from the command line.

SEE ALSO

diff(1), cmp(1), comm(1), patch(1)

Copied to clipboard