← All commands
THE COMMAND REFERENCE

sdiff

Compare two files side by side, or merge them interactively.

Last updated

Portable / common

Implementation-level reference; examples are not certified for every installed version.

Overview

sdiff shows two files in two columns, with a mark between them: nothing for the same line, | for a changed line, < for a line only on the left and > for a line only on the right. With -o FILE it asks you, line group by line group, which side to keep and writes the merged file.

Background

Today the name covers GNU sdiff and the sdiff that macOS ships. The BusyBox 1.37.0 build used for this page has no sdiff applet. They agree on the two-column view and on -w for the width.

Examples

Show two files side by side; the | marks a changed line, and the exact spacing is compared with blanks collapsed:

printf 'a\nb\n' > f1 && printf 'a\nc\n' > f2 && sdiff f1 f2; test $? -eq 1 && echo differ

Set the total width of the output with -w:

printf 'a\nb\n' > f1 && printf 'a\nc\n' > f2 && sdiff -w 20 f1 f2; test $? -eq 1 && echo differ

Show only the lines that differ with -s, leaving out the common ones:

printf 'a\nb\n' > f1 && printf 'a\nc\n' > f2 && sdiff -s f1 f2; test $? -eq 1 && echo differ

Options

-w
Set the width of the output in characters.

-s
Do not print lines that are the same.

-o
Merge interactively and write the result to this file.

Common mistakes

sdiff exits with status 1 when the files differ, like diff. The columns are padded with spaces and tabs, which differ between systems, so do not parse the output. Lines longer than half the width are cut. The interactive mode with -o waits for you to type a choice for each block, so it is not for scripts.

Portability

This page has no BusyBox view: BusyBox has no sdiff applet. The layout of the columns (tabs or spaces) differs between GNU and macOS, so the examples compare output with runs of blanks collapsed. The Common examples were executed on GNU diffutils 3.12 and macOS system tools (see the development guide for the exact check).

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.