Detailed Linux patch command parameters and usage _linux

Source: Internet
Author: User
Tags diff first string local time mkdir

When it comes to patch commands, you have to mention the diff command, which is the necessary tool for making patch. Diff command, in the production of patch files, basically only need to use the Diff-nau this parameter, if the comparison is a folder, but also to add-r parameters, so the general direct use of naur parameters.

Feature Description: patch file.

syntax:patch [-bceeflnnrsttuvz][-b < Backup header string >][-d < working directory >][-d < symbol >][-F < Supervisor number >][-g < Control numerical >][-i < patch file >][-o < output file >][-p < Peel level >][-r < reject file >][-v < backup >][-y < backup word First string >][- Z < backup Word tail string >][--backup-if-mismatch][--binary][--help][--nobackup-if-mismatch][--verbose][original file < patch file] or path [-p < peel level] < [patch file]

Supplemental Note:The patch directive lets the user modify, update the original file by using the method of setting up the patching file. If only one file is modified at a time, the instructions can be executed directly in the instruction column. It is also one of the core upgrades to the Linux system if you can patch a large number of files one at a time with patch files.

Parameters

-B or--backup back up each original file.
-b< Backup Header string > or--prefix=< backup header string > When setting a file backup, the first string appended to the file name, which can be the path name.
-C or--context to interpret the patch data as a correlation difference.
-d< working directory > or--directory=< working directory > setting working directory.
-d< sign > or--ifdef=< symbol > mark the change place with the specified symbol.
-E or--ed to interpret the patched data into a descriptive file that can be used by the ED directive.
-E or--remove-empty-files remove the file whose contents are blank when the file is patched.
-F or--force the effect of this parameter is similar to the specified-t parameter, but assumes that the version of the patched data is a new version.
-f< number of > or--fuzz< of the number of Supervisors > set the maximum number of supervisors Bielle.
-g< controls the value > or--get=< control of numeric > Settings to RSC or SCCS control of the repair operation.
-i< patch file > or--input=< patch file > read the specified fix ask home for you.
-L or--ignore-whitespace ignores patch data with the input data of the jump, the space character.
-N or--normal to interpret the patched data into general differences.
-N or--forward ignores patched data older than the version of the original file, or the patch data for that version has been used.
The-o< output file > or--output=< output file > Sets the name of the output file, and the patched file is stored in that name.
-p< Peel level > or--strip=< Peel level > Settings to split several layers of path names.
-f< reject file > or--reject-file=< reject File > Set the name of the file to save the information that was rejected for repair, and the preset file name is. Rej.
-R or--reverse assumes that the patching data is generated by the new and old file interchange location.
-S or--quiet or--silent does not display the instruction execution procedure unless an error occurs.
-T or--batch automatically skips errors without asking for any questions.
-T or--set-time the effect of this parameter is similar to the specified-Z parameter, but local time is the primary.
-U or--unified to interpret the patching data into a consistent difference.
-V or--version display version information.
-v< Backup method > or--version-control=< backup method > Back up the destination file with the-b parameter, the end of the backup file is followed by a backup string, which can be changed not only with the-Z parameter, but also when the-v parameter is used to specify different methods of backup. Also produces a backup string at the end of a different word.
-y< Backup Header string > or--basename-prefix=--< backup header string > Set file Backup, append the first character string at the beginning of the file base name.
-z< backup Word tail string > or--suffix=< back string > The effect of this parameter is similar to the specified-b parameter, except that the path and filename used by the patch job are src/linux/fs/super.c, plus backup/ After the string, the file Super.c is backed up in the/src/linux/fs/backup directory.
-Z or--SET-UTC to make the patched file changes, access time set to UTC.
--backup-if-mismatch backs up files when patching data does not exactly match and does not deliberately specify that you want to back up files.
--binary reads and writes data in binary mode, not through standard output devices.
--help online Help.
--nobackup-if-mismatch do not back up files when patching data does not exactly match and you do not deliberately specify that you want to back up files.
--verbose displays the execution of instructions in detail.

The basic steps of the experiment. I'm going to build a cascading directory./x/xx/xxx/, create two different file xxx1,xxx2 in the XXX directory. Then in the XXX directory with the diff command, set up a patch file Xxx.patch, in the XX directory to create a patch file Xx.patch, in the X directory to create a patch file X.patch. Then experiment in these three directories.

Start experiment: Set up an experimental catalogue

[King@fedora ~]$ MKDIR-PV x/xx/xxx mkdir: Directory "x" mkdir Created: Directory "x/xx
"
mkdir: Directory "x/xx/xxx" created

Enter the XXX directory to create xxx1,xxx2

 [King@fedora ~]$ cd x/xx/xxx
 [King@fedora xxx]$ cat >> xxx1 << EOF
> 111111
 > 111111
 &G T EOF


 [King@fedora xxx]$ cat >> xxx2 << eof
 > 111111
 > 222222
 > EOF

