sdiff command in Linux with Examples
Last Updated :
27 Sep, 2024
sdiff command in Linux is used to compare two files and then writes the results to standard output in a side-by-side format. It displays each line of the two files with a series of spaces between them if the lines are identical. It displays a greater than sign if the line only exists in the file specified by the File2 parameter, and a | (vertical bar) for lines that are different.
Syntax
sdiff [ -l | -s ] [ -o OutFile ] [ -w Number ] File1 File2
where,
- File1: The first file to compare.
- File2: The second file to compare.
Basic Example
Text File 1 (file1.txt):
Geeks
For
Geeks
A
Computer
Science
Portal
For
Geeks
Text File 2 (file2.txt):
Geeks
For
Geeks
Technical
Scripter
2018
Running the Command:
sdiff file1.txt file2.txt
Output:
Geeks Geeks
For For
Geeks Geeks
>
A Technical
Computer Scripter
Science 2018
Portal |
For For
Geeks Geeks
Common Options used with the sdiff command
1. sdiff -l file1 file2
:
Display only the left side of the comparison when lines are identical. This reduces the width of the output, making it easier to read.
2. sdiff -s file1 file2
:
Suppress identical lines. This option only shows lines that are different between the two files, making it easier to focus on the differences.
3. sdiff -w Number file1 file2
:
It sets the width of the output line. The default value of the Number variable is 130 characters. The maximum width of the Number variable is 2048. The minimum width of the Number variable is 20. The sdiff command uses 2048 if a value greater than 2048 is specified.
4. sdiff -o OutFile file1 file2
:
Creates a third file, specified by the OutFile variable, by a controlled line-by-line merging of the two files specified by the File1 and the File2 parameters. The following subcommands govern the creation of this file: Output File:
Geeks
For
Geeks
--- geek1.txt 5, 10
A
Computer
Science
Portal
For
Geeks
+++ geek2.txt 5, 8
Technical
Scripter
2018
Conclusion
The sdiff command is an essential tool for file comparison in Linux, offering clear side-by-side output for identifying differences between two files. With options to suppress identical lines, set output width, and merge files interactively, it is highly customizable and versatile, making it suitable for a wide range of tasks, including software development, document comparison, and system administration.
sdiff command in Linux – FAQs
What does the sdiff command do?
The sdiff command compares two files side-by-side, displaying differences between them. Identical lines are displayed with spaces in between, and different lines are marked with symbols (| and >).
How do I suppress identical lines in sdiff?
You can suppress identical lines by using the -s option:
sdiff -s file1.txt file2.txt
How can I adjust the width of the output using sdiff?
Use the -w option followed by a number to specify the output width:
sdiff -w 150 file1.txt file2.txt
What does the -o option do in sdiff?
The -o option allows you to merge two files interactively and save the result in a new file. You’ll be prompted to choose which lines to include from each file.