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 : Indicates the filename and modification time of the first file. --- menu1.txt 2019-12-16 18:46:13.794879924 +0900 : Indicates the filename and modification time of the second file. +++ menu2.txt 2019-12-16 18:46:42.090995670 +0900 : Marks a block of differences, indicating where differences occur between the two files. denotes six lines starting from the first line in the original file, and represents the same in the modified file. @@ -1,6 +1,6 @@ -1,6 +1,6 : Represents the content in the original file. signifies deleted lines. -Menu1: - : Represents the modified content in the new file. signifies added lines. +Menu: + , : These lines remain unchanged from the original file. Apples Bananas , : These lines from the original file were removed in the modified file. Oranges Pears , : These lines were added in the modified file. Grapes Strawberries 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 : This part represents the execution of the command to compare and . $ diff -u hello_world.txt hello_world_long.txt diff hello_world.txt hello_world_long.txt : This line shows the filename and the last modification time of the first file. --- hello_world.txt 2019-12-16 19:24:12.556102821 +0900 : This line shows the filename and the last modification time of the second file. +++ hello_world_long.txt 2019-12-16 19:24:38.944207773 +0900 : This is a diff block marker, indicating the difference between the two files. refers to the first line in the original file, and indicates the first line to the third line in the new file. @@ -1 +1,3 @@ -1 +1,3 : This line represents the content in the original file. Hello World : Signifies added content. + : This line represents the added content in the new file. It's a wonderful day!