patch is a tool used to apply diffs or patches to files. A patch is a file that contains a set of changes to a file or a set of files, usually created by the diff command. The patch command reads a patch file and applies the changes to the corresponding files, updating them with the changes specified in the patch file. This is a very useful tool for software developers who need to apply patches to source code, but it can also be used to update configuration files, scripts, and other types of files.
Basic Syntax
patch [options] [original_file] [patch_file]
Here, original_file is the file to which the patch is to be applied, and patch_file is the file that contains the patch. If original_file is not specified, the patch is applied to the file specified in the patch file.
Commonly Used Options
-
-p n: This option removes the firstndirectories from the file names in the patch file. This is useful if the patch was generated from a different directory than the one in which you want to apply it. -
-i: This option specifies the input file, i.e., the patch file to be applied. -
-o: This option specifies the output file, i.e., the file to which the patched contents will be written. -
-R: This option reverses the patch, i.e., it unapplies the changes specified in the patch file.
Useful Examples
-
patch -p1 < patch_file: This applies the patch inpatch_fileto the files in the current directory, removing the first directory level from the file names in the patch file. -
patch -p0 < patch_file: This applies the patch inpatch_fileto the files in the current directory, without removing any directory levels from the file names in the patch file. -
patch -i patch_file -o output_file original_file: This applies the patch inpatch_filetooriginal_fileand writes the patched contents tooutput_file. -
patch -R -p1 < patch_file: This unapplies the changes specified in the patch file and restores the original files in the current directory, removing the first directory level from the file names in the patch file.
Leave a Reply