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:
diffis a tool to create a summary of changes needed
patchis a tool to apply those summary of changes needed
Note for Manjaro(Arch Linux) Users
By default the install might not have the
So its better to install if needed:
# Find out if the 'diff' and 'patch' are available pacman -Q diffutils patch # If not list out then Install them sudo pacman -S diffutils patch
Difference between Files
Here is the command
diff file1 file2
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
diff -y file1 file2
Better Output and Understandable
diff -u file1 file2
This output is also great for sharing as part of documentation.
Creating a Patch
diff -u file1 file2 > patch_file
patch_file would be used to modify the respective files.
From the earlier example we created the
Let’s now modify the
file2 with the patch.
patch file2 patch_file
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
patch_file. Just answer
y to apply it.
This command would create a file for backup
Reversing a Patch
patch -p0 -R -i patch_file file2
-R means the reversal option.
-p0 option removes the first line.
-i takes in the patch file name.
This can also be written as
patch -p0 -R file2 patch_file
Both Above would prompt Unreversed Patch detected!.
Unreversed patch detected!
This is due to the fact that
file2 was already patched earlier.
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:
diff -ru folder1/ folder2/ > patch_file
Command Ignoring Absent files on either directories:
diff -ruN folder1/ folder2/ > patch_file
Here are the option parameter details:
rfor Recursive directory spanning
ufor the better user friendly output we discussed earlier
Nignore 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.
patch -s -p0 < patch_file
This would use the directory mentioned in the
to execute the patching.
More explicit command:
patch -s -p0 folder2/ patch_file
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.
This is not invented know-how. Just logging down what I found.
So here are the links - from wherein I dug up this info.