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.
Similar Reads
rm command in Linux with examples rm stands for remove here. rm command is used to remove objects such as files, directories, symbolic links and so on from the file system like UNIX. To be more precise, rm removes references to objects from the filesystem, where those objects might have had multiple references (for example, a file w
5 min read
rmdir Command in Linux With Examples In Linux, managing directories efficiently is a crucial skill for both system administrators and everyday users. The 'rmdir' command is a specialized tool designed specifically for removing empty directories, helping to maintain a clean file system and avoid accidental data loss. This guide delves i
6 min read
rmmod command in Linux with Examples The rmmod command in Linux is used to remove or unload a module from the kernel. Modules are pieces of code that can be dynamically loaded into the Linux kernel to extend its functionality, such as drivers for hardware devices. When a module is no longer needed, it can be removed using rmmod. Althou
3 min read
How to check the routing table in Linux | route Command The IP/kernel routing table acts as a crucial map, determining how network packets are forwarded between different hosts and networks. By utilizing the route command, Linux administrators and users can establish static routes, enabling precise control over network connectivity and optimizing data tr
4 min read
rsync command in Linux with Examples rsync or remote synchronization is a software utility for Unix-Like systems that efficiently sync files and directories between two hosts or machines. One is the source or the local-host from which the files will be synced, the other is the remote-host, on which synchronization will take place. Ther
7 min read
SAR command in Linux to monitor system performance sar (System Activity Report) It can be used to monitor Linux system's resources like CPU usage, Memory utilization, I/O devices consumption, Network monitoring, Disk usage, process and thread allocation, battery performance, Plug and play devices, Processor performance, file system and more. Linux s
9 min read
How to Securely Copy Files in Linux | scp Command Secure file transfer is a crucial part of Linux systems administration. Whether moving sensitive files between local machines or transferring data between servers, or you need to move backup files to a remote server, fetch logs from a hosted machine, or sync directories across multiple systems, scp
10 min read
screen command in Linux with Examples The screen command is an advanced terminal multiplexer that allows you to have multiple sessions within one terminal window. It's like having "tabs" in your Linux terminal â you can open, detach, switch, or resume sessions at any time without losing what you're working on. It's particularly convenie
7 min read
script command in Linux with Examples The 'script' command in Linux is a versatile tool that allows you to record all terminal activities, including inputs and outputs, making it a valuable resource for developers, system administrators, educators, and anyone who needs to document terminal sessions. This command captures everything disp
5 min read
scriptreplay command in Linux with Examples scriptreplay command is used to replay a typescript/terminal_activity stored in the log file that was recorded by the script command. With the help of timing information, the log files are played and the outputs are generated in the terminal with the same speed the original script was recorded. The
3 min read