Patch (not excerpted)

Source: Internet
Author: User
Patch
Apply the patch file to file 1 and change it to another file 2 (you must first generate the patch file with "Diff file 1 file 2 ).
Syntax
Patch [Option] [original file [patch file]

Description:
The patch command reads instructions on how to change the source file and then applies these changes. The source file contains the list of differences (or diff list) generated by the diff command ). The difference list is the result of comparing two files and building instructions on how to correct the difference. By default, the patch command uses the source file read from the standard input, but the-I flag and patchfile variables can overwrite this setting.
The differential list has three formats: Normal, context, or Ed editor style. The patch command determines the format of the differential list, unless it is rejected by the-C,-E, or-N flag.
By default, the patch version of the file replaces the original version. When the-B flag is specified, the original file of each patch file is saved in the file with the same name, but the suffix. orig is appended to the file name. You can use the-O flag to specify the output destination.

Common options:
-R is a recursive option. With this option set, diff compares all the corresponding files in the source code directories of two different versions, including subdirectory files.
-N option ensures that the patch file correctly processes the files that have been created or deleted.
-U option creates patch files in a uniform format, which is more compact than the default format.
-P0 option: Find the target file (folder) from the current directory (directly use the path specified in the patch file)
-The P1 option ignores the first-level directory and searches from the current directory (removes the 1st '/' At the far left of the path specified by the patch file and all the preceding content ).
-E indicates that if an empty file is found, delete it.
-The "r" option indicates that the "new" and "old" files in the patch file need to be replaced now (in fact, the new version is patched to make it an old version)

Example:

** For a single file:
First, the content of the two files is shown as follows:
$ Cat test0
00000000
00000000
00000000
$ Cat test1
00000000
11111111
00000000

* Patch generation:
$ Diff-UN test0 test1> test1.patch
In this way, the test1 patch file will be generated through comparison. Here, the "U" table shows that outputs in the same format are easy to read and modify. N indicates that nonexistent files are considered as empty files. Even if test0 does not exist, patches are generated.

* Patch test0 to the test1 file:
$ Patch-P0 Prj1.patch
Here, the option u table shows that output in the same format is easy to read and modify. N indicates that non-existing files are treated as empty, and r indicates recursive comparison of sub-directories, the comparison result is redirected to the prj1.patch file.
After running, a patch is output, describing the differences between the two files. This patch is to patch the first file of the diff parameter into the patch file of the Second file.
The actual process compares the files with the same name in two directories in sequence. If-N is not added here, it indicates that prj0name and test0 only exist in prj0, and prj1name and test1 only exist in prj1, therefore, the-n option is added for comparison.
For ease of understanding, the content of the prj1.patch file is provided here:
$ Cat prj1.patch
Diff-unr prj0/prj0name prj1/prj0name
--- Prj0/prj0name 10:44:19. 000000000 + 0800
++ Prj1/prj0name 08:00:00. 000000000 + 0800
@-+ @@
---------
-
-Prj0/prj0name
-
---------
Diff-unr prj0/prj1name prj1/prj1name
--- Prj0/prj1name 08:00:00. 000000000 + 0800
++ Prj1/prj1name 10:45:05. 000000000 + 0800
@-+ @@
+ ---------
+
+ Prj1/prj1name
+
+ ---------
Diff-unr prj0/test0 prj1/test0
--- Prj0/test0 11:21:12. 000000000 + 0800
++ Prj1/test0 08:00:00. 000000000 + 0800
@-+ @@
-0000000
-0000000
-0000000
-0000000
-0000000
-0000000
-0000000
Diff-unr prj0/test1 prj1/test1
--- Prj0/test1 1970-01-01 08:00:00. 000000000 + 0800
++ Prj1/test1 11:21:33. 000000000 + 0800
@-+ @@
+ 1111111
+ 1111111
+ 1111111
+ 1111111
+ 1111111
+ 1111111
+ 1111111

* Patch all files in prj0 to all files in prj1:
The procedure is as follows:
1) $ CP prj1.patch./prj0
2) $ CD prj0
3) $ patch-p1

  • Previous: gzip (not excerpted)
  • Next article: Sort (not excerpted)
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.