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.
And here's the output sdiff produced in this case:
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:
sdiff file1 file2
produces the following output:
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
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:
Then there are several other space-related options that you should be aware of. Here's a list:
ignore changes due to tab expansion
ignore changes in the amount of white 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:
However, you can make the command ignore blank lines by using the -B command line option.
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
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.