View these two files

[King@fedora xxx]$ diff-y xxx1 xxx2 111111 111111 111111       | 222222

Be sure to note: The directory where the patch was made

Create a patch file Xxx.patch in the XXX directory and view it.

[King@fedora xxx]$ Diff-naru xxx1 xxx2 > Xxx.patch
[King@fedora xxx]$ cat Xxx.patch-- 
xxx1 2009-12-19 22 : 28:26.582959182 +0800
+++ xxx2 2009-12-19 22:28:42.798928591
 +0800 @@ -1,2
 +1,2 @@ -1,2
-111111
 +222222

Create a patch file Xx.patch in the XX directory and view

[King@fedora xxx]$ cd ...
 [King@fedora xx]$ Diff-naru xxx/xxx1 xxx/xxx2 > Xx.patch
 [King@fedora xx]$ cat Xx.patch 
---xxx/xxx1 2009-12-1 9 22:28:26.582959182 +0800
 +++ xxx/xxx2 2009-12-19 22:28:42.798928591
 +0800 @@ -1,2 +1,2 @@ -1,2
- 111111
 +222222

Create a patch file X.patch in the X directory and view

[King@fedora xx]$ cd ...
 [King@fedora x]$ diff-nu xx/xxx/xxx1 xx/xxx/xxx2 > X.patch
 [King@fedora x]$ cat X.patch 
---xx/xxx/xxx1 2009 -12-19 22:28:26.582959182 +0800
 +++ xx/xxx/xxx2 2009-12-19 22:28:42.798928591
 +0800 @@ -1,2 +1,2 @@ -1,2 111111
-111111
 +222222

Patch files are now copied to the XXX directory down.

[King@fedora x]$ CP x.patch xx/xxx/
 [King@fedora x]$ CP Xx/xx.patch xx/xxx/

Enter the XXX directory to start the experiment

[King@fedora x]$ cd xx/xxx
 [king@fedora xxx]$ ls
 x.patch xx.patch xxx1 xxx2 xxx.patch

 [King@fedora xxx]$ PATC h-p0< xxx.patch #用第二个的 Patch modifies first file
patching file xxx1
 [King@fedora xxx]$ cat xxx1 111111 222222
 [ King@fedora xxx]$ Patch-re < xxx.patch #用第一个的 Patch modifies the first file
patching file xxx1
 [King@fedora xxx]$ cat xxx1
   
    111111
 111111

 [King@fedora xxx]$ patch-p1 < xx.patch
patching file xxx1
 [King@fedora xxx]$ Cat Xxx1
 111111
 222222
 [King@fedora xxx]$ patch-re < xxx.patch
 patching file xxx1
 [ King@fedora xxx]$ cat xxx1
 111111
 111111

 [King@fedora xxx]$ patch-p2 < x.patch patching
file Xxx1
 [King@fedora xxx]$ cat xxx1
 111111
 222222
 [King@fedora xxx]$ patch-re < X.patch
 Patching file xxx1
 [King@fedora xxx]$ cat xxx1
 111111
 111111


   

------------------------------------------------------------

[King@fedora xx]$ Patch-p0 < Xx.patch # Modify the first file with a second patch
patching file xxx1
 [King@fedora xxx]$ cat xxx1
 11 1111
 222222
 [King@fedora xxx]$ patch-re < xxx.patch #用第一个的 Patch modifies the first file
patching file xxx1
 [King@fedor A xxx]$ cat xxx1
 111111
 111111

 [King@fedora xxx]$ patch-p1 < x.patch patching
file xxx1
 [ King@fedora xxx]$ cat xxx1
 111111
 222222
 [King@fedora xxx]$ patch-re < xxx.patch patching
 file Xxx1
 [King@fedora xxx]$ cat xxx1
 111111
 111111

--------------------------------------------------------------------------

[King@fedora x]$ patch-p0< X.patch # modifies the first file with a second patch
patching file xxx1
 [King@fedora xxx]$ cat xxx1
 11 1111
 222222
 [King@fedora xxx]$ patch-re < xxx.patch #用第一个的 Patch modifies the first file
patching file xxx1
 [king@f Edora xxx]$ cat xxx1
 111111
 111111

The only thing that needs to be explained here is the meaning of p0, because the path information in the X.patch patch file is this:
---xx/xxx/xxx1

P indicates skipping a few levels, because it is the patch command used in the X directory, the XX directory is in the X directory, so you do not have to skip any directory, but should use the---xx/xxx/xxx1 full path, so this time using P0.

Note: Patch-p cannot be followed by a negative number. When the P parameter is not used, the patch command ignores any directory and uses the file directly.

[King@fedora x]$ Patch x/xx/xxx/xxx1 < x.patch # modifies file X.patch directly with patch xxx1, because no p parameter is used, so all directories in the patch file are ignored.

