Tomorrows Technology Today

How To

Compare Files side by side with sdiff

I have been familiar with the linux diff command for a long time, for comparing files, but never really liked the output and I would gravitate towards tools such as Notepad++ or Visual studio instead for the task. Recently I came across sdiff, which is very similar to diff except for the key difference that the output is displayed side by side, this makes it far more practical for me to find differences in files on the command line. Adding the -s parameter to supress common output also helps you get straight to the useful information.

Exploring the sdiff Command in Linux

The sdiff command compares two files line by line and displays the differences in a side-by-side format. This makes it easier to see the variations between the files at a glance. Unlike the diff command, which lists differences sequentially, sdiff presents them in a more readable, parallel format.

Basic Syntax

The basic syntax, for the sdiff command is the same as diff, where file1 and file2 are the names of the files you want to compare:

sdiff [options] file1 file2


Here, file1 and file2 are the names of the files you want to compare.

Key Options

sdiff comes with several options that enhance its functionality:

  • -o, --output=FILE: Operate interactively, sending output to FILE for merging.
  • -i, --ignore-case: Ignore case differences.
  • -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.
  • -I, --ignore-matching-lines=RE: Ignore changes whose lines match the regular expression RE.
  • -H, --speed-large-files: Speed up comparison of large files.
  • -a, --text: Treat all files as text.
  • -w, --width=NUM: Set the output width to NUM columns (default is 130).
  • -l, --left-column: Output only the left column of common lines.
  • -s, --suppress-common-lines: Do not output common lines.

Practical Examples

Let’s look at some practical examples to understand how sdiff works.

Basic Comparison

 To compare two files, simply use:sdiff file1.txt file2.txt This will display the differences between file1.txt and file2.txt side-by-side

Ignoring Case Differences 

If you want to ignore case differences, use the -i option:sdiff -i file1.txt file2.txt

    Suppressing Common Lines

     To suppress lines that are identical in both files, use the -s option:sdiff -s file1.txt file2.txt

      user@pc:/usr/lib/tuned$ sdiff -s laptop-ac-powersave/tuned.conf laptop-battery-powersave/tuned.conf
      summary=Optimize for laptop with power savings                | summary=Optimize laptop profile with more aggressive power sa
      include=desktop-powersave                                     | include=powersave
                                                                    <
      [script]                                                      <
      script=${i:PROFILE_DIR}/script.sh                             <

      Leave a Reply

      Your email address will not be published. Required fields are marked *