Skip to main content

Module 3: Text Processing & Search

Lesson 36: sdiff Command

In this lesson, you'll learn how to use the sdiff command to show the differences between two files and merge them interactively in Linux.

,sdiff is a simple command-line utility for showing the differences between two files and merging interactively. It is easy to use and comes with straightforward usage options as explained below.

sdiff Command Syntax

The syntax for using sdiff is as follows.

$ sdiff [OPTIONS]... FILE1 FILE2

sdiff Command Options

Option Description
-a Treat all files as text and compare them line-by-line
-W Ignore all white space while comparing
-z Ignore white space at the line end
-E Ignore changes due to tab expansion
-i Ignore case when comparing (treat upper-case and lower-case as the same)
-B Ignore blank lines
-w N Set the number of output columns (default is 130)
-t Expand tabs to spaces in the output
-o FILE Run interactively and send merged output to a file
--diff-program Use another program to compare files

1. Show the Difference Between Two Files

The easiest way to run sdiff is to provide the two file names you are trying to compare. It will show the merged difference side-by-side as shown below.

First, let's create two sample files using the cal and df commands:

$ cal > cal.txt
$ df -h > du.txt

Now compare them with sdiff:

$ sdiff du.txt cal.txt
Filesystem      Size  Used Avail Use% Mounted on    | September 2023
tmpfs           794M  2.1M  792M   1% /run           | Su Mo Tu We Th Fr Sa
/dev/sda3        49G   12G   35G  26% /               | 1  2
tmpfs           3.9G     0  3.9G   0% /dev/shm        | 3  4  5  6  7  8  9
tmpfs           5.0M  4.0K  5.0M   1% /run/lock       | 10 11 12 13 14 15 16
/dev/sda2       512M  6.1M  506M   2% /boot/efi       | 17 18 19 20 21 22 23
tmpfs           794M  116K  794M   1% /run/user/1000  | 24 25 26 27 28 29 30
/dev/sr1        3.6G  3.6G     0 100% /media/ubuntu/Ubuntu 22  |
/dev/sr0        127M  127M     0 100% /media/ubuntu/CDROM       <

2. Treat All Files as Text

To treat all files as text and compare them line-by-line, whether they are text files or not, use the -a flag.

$ sdiff -a du.txt cal.txt
Filesystem      Size  Used Avail Use% Mounted on    | September 2023
tmpfs           794M  2.1M  792M   1% /run           | Su Mo Tu We Th Fr Sa
/dev/sda3        49G   12G   35G  26% /               | 1  2
tmpfs           3.9G     0  3.9G   0% /dev/shm        | 3  4  5  6  7  8  9
tmpfs           5.0M  4.0K  5.0M   1% /run/lock       | 10 11 12 13 14 15 16
/dev/sda2       512M  6.1M  506M   2% /boot/efi       | 17 18 19 20 21 22 23
tmpfs           794M  116K  794M   1% /run/user/1000  | 24 25 26 27 28 29 30
/dev/sr1        3.6G  3.6G     0 100% /media/ubuntu/Ubuntu 22  |
/dev/sr0        127M  127M     0 100% /media/ubuntu/CDROM       <

3. Ignore All White Space

Pro TecMint Β· Root Plan
This Article is for Root Members
Join Root to read the full article and unlock everything

Full Access to Every Article, Course & Certification Track

Join thousands of Linux professionals who use Pro TecMint to advance their careers.

Ad-free access to all premium articles
All courses: Learn Linux, Bash, Golang, Ubuntu and more
RHCSA, RHCE, LFCS & LFCA certification prep
New courses added every month
Private Telegram community & priority support
Root Plan
$8/mo
or $59/year billed annually
Save $37 with annual plan
Start Reading This Article in the Next 60 Seconds
Join Root Plan β†’