As a programmer, it is necessary to understand the Diff&patch command. For example, we find that a project has bug code, and we do not have the right to submit, then the most appropriate solution is to use the diff command to do a patch to the project members. Project members can immediately know your intentions through the patch command. Someone would say it's not simpler to send a new file directly? Don't forget, a patch file size is smaller and faster, and you can obviously see what changes have been made.

Ensure the current directory is a demo directory:

# mkdir Demo
 # CD Demo

First simulate a project directory old:

# mkdir-p old/a/b
 # vi old/a/b/foo.txt
 old_line_1
 old_line_2

Let's say we've found the bug code for the old project, so let's start by copying a new one and modifying the bug code here:

# cp-r old New
 # vi new/a/b/foo.txt
 new_line_1
 new_line_2

Guaranteed old and new two directories are in the current directory, the following can use the diff command, do not use absolute path, but should use relative path, as for the reason, see the end of the article you are clear:

# lc_all=c TZ=UTC0 diff-naur old New > Foo.patch

You can omit lc_all=c tz=utc0 environment variables If you are not in the character set, jet lag, and so on:

# Diff-naur Old New > Foo.patch

Content from Linuxren.net

Where the-naur parameter is a fixed usage, most of the time, this parameter is used when using the diff command.

Look around the patch file:

# cat Foo.patch
 diff-naur old/a/b/foo.txt new/a/b/foo.txt
---old/a/b/foo.txt  2009-12-07 20:40:07.000000000 +0800
 +++ new/a/b/foo.txt  2009-12-07 20:41:51.000000000
 +0800 @@ -1,2 +1,2 @@ -1,2 -old_line_1
-old_line_2
 +new_line_1
 +new_line_2

Add a minus sign after the content is useful content, other content is convenient for you to check the relevant information content, patch production completed.

The file directory structure at this point is probably as follows:

#tree
 Demo | |
 '--a
 |  '--B
 |   '--foo.txt  |--| '--B
 |   '--foo.txt-- 
Foo.patch

Below see how to use patch to apply a patch, note that the current directory is demo, try the following command:

# Patch-p0 < foo.patch
 Patching file Old/a/b/foo.txt

The only thing that needs to be explained here is the meaning of p0, because the path information in the Foo.patch patch file is this:

---old/a/b/foo.txt

P to skip a few levels of directory, because it is in the demo directory to use the patch command, the old directory is in the demo directory, so do not have to skip any directory, and should use old/a/b/foo.txt full path, so at this time using the P0.

Looking at the target file, you will find that the content has been modified to a new one:

# cat Old/a/b/foo.txt
 new_line_1
 new_line_2

At this point if you use the patch command again, the system asks you if you want to restore, enter y restore:

# Patch-p0 < foo.patch
 patching file Old/a/b/foo.txt reversed
 (or previously applied) patch detected! Assume-r? [N] Y

Looking at the target file, you will find that the content has been restored to the old:

# cat Old/a/b/foo.txt
 old_line_1
 old_line_2

If you want to strictly specify that the application patch can use the following command (that is, increase the n parameter):

# Patch-np0 < Foo.patch

If you want to strictly specify that the restore patch can use the following command (that is, add the R parameter):

# Patch-rp0 < Foo.patch

Note: In this example, every time you apply a patch, you restore the patch, for later use to continue testing, I will not say more.

See here if you're not quite sure about the P parameter of patch, then look down and we'll change the current path:

# CD Old

At this point should be P1, rather than p0, the reference foo.patch file path to the relative change, because the current directory is old: linuxren.net

 # Patch-p1 < ... /foo.patch
 Patching file A/b/foo.txt

Because at this point we are using the patch command in old, and a subdirectory peer, and the path declaration in patch file Foo.patch is:

---old/a/b/foo.txt

That is to say the first slash left old/part is useless, this is the meaning of P1!

Continue to the depth transformation path, and then test the use of the P2,P3 parameter:

# CD A

 # PATCH-P2 <. /.. /foo.patch
 Patching file b/foo.txt

 # CD b

 # PATCH-P3 <. /.. /.. /foo.patch
 Patching file Foo.txt

In this case, the P3 is already the deepest directory, and the P argument can be omitted at this point:

# Patch < ... /.. /.. /foo.patch
 Patching file Foo.txt

That is, when the P parameter is not used, the patch command ignores any directory and uses the file directly.

The following article said earlier why use the diff command is best not to use absolute path, but should use relative path?

A: If you use the absolute path when using diff, the file path information in the patch file will look something like the following:

---/a/b/c/d/e/f/g/bar.txt

This way, when people want to apply your patch, because the directory structure is certainly different, so you have to struggle to determine the end of the use of P. This makes it easy to make mistakes, and conversely, if you use relative paths, most of the time, P0 or P1 is enough to make mistakes.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.