Diff and Patch : A Guide

Diff and Patch : A Guide

26 Sep 2019, 08:52am TZ +05:30
linux, manjaro
tips, tools

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.

Diff and Patch Concept Image

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:

1
2
3
4
5
# 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

1
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 #

1
diff -y file1 file2

Better Output and Understandable #

1
diff -u file1 file2

This output is also great for sharing as part of documentation.

Creating a Patch #

1
diff -u file1 file2 > patch_file

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.

1
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 noted by patch_file. Just answer y to apply it.

This command would create a file for backup file2.orig.

Reversing a Patch #

1
patch -p0 -R -i patch_file file2

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

1
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. 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:

1
diff -ru folder1/ folder2/ > patch_file

Command Ignoring Absent files on either directories:

1
diff -ruN folder1/ folder2/ > patch_file

Here are the option parameter details:

  • r for Recursive directory spanning
  • u for the better user friendly output we discussed earlier
  • N 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 #

1
patch -s -p0 < patch_file

This would use the directory mentioned in the patch_file to execute the patching.

More explicit command:

1
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.

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.