Diff and Patch : A Guide
26 Sep 2019, 08:52am TZ +05:30
Finding our difference between files is a common thing I used to do.
For most of the time I had the trusty k3diff.
Things got messy with command line and scripting the difference job.
I am not sure how many times I searched for diff command linux
in Google.
Also, do manual patching not knowing the patch
command was a handicap.
Hence after much spite, I have decided to log it down here.
In fact the literal sense the above picture was my understanding of patch.
Later I came to understand patch.
Here are my conclusions:
diff
is a tool to create a summary of changes needed
patch
is a tool to apply those summary of changes needed
Note for Manjaro(Arch Linux) Users #
By default the install might not have the patch
tool.
So its better to install if needed:
|
|
Difference between Files #
Here is the command
|
|
If you get an output then there is difference.
If not Then there is no difference between the files.
Simple! Isn’t it !
Side by Side comparison #
|
|
Better Output and Understandable #
|
|
This output is also great for sharing as part of documentation.
Creating a Patch #
|
|
This patch_file
would be used to modify the respective files.
Patching Files #
From the earlier example we created the patch_file
.
Let’s now modify the file2
with the patch.
|
|
That is Simple! right!
The above command would also prompt you about a reversed patch.
Reversed (or previously applied) patch detected!
This is due to the fact that file2
does not contain the difference
noted by patch_file
. Just answer y
to apply it.
This command would create a file for backup file2.orig
.
Reversing a Patch #
|
|
Here the -R
means the reversal option.
The -p0
option removes the first line.
And -i
takes in the patch file name.
This can also be written as
|
|
Both Above would prompt Unreversed Patch detected!.
Unreversed patch detected!
This is due to the fact that file2
was already patched earlier.
Just answer y
to reverse it.
Differences in Directories #
This is one useful piece to create a bundle of all changes needed.
Command For doing holistic comparison:
|
|
Command Ignoring Absent files on either directories:
|
|
Here are the option parameter details:
r
for Recursive directory spanningu
for the better user friendly output we discussed earlierN
ignore absent files Note You might want to remove this one incase you have to copy those new files as well.
This file might be huge in case you have many files. So be cautious about that.
Possibly one might need to archive the directory patch file.
Patching Directories #
|
|
This would use the directory mentioned in the patch_file
to execute the patching.
More explicit command:
|
|
This would again warn of the reversed patch. Just do the same as earlier.
Reversing a directory patch can be difficult. So make sure to keep a backup of the folder on which the patch is applied.
References #
This is not invented know-how. Just logging down what I found.
So here are the links - from wherein I dug up this info.