The making and application of patch under Linux

Source: Internet
Author: User
Tags diff diff patch

transferred from:http://blog.chinaunix.net/u3/100239/showart_1984963.html

First introduce diff and patch. There is no need to introduce all the options on the man online document. In 99% of the time, we will only use a few options. So you have to learn these few options.
1. diff
--------------------
NAME
Diff-find differences between the files
Synopsis
diff [Options] from-file To-file
--------------------

Simply put, the diff function is used to compare the two files, and then record, that is, the so-called diff patch. Syntax format: diff "Options" source file (folder) destination file (folder), is to give the source file (clip) patch, make it into the destination file (folder), the term is "upgrade." The three most common options are described below:


-R is a recursive option that sets this option, and diff will compare all of the corresponding files in the two different versions of the source code directory, including the subdirectory files.
The-n option ensures that the patch file will correctly handle the case where the file has been created or deleted.
The-u option creates a patch file in a uniform format that is more compact than the default format.

2. Patch
------------------
NAME
Patch-apply a diff file to an original
Synopsis
patch [Options] [Originalfile [Patchfile]]
But usually just
Patch-pnum <patchfile>
------------------
Simply put, patch is the use of diff-made patches to achieve the source file (clip) and destination file (clip) conversion. This means that you can have the source file (clip) ――> destination file (clip), or the destination file (clip) ――> source file (clip). Here are a few of the most common options:
-P0 option to find the destination file (clip) from the current directory
-P1 option to ignore the first level of the directory, starting from the current directory to find.
************************************************************
Here is an example description:
---old/modules/pcitable Mon Sep 27 11:03:56 1999
+ + + new/modules/pcitable Tue Dec 19 20:05:41 2000
If you use the parameter-p0, it means to find a folder called old from the current directory, and under it look for the pcitable file under modules to perform the patch operation.
If you use parameter-p1, it means ignoring the first-level directory (that is, old), looking for the Modules folder from the current directory, and looking for pcitable under it. This assumes that the current directory must be the directory where the modules is located. The diff patch file can be anywhere, as long as the path of the diff patch file is indicated. Of course, you can use a relative path, or you can use an absolute path. But I'm generally used to using relative paths.
************************************************************

The-e option indicates that if an empty file is found, delete it
The-r option indicates that the "new" file and the "old" file in the patch file are now switched over (in effect, the new version is patched to make it the old version)
The following examples are analyzed and resolved in two types: patching a single file and patching multiple files within a folder.
Environment: Redhat 9.0 under the Armlinux user login.
The directory tree is as follows:
|--bootloader
|--Debug
|--Images
|--kernel
|--Program
|--Rootfiles
|--Software
|--Source
|--Sysapps
|--tmp
|--Tools
Below the program folder, create a patch folder for experimentation, and then go to the Patch folder.
One, for a single file patch operation
1, the establishment of test documents TEST0, Test1
[email protected] patch]$ cat >>test0<<eof
> 111111
> 111111
> 111111
> EOF
[Email protected] patch]$ more test0
111111
111111
111111
[email protected] patch]$ cat >>test1<<eof
> 222222
> 111111
> 222222
> 111111
> EOF
[Email protected] patch]$ more test1
222222
111111
222222
111111
2. Create a patch using diff test1.patch
[Email protected] patch]$ Diff-un test0 test1 > Test1.patch
Note: the-r option is not required because of a single file. The order of options is not related, that is, it can be-un or-nu. 】
[[email protected] patch]$ ls
Test0 test1 Test1.patch
[Email protected] patch]$ more Test1.patch
************************************************************
The structure of the patch file
Patch Header
Patch headers are two lines that start with the---/+++, and are used to indicate the file to be patched. The---begins with the old file, + + + starts with the new file.
Multiple patches in a patch file
A patch file may contain many sections beginning with the---/+++, each of which is used to make a patch. So you can include multiple patches in a patch file.
Block
The block is the place to be modified in the patch. It usually starts and ends with something that doesn't have to be modified. They are simply used to indicate the location to be modified. They usually start with @@ 开始 end at the beginning of another block or a new patch header.
Indentation of Blocks
The block is indented one column, and this column is used to indicate whether the line is to be increased or deleted.
The first column of the Block
The + sign indicates that the line is to be added.
-the number indicates that the line is to be deleted.
There is no plus sign and no minus sign to indicate that this is just a reference without modification.
************************************************************
The diff command records the first creation time of these two files in a patch file, as follows * * *
---test0 2006-08-18 09:12:01.000000000 +0800
+ + + test1 2006-08-18 09:13:09.000000000 +0800
@@ -1,3 +1,4 @@
+222222
111111
-111111
+222222
111111
[Email protected] patch]$ Patch-p0 < Test1.patch
Patching file Test0
[[email protected] patch]$ ls
Test0 test1 Test1.patch
[email protected] patch]$ cat Test0
222222
111111
222222
111111
3, can remove the patch, restore the old version
[Email protected] patch]$ Patch-re-p0 < Test1.patch
Patching file Test0
[[email protected] patch]$ ls
Test0 test1 Test1.patch
[email protected] patch]$ cat Test0
111111
111111
111111
Second, for multiple files to patch operation
1. Create a Test folder
[Email protected] patch]$ mkdir prj0
[email protected] patch]$ CP test0 prj0
[[email protected] patch]$ ls
Prj0 test0 test1 Test1.patch
[Email protected] patch]$ CD prj0/
[[email protected] prj0]$ ls
Test0
[email protected] prj0]$ cat >>prj0name<<eof
>--------
> Prj0/prj0name
>--------
> EOF
[[email protected] prj0]$ ls
Prj0name test0
[email protected] prj0]$ cat Prj0name
--------
Prj0/prj0name
--------
[Email protected] prj0]$ CD.
[Email protected] patch]$ mkdir prj1
[email protected] patch]$ CP test1 PRJ1
[Email protected] patch]$ CD prj1
[email protected] prj1]$ cat >>prj1name<<eof
>---------
> Prj1/prj1name
>---------
> EOF
[email protected] prj1]$ cat Prj1name
---------
Prj1/prj1name
---------
[Email protected] prj1]$ CD.

