sdiff Command in Linux Explained [With Examples]

Last updated: October 20, 2023 | Bobbin Zachariah

The sdiff command in Linux compares two files line by line and displays the differences in a side-by-side format. It takes two files as an argument and outputs the differences marked with special characters like ‘>’, ‘<’, and ‘|’.

sdiff helps to quickly understand the differences between two files as it presents the differences side-by-side making it easier to compare the variations. It is very similar to the diff command. The key difference comes in the way they present the output to the user.

Syntax

sdiff [options] FILE1 FILE2

"FILE1" and "FILE2" are the names of the files you're comparing.

Key Options

Some of the useful options to use with sdiff command:

  • -o, --output=FILE: Operate interactively, sending output to FILE - merging.
  • -i, --ignore-case: Consider upper- and lower-case to be the same.
  • -E, --ignore-tab-expansion: Ignore changes due to tab expansion.
  • -Z, --ignore-trailing-space: Ignore white space at line end.
  • -b, --ignore-space-change: Ignore changes in the amount of white space.
  • -W, --ignore-all-space: Ignore all white space.
  • -B, --ignore-blank-lines: Ignore changes whose lines are all blank.
  • -b, --ignore-space-change: Ignore changes in the amount of white space.
  • -I, --ignore-matching-lines=RE: Ignore changes all whose lines match RE.
  • -H, --speed-large-files: Speeds up comparison of large files.
  • --strip-trailing-cr: Strip trailing carriage return on input.
  • -a, --text: Treat all files as text.
  • -w, --width=NUM: Output at most NUM (default 130) print columns.
  • -l, --left-column: Output only the left column of common lines.
  • -s, --suppress-common-lines: Do not output common lines.

Basic Usage

To compare two files

sdiff file1.txt file2.txt

Let's consider two files file.1.txt and file2.txt

cat file1.txt
  A
  1
  B
  C
  D
cat file2.txt
  a
  B
  C
  e
  2

Output

  A                                                           |   a
  1                                                           <
  B                                                               B
  C                                                               C
  D                                                           |   e
                                                              >   2

From the output above, the symbol ‘|’ indicates the lines are different, lines 1 and 5. In line 2, the ‘<’ indicates that the line is unique to the file1.txt. In line 6, ‘>’ indicates that the line is unique to file2.txt. There's no special symbol when it's identical in both files.

Examples

Let's look into some use case examples of sdiff.

1. -l option: Displaying only the left column of common lines

sdiff -l file1.txt file2.txt

Output

  A                                                           |   a
  1                                                           <
  B                                                           (
  C                                                           (
  D                                                           |   e
                                                              >   2

The common between the two files will be shown in the left column. The lines "B" and "C" are the same in both files, so only the version from file1.txt (left column) is shown.

2. -s option: Suppressing common lines

sdiff -s file1.txt file2.txt

Output

  A                                                           |   a
  1                                                           <
  D                                                           |   e
                                                              >   2

The lines "B" and "C", which are common in both files, are not shown in the output.

3. -i option: Ignoring case

sdiff -i file1.txt file2.txt

Output

  A                                                               a
  1                                                           <
  B                                                               B
  C                                                               C
  D                                                           |   e
                                                              >   2

"A" in file1.txt and "a" in file2.txt are considered identical because the -i option makes the comparison case-insensitive. Hence, they are shown side by side without any separator.

4. -b option: Ignoring white space changes

sdiff -b file1.txt file2.txt

This helps ignore white space, useful when differences in spacing, tabs, and newlines are not significant.

Here I have added an extra space before B in new1.txt. Now that line is matching with new2.txt.

$ sdiff  new1.txt new2.txt
  A                                                           |   a
  1                                                           |   B
   B                                                          <
  C                                                               C
  D                                                           |   e
                                                              >   2

Lets now tell sdiff to ignore white space change:

sdiff -b  new1.txt new2.txt
  A                                                           |   a
  1                                                           <
   B                                                              B
  C                                                               C
  D                                                           |   e
                                                              >   2

Now you can see the line showing side by side without any separator.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment