Wednesday, January 30, 2013

Using diff and patch

Diff is a file comparison utility that computes the differences between two files. The output of diff is called a "diff" or a "patch," since it can be applied to another utility named patch to update an old file to a new version. Here is a simple how-to on using diff and patch.

To compute the patch between OldFile and NewFile, we type:


diff -u OldFile NewFile > PatchforOld

where -u specifies that the differences is represented in unified format. The diff (patch) is stored in file PatchforOld. To apply the patch to OldFile, change to the directory where it is located and type:

patch < PatchforOld

or

patch -i PatchforOld

It is not necessary to specify the target file for a patch, which is included in the patch file generated in unified format. If something went wrong (for example, patch was  created  with  the  old  and  new  files swapped), you are able to reverse your patch (and revert to OldFile) by issuing:

patch -R -i PatchforOld


To compute the patch between two directories OldDir and NewDir, we type:

diff -urN OldDir NewDir > PatchforOldD

where -r means recursively compare any subdirectories, -N treats absent files as empty.

No comments:

Post a Comment