Summary of Linux patch command (Collection)

Time:2020-11-25

When it comes to the patch command, we have to mention the diff command, which is a necessary tool for making a patch. The diff command is used to create the patch file,Basically, you only need to useThe parameter “diff – NAU”, if the folder is compared, the – R parameter should be added. Therefore, the Naur parameter is generally used directly.

The basic steps of the experiment. My plan is to create a cascading directory. / X / XX / xxx /, and create two different files, xxx1 and XXX2, under the directory XXX. Then use the diff command in XXX directory to create a patch file xxx.patch , create a patch file in XX directory xx.patch Create a patch file x.patch in the X directory. Then experiment in these three directories.

Start the experiment: set up the experiment directory

[[email protected] ~]$ mkdir -pv x/xx/xxx
MKDIR: created directory ‘x’
MKDIR: created directory ‘x / XX’
MKDIR: created directory “x / XX / xxx”

Enter the XXX directory to create xxx1, XXX2

[[email protected] ~]$ cd x/xx/xxx
[[email protected] xxx]$ cat >> xxx1 << EOF
> 111111
> 111111
> EOF

[[email protected] xxx]$ cat >> xxx2 << EOF
> 111111
> 222222
> EOF

Look at these two files

[[email protected] xxx]$ diff -y xxx1 xxx2
111111                                111111
111111                           |    222222

Be sure to note: the directory where the patch is located