2. Create Patches
[Email protected] patch]$ diff-unr prj0 prj1 > Prj1.patch
[Email protected] patch]$ more Prj1.patch
Diff-unr Prj0/prj0name Prj1/prj0name
---prj0/prj0name 2006-08-18 09:25:11.000000000 +0800
+ + + prj1/prj0name 1970-01-01 08:00:00.000000000 +0800
@@ -1,3 +0,0 @@
---------
-prj0/prj0name
---------
Diff-unr Prj0/prj1name Prj1/prj1name
---prj0/prj1name 1970-01-01 08:00:00.000000000 +0800
+ + + prj1/prj1name 2006-08-18 09:26:36.000000000 +0800
@@ -0,0 +1,3 @@
+---------
+prj1/prj1name
+---------
Diff-unr prj0/test0 Prj1/test0
---prj0/test0 2006-08-18 09:23:53.000000000 +0800
+ + + prj1/test0 1970-01-01 08:00:00.000000000 +0800
@@ -1,3 +0,0 @@
-111111
-111111
-111111
Diff-unr Prj0/test1 Prj1/test1
---prj0/test1 1970-01-01 08:00:00.000000000 +0800
+ + + prj1/test1 2006-08-18 09:26:00.000000000 +0800
@@ -0,0 +1,4 @@
+222222
+111111
+222222
+111111
[[email protected] patch]$ ls
Prj0 prj1 prj1.patch test0 test1 Test1.patch
[email protected] patch]$ CP Prj1.patch./prj0
[Email protected] patch]$ CD prj0
[Email protected] prj0]$ PATCH-P1 < Prj1.patch
Patching file Prj0name
Patching file Prj1name
Patching file Test0
Patching file Test1
[[email protected] prj0]$ ls
Prj1name Prj1.patch Test1
[Email protected] prj0]$ Patch-r-P1 < Prj1.patch
Patching file Prj0name
Patching file Prj1name
Patching file Test0
Patching file Test1
[[email protected] prj0]$ ls
Prj0name Prj1.patch test0
-------------------------------------------------
To summarize:
Single File
Diff–un from-file To-file >to-file.patch
Patch–p0 < To-file.patch
Patch–re–p0 < To-file.patch

Multiple files
Diff–unr FROM-DOCU TO-DOCU >to-docu.patch
Patch–p1 < To-docu.patch
PATCH–R–P1 <to-docu.patch

The making and application of patch under Linux

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.