Linux sdiff Command Tutorial for Beginners (8 Examples)
On this page
- Linux sdiff command
- Q1. How sdiff command works?
- Q2. How to make sdiff ignore case?
- Q3. How to make sdiff ignore spaces?
- Q4. How to make sdiff ignore blank lines?
- Q5. How to make sdiff suppress common lines?
- Q6. How to make sdiff use a different diff program?
- Q7. How to save sdiff output to file
- Q8. How to output only left colum
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 one such command dubbed sdiff using some easy-to-understand examples.
Please note that all the sdiff command examples mentioned in this article have been tested on Ubuntu 22.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 the 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 the 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 files 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 into 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 the 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 the program name as input.
sdiff --diff-program=[PROGRAM] file1 file2
Q7. How to save sdiff output to file
The -o or --output option allows us to save the sdiff result to an output file. Example:
sdiff --output=/tmp/mydiff.txt file1 file2
Q8. How to output only left colum
With the command line option -l or its long form --left-column, you get only the left column as result.
sdiff --left-column file1 file2
The Sdiff tool offers a lot of features, but its 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.