Linux sdiff Command Tutorial for Beginners (6 Examples)

Here at HowtoForge, we have already discussed multiple command line utilities that are focused on finding file differences. But there are several more such tools, and in this tutorial, we will discuss on such command dubbed sdiff using some easy to understand examples.

Please note that all examples mentioned in this article have been tested on Ubuntu 16.04 LTS.

Linux sdiff command

The sdiff command shows you file differences in side-by-side form. Following is its syntax:

sdiff [OPTION]... FILE1 FILE2

And here's how the man page describes it:

sdiff - side-by-side merge of file differences
Side-by-side merge of differences between FILE1 and FILE2.

Following are some Q&A-styled examples that should give you a good idea on how this tool works.

Q1. How sdiff command works?

Basic usage is simple, all you have to do is to run the sdiff command with filenames as arguments. For example:

sdiff file1 file2

file1 and file2 were exactly same in my case.

How sdiff command works

And here's the output sdiff produced in this case:

sdiff command result

This type of output means there's no difference in files.

Q2. How to make sdiff ignore case?

Suppose file1 and file2 contain following lines:

How to make sdiff ignore case

The command:

sdiff file1 file2

produces the following output:

sdiff file1 file2

The pipe (|) in the first line in the second column indicates the two files are different in the very first line itself. However, if you want, you can force sdiff to ignore case, something which you can do using the -i command line option.

sdiff -i file1 file2

sdiff -i file1 file2

So you can see the output now shows the two files to be identical, meaning the case difference was ignored.

Q3. How to make sdiff ignore spaces?

In case any of the file contains spaces, the sdiff command will show the difference by default. However, if you want, you can suppress this behavior using the -Z command line option:

How to make sdiff ignore spaces

Then there are several other space-related options that you should be aware of. Here's a list:

-E, --ignore-tab-expansion
              ignore changes due to tab expansion
-b, --ignore-space-change
              ignore changes in the amount of white space
-W, --ignore-all-space
              ignore all white space

Q4. How to make sdiff ignore blank lines?

Just like spaces, by default, the sdiff command takes blank lines in to account as well. For example:

How to make sdiff ignore blank lines

However, you can make the command ignore blank lines by using the -B command line option.

sdiff -B file1 file2

sdiff -B file1 file2

So you can see the sdiff command didn't consider blank line as a difference in this case.

Q5. How to make sdiff suppress common lines?

Suppose the files contain some common lines, and you want the sdiff command to suppress common lines, then you can do this using the -s command line option.

sdiff -s file1 file2

Q6. How to make sdiff use a different diff program?

By default, sdiff uses diff to carry out the comparison. However, if you want, you can even replace diff with the program of your choice. To do this, use the --diff-program option which requires program name as input.

sdiff --diff-program=[PROGRAM] file1 file2

Conclusion

The Sdiff tool might offer a lot of features, but it's learning curve isn't that steep. All you need to do is a bit of practice, and you should be good to go. We've discussed the majority of command line options here. For more, head to sdiff's man page.

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

Suggested articles

0 Comment(s)

Add comment