Linux uses patch and diff commands to Create File Patches

Source: Internet
Author: User
Tags diff patch file diff

Linux uses the patch and diff commands to create a file patch. Because during the u-boot porting process, there are several common files to be modified. It is too troublesome to manually modify them each time. Patch creation can solve this problem. The collection of learning materials is relatively simple. The method 1 is similar to this kind of elementary problem. There are a lot of online materials. google or baidu searches for valuable materials and then selects valuable materials. The method 2 is to read man's online documents. To complete the collection, of course, we will eventually conduct an experiment on our own Linux system, make a summary, and digest and absorb your own things. To remove such a wrong idea, you must learn it all. You need to know that it is impossible to learn all at once. You can only learn the most common ones first. In the future, you will gradually enrich your learning experience and eventually reach a relatively high level. The principle is to apply what you have learned on a daily basis to promote learning. Www.2cto.com first introduces diff and patch. All options in the man online document will not be described here, which is unnecessary. In the 99% period, we only use a few options. Therefore, you must learn these options. 1. diff ------------------ NAME diff-find differences between two filesSYNOPSIS diff [options] from-file to-file -------------------- simply put, diff is used to compare two files, then record it, the so-called diff patch. Syntax format: diff [Option] source file (folder) Destination File (folder), that is, to patch the source file (folder) to make it into the destination file (folder ), the term is "Upgrade ". The following describes the three most 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. 2. patch ------------------ NAME patch-apply a diff file to an ordered patch [options] [originalfile [patchfile] but usually just www.2cto.com patch-pnum <patchfile> ---------------- simply put, patch is a patch created by diff to convert the source file (folder) and target file (folder. This means you can have the source file (folder)> the target file (folder) or the target file (folder)> The source file (folder ). The following describes some of the most common options:-p0 option to find the target file (folder)-p1 option from the current directory to ignore the first-level directory and start searching from the current directory. **************************************** * ******************* Here, the instance description: --- old/modules/pcitable Mon Sep 27 11:03:56 1999 ++ new/modules/pcitable Tue Dec 19 20:05:41 2000 if the parameter-p0 is used, it indicates finding a folder named old from the current directory and searching for the pcitable file under modules under it to perform the patch operation. If the-p1 parameter is used, ignore the first-level directory (regardless of the old directory), find the modules folder from the current directory, and find pcitable under it. The premise is that the current directory must be the directory where modules is located. The diff patch file can be located anywhere, as long as the path of the diff patch file is specified. Of course, you can use relative paths or absolute paths. However, I usually use relative paths. **************************************** * *******************-E Option indicates that if an empty file is found, delete it. The-R option indicates that the "new" file and "old" file in the patch file are to be replaced now (in fact, the new version is patched to make it an old version) the following examples are used to analyze and solve the problem. There are two types: patching a single file and patching multiple files in the folder. Environment: log on with an armlinux user under RedHat 9.0. The directory tree is as follows: www.2cto.com | -- bootloader | -- debug | -- images | -- kernel | -- program | -- rootfiles | -- software | -- source | -- sysapps | -- tmp '-- tools is created under the program Folder the patch folder is used as an experiment, then go to the patch folder.
1. patch a single file. 1. Create the test files test0 and test1 [armlinux @ lqm patch] $ cat> test0 <EOF> 111111> 111111> EOF [armlinux @ lqm patch] $ more test011111111111111 [armlinux @ lqm patch] $ cat> test1 <EOF> 222222> 111111> 222222> EOF [armlinux @ lqm patch] $ more use diff to create a patch test1.patch [armlinux @ lqm patch] $ diff-uN test0 test1> test1.patch [Note: the-r option is not required because of a single file. There is no relation between the option sequence, that is, it can be-uN or-Nu .] Www.2cto.com [armlinux @ lqm patch] $ lstest0 test1 test1.patch [armlinux @ lqm patch] $ more test1.patch ******************** **************************************** the patch file's structure patch header contains two lines starting with ---/++, indicates the file to be patched. --- The beginning indicates the old file, and the beginning of ++ indicates the new file. One patch file contains multiple patches. One patch file may contain many sections starting with ---/++. Each section is used for a patch. Therefore, a patch file can contain many patches. Blocks are the parts to be modified in the patch. It usually starts and ends with something that does not need to be modified. They are only used to indicate the location to be modified. They usually start with @ and end with another block or a new patch header. The indention block of the block is reduced into a column, which indicates whether to add or delete the row. The first column + number of the block indicates that this row is to be added. -Indicates that this row is to be deleted. There is no plus sign or minus sign, indicating that this is just referenced and does not need to be modified. **************************************** * ********************** The diff Command records the first creation time of the two files in the patch file, *** --- test0 09:12:01. 000000000 + 0800 ++ test1 09:13:09. 000000000 + 0800 @-222222 + 111111 @ + 111111 222222-111111 + [armlinux @ lqm patch] $ patch-p0 <test1.patchpatching file test0 [armlinux @ lqm patch] $ lstest0 test1 test1.patch [armlinux @ lqm patch] $ cat test022222211111122221111113. patches can be removed, recover the old version of [armlinux @ lqm patch] $ patch-RE-p0 <test1.patchpatching file test0 [armlinux @ lqm patch] $ lstest0 test1 test1.patch [armlinux @ lqm patch] $ cat test01111 www.2cto.com 111111111111 2. Patch multiple files
1. Create a test folder [armlinux @ lqm patch] $ mkdir prj0 [armlinux @ lqm patch] $ cp test0 prj0 [armlinux @ lqm patch] $ lsprj0 test0 test1 test1.patch [armlinux @ lqm patch] $ cd prj0/[armlinux @ lqm prj0] $ lstest0 [armlinux @ lqm prj0] $ cat> prj0name <EOF> --------> prj0/prj0name> --------> EOF [armlinux @ lqm prj0] $ lsprj0name test0 [armlinux @ lqm prj0] $ cat prj0name -------- prj0/prj0name -------- [armlinux @ lqm prj0] $ cd .. [armlinux @ Lqm patch] $ mkdir prj1 [armlinux @ lqm patch] $ cp test1 prj1 [armlinux @ lqm patch] $ cd prj1 [armlinux @ lqm prj1] $ cat> prj1name <EOF> ---------> prj1/prj1name> ---------> EOF [armlinux @ lqm prj1] $ cat prj1name --------- prj1/prj1name ------- [armlinux @ lqm prj1] $ cd .. 2. Create a patch [armlinux @ lqm patch] $ diff-uNr prj0 prj1> prj1.patch [armlinux @ lqm patch] $ more prj1.patch diff-uNr prj0/prj0name prj1/prj0name --- p Limit 0/prj0name 09:25:11. 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 09:26:36. 000000000 + 0800 @-+ @ + --------- + prj1/prj1name + --------- diff-uNr prj0/test0 prj1/test0 --- prj0/test0 2006-08-18 09: 23: 53. 000000000 + 0800 ++ prj1/test0 08:00:00. 000000000 + 0800 @-+ @-111111-111111diff-uNr prj0/test1 prj1/test1 --- prj0/test1 08:00:00. 000000000 + 0800 ++ prj1/test1 09:26:00. 000000000 + 0800 @-222222 + 111111 @ www.2cto.com + 222222 + 111111 + + [armlinux @ lqm patch] $ lsprj0 prj1 prj1.patch test0 test1 test1.patch [armlinux @ lqm patch] $ cp prj1.patch. /Prj0 [armlinux @ lqm patch] $ cd prj0 [armlinux @ lqm prj0] $ patch-p1 <your file contains file test0patching file test1 [armlinux @ lqm prj0] $ lsprj1name prj1.patch test1 [armlinux @ lqm prj0] $ patch-R-p1 <prj1.patchpatching file contains file test0patching file test1 [armlinux @ lqm prj0] $ lsprj0name prj1.patch test0 ---------------- --- Summary: single file diff-uN from-file to-file> to-file.patchpatch-p0 <to-file.patchpatch-RE-p0 <to-file.patch multiple files www.2cto.com diff-uNr from-docu to-docu> to-docu.patchpatch-p1 <to-docu.patchpatch-R-p1 <to-docu.patch ------------------- 3. The application patches the kernel. When creating a cross-compilation toolchain, one step is to patch the kernel. 1. decompress the package because the released patch files are compressed using gzip. $ Gunzip .. /setup-dir/patch-2.4.21-rmk1.gz2, and then enter your kernel source code directory $ cd linux-2.4.213, patching $ patch-p1 <.. /.. /setup-dir/patch-2.4.21-rmk1 after the patch, you need to check whether there is a file to reject the execution, that is, check. the rej file exists. Run the command $ find.-name *. rej to output it to the standard output terminal. The default screen is displayed. Of course, you can also use redirection to output to a specified file, such as reject. $ Fine.-name *. rej> reject can then view the reject content. Author: kongweile

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.