Create patch file in XXX directory xxx.patch , and view.
[[email protected] xxx]$ diff -Naru xxx1 xxx2 > xxx.patch
[[email protected]ora 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 @@
  111111
– 111111
+222222

Create patch file in XX directory xx.patch , and view

[[email protected] xxx]$ cd ..
[[email protected] xx]$ diff -Naru xxx/xxx1 xxx/xxx2 > xx.patch
[[email protected] xx]$ cat xx.patch
— xxx/xxx1    2009-12-19 22:28:26.582959182 +0800
+++ xxx/xxx2    2009-12-19 22:28:42.798928591 +0800
@@ -1,2 +1,2 @@
111111
-111111
+222222

Create the patch file x.patch in the X directory and view it

[[email protected] xx]$ cd ..
[[email protected] x]$ diff -Nu xx/xxx/xxx1 xx/xxx/xxx2 > x.patch
[[email protected] 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 @@
111111
-111111
+222222

Now copy the patch files to the XXX directory.

[[email protected] x]$ cp x.patch xx/xxx/
[[email protected] x]$ cp xx/xx.patch xx/xxx/

Enter the XXX directory to start the experiment

[[email protected] x]$ cd xx/xxx
[[email protected] xxx]$ ls
x.patch  xx.patch  xxx1  xxx2  xxx.patch

[[email protected] xxx]$ patch-p0< xxx.patch Modify the first file with the second patch
patching file xxx1
[[email protected] xxx]$ cat xxx1
111111
222222
[[email protected] xxx]$ patch -RE < xxx.patch #Modify the first file with the first patch
patching file xxx1
[[email protected] xxx]$ cat xxx1
111111
111111

[[email protected] xxx]$ patch -p1 < xx.patch
patching file xxx1
[[email protected] xxx]$ cat xxx1
111111
222222
[[email protected] xxx]$ patch -RE < xxx.patch
patching file xxx1
[[email protected] xxx]$ cat xxx1
111111
111111

[[email protected] xxx]$ patch -p2 < x.patch
patching file xxx1
[[email protected] xxx]$ cat xxx1
111111
222222
[[email protected] xxx]$ patch -RE < x.patch
patching file xxx1
[[email protected] xxx]$ cat xxx1
111111
111111

——————–

[[email protected] xx]$ patch-p0< xx.patch Modify the first file with the second patch
patching file xxx1
[[email protected] xxx]$ cat xxx1
111111
222222
[[email protected] xxx]$ patch -RE < xxx.patch #Modify the first file with the first patch
patching file xxx1
[[email protected] xxx]$ cat xxx1
111111
111111

[[email protected] xxx]$ patch -p1 < x.patch
patching file xxx1
[[email protected] xxx]$ cat xxx1
111111
222222
[[email protected] xxx]$ patch -RE < xxx.patch
patching file xxx1
[[email protected] xxx]$ cat xxx1
111111
111111
———————————-
[[email protected] x]$ Patch-p0 < x.patch # modify the first file with the second patch
patching file xxx1
[[email protected] xxx]$ cat xxx1
111111
222222
[[email protected] xxx]$ patch -RE < xxx.patch #Modify the first file with the first patch
patching file xxx1
[[email protected] 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 as follows:
— xx/xxx/xxx1   

P means to skip several levels of directory. Because the patch command is used under the X directory, and the XX directory is under the X directory, it is not necessary to skip any directory, but the full path of — XX / xxx / xxx1 should be used, so the value of P0 is used at this time.

be careful:No negative number is allowed after patch – PNot usedWith the P parameter, the patch command ignores any directoryDirect use of documents

[[email protected]x]$ Patch X / XX / xxx / xxx1 < x.patch? Use patch x.patch to modify the file xxx1 directly. Because the P parameter is not used, all directories in the patch file will be ignored.

As a programmer, it is necessary to understand the diff & patch command. For example, if we find that there is a bug code in a project, but we do not have the permission to submit it, then the most appropriate solution is to use the diff command to make a patch and send it to the project members. Project members can immediately know your intention through the patch command. Some people will say that it is not easier to transfer a new file directly? Don’t forget,OneThe patch file size is smaller and the transmission is fasterAnd you can clearly see what changes have been made.

Ensure that the current directory is the 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

Suppose we find that there is a bug code in the old project. Next, we copy a new directory, new, and modify the bug code here

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

Make sure that the old and new directories are in the current directory. You can use the diff command below,Do not use absolute paths, use relative paths insteadAs for the reason, you can see the end of the article

# LC_ALL=C TZ=UTC0 diff -Naur old new > foo.patch

If you don’t care about character set and time difference, you can also omit LC_ All = C TZ = utc0 environment variable:

# diff -Naur old new > foo.patch

Content from Linuxren.net

The – Naur parameter is a fixed usage. Most of the time, you can use this parameter with the diff command.

Browse 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 @@
-old_line_1
-old_line_2
+new_line_1
+new_line_2

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

The file directory structure is as follows:

#tree
demo
|– old
|   `– a
|       `– b
|           `– foo.txt
|– new
|   `– a
|       `– b
|           `– foo.txt
— foo.patch

Let’s see how to use patch to apply the patch,Note that the current directory isdemo, 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 in the foo.patch The path information in the patch file is as follows:

— old/a/b/foo.txt

It is not necessary to use the command “demo / old” under any directory, so it is not necessary to use the command “demo / old” under any directory/ foo.txt Full path, so use P0 at this time.

Take a look at the target file and you will find that the content has been changed to a new one:

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

hereIf you use it againThe patch command,The system will ask you if you want to restore, typeY reduction
# patch -p0 < foo.patch
patching file old/a/b/foo.txt
Reversed (or previously applied) patch detected!  Assume -R? [n] y

If you look at the target file, you will find that the contents have been restored to the old one:

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

If you want to specify exactlyApply Patch You can use the following command (i.e. increase n parameter)

# patch -Np0 < foo.patch

If you want to specify exactlyRestore patchYou can use the following command(It’s increasingR parameter):

# patch -Rp0 < foo.patch

Note: in this example, after each application of the patch, restore the patch yourself for future use. I won’t say much about it.

If you are not clear about the P parameter of the patch, let’s move on and change the current path:

# cd old

In this case, it should be P1 instead of P0 foo.patch The path of the file should also be changed, because the current directory is old: Linuxren.net

# patch -p1 < ../foo.patch
patching file a/b/foo.txt

becauseAt this time, we are in theUse the patch command under old, and a subdirectory level, and patch file foo.patch The path statement in is:

— old/a/b/foo.txt

That is to say, the old / part to the left of the first slash is no longer useful, which is the meaning of P1!

Continue to transform the path to the depth, test and use P2, P3 parameters in turn:

# cd a
# patch -p2 < ../../foo.patch
patching file b/foo.txt
# cd b
# patch -p3 < ../../../foo.patch
patching file foo.txt

In this case, P3 is already the deepest directory. In this case, you can omit the P parameter:

# patch < ../../../foo.patch
patching file foo.txt

in other words,Not usedWith the P parameter, the patch command ignores any directoryDirect use of documents

Next, why is it better to use a relative path instead of an absolute path when using the diff command?

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

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

In this way, when others want to apply your patch, the directory structure must be different, so they have to make a hard judgment on how many to use. In this way, it is easy to make mistakes. On the contrary, if you use relative paths, most of the time, P0 or P1 is enough and not easy to make mistakes.

The above is the summary of Linux patch command introduced by Xiaobian. I hope it can help you. If you have any questions, please leave me a message, and the editor will reply to you in time. Thank you very much for your support to the developeppaer website!