Cheat Sheet #day59 - diff
diff
Command Cheatsheet
The diff
command in Unix-like systems is used to compare two files line by line and display the differences between them. Here’s a quick reference guide:
Basic Syntax
diff [OPTION]... FILE1 FILE2
Common Options
-u
,--unified
: Show differences in a unified format. This is the most commonly used format.diff -u file1.txt file2.txt
-c
,--context
: Show differences in a context format, which includes a few lines of context before and after each change.diff -c file1.txt file2.txt
-i
,--ignore-case
: Ignore case differences.diff -i file1.txt file2.txt
-w
,--ignore-all-space
: Ignore all white space.diff -w file1.txt file2.txt
-B
,--ignore-blank-lines
: Ignore changes whose lines are all blank.diff -B file1.txt file2.txt
-y
: Display differences side by side.diff -y file1.txt file2.txt
-Z
,--ignore-trailing-space
: Ignore whitespace at the end of lines.diff -Z file1.txt file2.txt
Examples
Basic difference between two files:
diff file1.txt file2.txt
Unified format comparison:
diff -u file1.txt file2.txt
Context format comparison:
diff -c file1.txt file2.txt
Ignore case differences:
diff -i file1.txt file2.txt
Ignore all white space:
diff -w file1.txt file2.txt
Ignore blank lines:
diff -B file1.txt file2.txt
Side-by-side comparison:
diff -y file1.txt file2.txt
Ignore trailing spaces:
diff -Z file1.txt file2.txt
Displaying Differences with Line Numbers
Show line numbers in the output:
diff -n file1.txt file2.txt
Additional Information
Help option:
diff --help
View
diff
manual page:man diff
This cheatsheet covers the essential options and usage scenarios for the diff
command. For more advanced features and details, refer to the man
page or use diff --help
.
Subscribe to my newsletter
Read articles from Cloud Tuned directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by