Http://www.cnblogs.com/tureno/articles/3748195.html
Linux applications: Linux upgrades under the package and patching production, in the process of doing development will inevitably need to the kernel and download some of the source code to play patches or upgrade, so we learn to use the diff in linux to make patches and how to use patch to play patches is particularly important.
Preface
In the process of doing development will inevitably need to the kernel and download some of the source code to play patches or upgrades, so we learn to use the diff in linux to make patches and how to use patch to play patches is particularly important.
diff and Patch Command introduction
1, diff command
NAME
Diff-find differences between two files
Synopsis
diff [Options] from-file To-file
--------------------
Simply put, the diff function is used to compare two files, and then record down, that is, the so-called diff patch.
Syntax Format: diff "option" Source file (folder) purpose file (folder), is to give the source file (folder) to make a patch to become the destination file (folder), the term is "upgrade."
Here are three of the most common options:
-R is a recursive option, set this option, diff will compare all the corresponding files in the two different versions of the source code directory, including subdirectory files.
The-n option ensures that the patch file will correctly handle situations where files have 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 command
------------------
NAME
Patch-apply a diff file to a original
Synopsis
patch [Options] [Originalfile [Patchfile]]
But usually just
Patch-pnum
------------------
Simply put, patch is the use of diff made patches to achieve the source file (folder) and the destination file (folder) conversion. This means that you can have the source file (folder) ――> destination file (folder), can also be the destination file (folder) ――> source files (folders).
Here are some of the most common options:
-P0 option to find the destination file (folder) from the current directory
The-P1 option is to ignore the first level directory and start looking from the current directory.
Here is an example to illustrate:
---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 look for a folder called old from the current directory and find the pcitable file under modules below it to perform the patch operation.
If you use parameter-p1, that means ignoring the first level directory (that is, regardless of old), looking for the Modules folder from the current directory, and looking for pcitable underneath it. This assumes that the current directory must be the directory where the modules resides. The diff patch file can be anywhere, as long as it indicates the path to the diff patch file. Of course, you can use a relative path, or you can use an absolute path. But I am generally accustomed to using a relative path.
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 being swapped (in effect, patching the new version to the old version)
The following combination of specific examples to analyze and solve, divided into two types: for a single file patch and a folder for a number of files to play patches.
Environment: Log on to Ubuntu users below ubuntu9.10.
The directory tree is as follows:
|--Bootloader
| |--DEBUG
|--Images
|--Kernel
|--Program
|--Rootfiles
|--Software
| |-source
|--Sysapps
| |-tmp
'--tools
Below the program folder to establish the patch folder as an experiment, and then enter the Patch folder.
Patch a single file
1, the establishment of test documents TEST0, Test1
[Ubuntu@likui patch]$ Cat >>test0<
> 111111
> 111111
> 111111
> EOF
[Ubuntu@likui patch]$ more Test0
111111
111111
111111
[Ubuntu@likui patch]$ Cat >>test1<
> 222222
> 111111
> 222222
> 111111
> EOF
[Ubuntu@likui patch]$ more Test1
222222
111111
222222
111111
2, use diff to create a patch Test1.patch
[Ubuntu@likui 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. 】
[Ubuntu@likui patch]$ ls
Test0 test1 Test1.patch
[Ubuntu@likui patch]$ more Test1.patch
Structure of the patch file
? Patch Head
The patch header is two lines that begin with the---/+++ to indicate the file to be patched. The beginning of the---represents the old file, and +++ begins with the new file.
Multiple patches in a patch file
A patch file may contain many sections that begin with a---/+++, and each section is used to make a patch. So you can include multiple patches in a patch file.
? Block
Block is the place to modify in the patch. It usually starts and ends with something that doesn't have to be modified. They are only used to indicate the location to be modified. They usually start with @@ 开始 ending with the start of another block or a new patch header.
? Indentation of Blocks
Blocks are indented, and this column is used to indicate whether the line is to be added or deleted.
? The first column of the Block
The + number 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 indicates that it is only referenced and does not need to be modified.
The diff command records the first creation time of these two files in the 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
[Ubuntu@likui patch]$ Patch-p0 < Test1.patch
Patching file Test0
[Ubuntu@likui patch]$ ls
Test0 test1 Test1.patch
[Ubuntu@likui patch]$ Cat Test0
222222
111111
222222
111111
3, can remove the patch, restore the old version
[Ubuntu@likui patch]$ Patch-re-p0 < Test1.patch
Patching file Test0
[Ubuntu@likui patch]$ ls
Test0 test1 Test1.patch
[Ubuntu@likui patch]$ Cat Test0
111111
111111
111111
Patch for multiple files
1. Create a Test folder
[Ubuntu@likui patch]$ mkdir prj0
[Ubuntu@likui patch]$ CP test0 prj0
[Ubuntu@likui patch]$ ls
Prj0 test0 test1 Test1.patch
[Ubuntu@likui patch]$ CD prj0/
[Ubuntu@likui prj0]$ ls
Test0
[Ubuntu@likui prj0]$ Cat >>prj0name<
>--------
> Prj0/prj0name
>--------
> EOF
[Ubuntu@likui prj0]$ ls
Prj0name test0
[Ubuntu@likui prj0]$ Cat Prj0name
--------
Prj0/prj0name
--------
[Ubuntu@likui prj0]$ CD ...
[Ubuntu@likui patch]$ mkdir prj1
[Ubuntu@likui patch]$ CP test1 PRJ1
[Ubuntu@likui patch]$ CD prj1
[Ubuntu@likui prj1]$ Cat >>prj1name<
>---------
> Prj1/prj1name
>---------
> EOF
[Ubuntu@likui prj1]$ Cat Prj1name
---------
Prj1/prj1name
---------
[Ubuntu@likui prj1]$ CD ...
2, create a patch
[Ubuntu@likui patch]$ diff-unr prj0 prj1 > Prj1.patch
[Ubuntu@likui 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
[Ubuntu@likui patch]$ ls
Prj0 prj1 prj1.patch test0 test1 Test1.patch
[Ubuntu@likui patch]$ cp Prj1.patch./prj0
[Ubuntu@likui patch]$ CD Prj0
[Ubuntu@likui prj0]$ Patch-p1 < Prj1.patch
Patching file Prj0name
Patching file Prj1name
Patching file Test0
Patching file Test1
[Ubuntu@likui prj0]$ ls
Prj1name Prj1.patch Test1
[Ubuntu@likui prj0]$ patch-r-p1 < Prj1.patch
Patching file Prj0name
Patching file Prj1name
Patching file Test0
Patching file Test1
[Ubuntu@likui prj0]$ ls
Prj0name Prj1.patch test0
Summary
? 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
Application
Patching the kernel
1, decompression
Because the released patch files are compressed using gzip.
$gunzip. /setup-dir/patch-2.4.21-rmk1.gz
2, then enter your kernel source code directory
$CD linux-2.4.21
3, play the patch
$patch –p1 < ... /.. /setup-dir/patch-2.4.21-rmk1