sdiff command in Linux with examples
The sdiff command produces a side-by-side comparison of file1 with file2. Output is shown in the format:
text text
Identical lines.
text <
Line that exists only in file1.
> text
Line that exists only in file2.
text | text
Lines that are different.
Examples:
1. To spot the differences of given files:
# sdiff file1.txt file2.txt
2. To operate interactively and send output to file:
# sdiff file1 file2 --output=FILE
3. To ignore the case:
# sdiff -i file1 file2
# sdiff --ignore-case file1 file2
4. To ignore changes due to tab expansion:
# sdiff -E file1 file2
# sdiff --ignore-tab-expansion file1 file2
5. To Ignore changes in the amount of white space:
# sdiff -b file1 file2
# sdiff --ignore-space-change file1 file2
6. To Ignore all white space:
# sdiff -W file1 file2
# sdiff --ignore-all-space file1 file2
7. To Ignore changes whose lines are all blank:
# sdiff -B file1 file2
# sdiff --ignore-blank-lines file1 file2
8. To Ignore changes whose lines all match RE:
# sdiff -I RE file1 file2
# sdiff --ignore-matching-lines=RE file1 file2
9. To strip trailing carriage return on input:
# sdiff --strip-trailing-cr file1 file2
10. To treat all files as text:
# sdiff -a file1 file2
# sdiff --text file1 file2
11. To Output at most NUM (default 130) columns per line:
# sdiff -w 10 file1 file2
# sdiff --width=NUM 10 file1 file2
12. To output only the left column of common lines:
# sdiff -l file1 file2
# sdiff --left-column file1 file2
13. To do not output common lines:
# sdiff -s file1 file2
# sdiff --suppress-common-lines file1 file2
14. To expand tabs to spaces in output:
# sdiff -t file1 file2
# sdiff --expand-tabs file1 file2
15. To Try hard to find a smaller set of changes:
# sdiff -d file1 file2
# sdiff --minimal file1 file2
16. To assume large files and many scattered small changes:
# sdiff -H file1 file2
# sdiff --speed-large-files file1 file2
17. To Use PROGRAM to compare files:
# sdiff --diff-program=PROGRAM file1 file2
18. To get the version of sdiff command:
# sdiff -v
# sdiff --version
19. To get the help:
# sdiff --help
Conclusion
If most of the lines are the same, consider using the -s flag so the identical lines are not shown. For example, type “sdiff -s file1 file2”. If the output scoots by too fast to read, remember that you can pipe the entire command to more, as in “sdiff file1 file2 | more”.