diff and patch
The diff command
You use the diff command to find the differences between two files. On its own, itβs a bit hard to use; instead, use diff -u to find lines that differ in two files:
Using diff -u
You use the diff -u command to compare two files, line by line, and have the differing lines compared side-by-side in the same output. For an example of what this looks like, see below:
Command:
$ cat menu1.txt
Code output:
Menu1:
Apples
Bananas
Oranges
Pears
Command:
$ cat menu2.txt
Code output:
Menu:
Apples
Bananas
Grapes
Strawberries
Command:
$ diff -u menu1.txt menu2.txt
Code output:
--- menu1.txt 2019-12-16 18:46:13.794879924 +0900
+++ menu2.txt 2019-12-16 18:46:42.090995670 +0900
@@ -1,6 +1,6 @@
-Menu1:
+Menu:
Apples
Bananas
-Oranges
-Pears
+Grapes
+Strawberries
Explaination
--- menu1.txt 2019-12-16 18:46:13.794879924 +0900
: Indicates the filename and modification time of the first file.+++ menu2.txt 2019-12-16 18:46:42.090995670 +0900
: Indicates the filename and modification time of the second file.@@ -1,6 +1,6 @@
: Marks a block of differences, indicating where differences occur between the two files.-1,6
denotes six lines starting from the first line in the original file, and+1,6
represents the same in the modified file.-Menu1:
: Represents the content in the original file.-
signifies deleted lines.+Menu:
: Represents the modified content in the new file.+
signifies added lines.Apples
,Bananas
: These lines remain unchanged from the original file.Oranges
,Pears
: These lines from the original file were removed in the modified file.Grapes
,Strawberries
: These lines were added in the modified file.
The patch command
The patch command is useful for applying file differences. See the example below, which compares two files. The comparison is saved as a .diff file, which is then patched to the original file!
Command:
$ cat hello_world.txt
Code output:
Hello World
Command:
$ cat hello_world_long.txt
Code output:
Hello World
It's a wonderful day!
Command:
$ diff -u hello_world.txt hello_world_long.txt
Code output:
--- hello_world.txt 2019-12-16 19:24:12.556102821 +0900
+++ hello_world_long.txt 2019-12-16 19:24:38.944207773 +0900
@@ -1 +1,3 @@
Hello World
+
+It's a wonderful day!
Command:
$ diff -u hello_world.txt hello_world_long.txt > hello_world.diff
$ patch hello_world.txt < hello_world.diff
Code output:
patching file hello_world.txt
Command:
$ cat hello_world.txt
Code output:
Hello World
It's a wonderful day!
Explanation
$ diff -u hello_world.txt hello_world_long.txt
: This part represents the execution of thediff
command to comparehello_world.txt
andhello_world_long.txt
.--- hello_world.txt 2019-12-16 19:24:12.556102821 +0900
: This line shows the filename and the last modification time of the first file.+++ hello_world_long.txt 2019-12-16 19:24:38.944207773 +0900
: This line shows the filename and the last modification time of the second file.@@ -1 +1,3 @@
: This is a diff block marker, indicating the difference between the two files.-1
refers to the first line in the original file, and+1,3
indicates the first line to the third line in the new file.Hello World
: This line represents the content in the original file.+
: Signifies added content.It's a wonderful day!
: This line represents the added content in the new file.