Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use patch to generate patches in Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use patch to generate patches in Linux. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

I. principle

Diff compares the differences between two files or sets of files, and records them to generate a diff file, which is also what we often call a patch file, that is, a patch file.

Patch can apply the diff file to one of the original two collections, resulting in another collection.

For example, file An and file B generate patch file C after diff, then the process is equivalent to A-B = C, then the process of patch is B-C = An or A-M C = B.

So as long as we can get any two of the three files A, B, C, we can use the tools diff and patch to generate another file.

II. Usage

1. The usage of diff

Diff can be followed by two file names or two directory names. If it is a directory name plus a file name, it only works on files with the same name in that directory.

If there are two directories, all files in that directory are not recursive. If we want to execute recursively, we need to use the-r parameter.

Command diff A B > C, generally An is the original file, B is the modified file, and C is called the patch file of A.

The diff file format generated without any parameters is a simple format that only marks a different number of lines and contents. We need a more detailed format that can identify different contexts, which is more helpful to improve the recognition of patch commands. You can use the-c switch at this time. The usual parameter is-Nuar.

2. The usage of patch

Patch is used to generate target files from the original and patch files. Or take the last example.

Patch A C gets B, which is called patching A with B's name C.

After one step, your file A becomes file B. What if you want to get back to An after the patch?

Patch-R B C can be restored to A.

So don't worry about losing A.

In fact, patch does not need to specify the original file when it is used, because the path and name of the original file have been recorded in the patch file. Patch is smart enough to recognize it. But sometimes there's a little problem. For example, when you diff two directories, you may already include the name of the original directory, but when we patch it, we will enter the directory and then use patch. At that time, you need to tell patch command how to handle the path in the patch file. You can use the-pn switch to tell the patch command the number of path delimiters ignored. Examples are as follows:

A file is under DIR_A, the modified B file is under DIR_B, and generally DIRA and DIR_B are in the same level directory. In order to diff all the files in the entire directory at once, we usually execute the following command in the parent directory of DIR_A and DIR_B

The code is as follows:

Diff-rc DIR_A DIR_B > C

At this time, patch file C will record the path of the original file as DIR_A/A.

Now another user has the A file and the C file, where the A file is in the same directory as DIR_A. In general, he would prefer to patch under the DIR_A directory, which will execute the

The code is as follows:

Patch

But at this time, patch analyzes the records in the C file and thinks that the original file is DIR_A/A, but it is actually. / A, and patch will not find the original file. To avoid this, we can use the-p1 parameter as follows

The code is as follows:

Patch-p1

At this point, patch ignores the content before the first "/" and thinks that the original file is. / A, which is correct.

Finally, there are the following points to note:

1. If you play more than one patch at a time, these patch usually have a sequence, so you have to type them in order.

two。 Do not make any changes to the original file before patch

3. If the original file recorded in patch does not match the original file version you get (it is easy to appear), then you can try using patch and, if you are lucky, succeed. In most cases, there will be mismatches, and patch will generate a rej file to record the failures, which you can modify manually.

Third, give examples

Typically, diff is used with patch. Even if you call the diff file with patch. Such as the following example:

There are two directories in old and new,old, there are f1.c _ force _ f2.c _ new _ c Now we are going to add new to old.

The code is as follows:

[fsy@localhost ~] $mkdir old new

[fsy@localhost ~] $cd old

[fsy@localhost old] $touch f1.c f2.c

# create two folders and files in old

[fsy@localhost old] $echo 1 > f1.c

[fsy@localhost old] $cat f1.c

one

[fsy@localhost old] $echo 2 > f2.c

# write content in old file

[fsy@localhost old] $cd.. / new

[fsy@localhost new] $touch f1.c f3.c

[fsy@localhost new] $echo new 1 > f1.c

[fsy@localhost new] $cat f1.c

New 1

[fsy@localhost new] $echo 3 > f3.c

# create new file and write content

[fsy@localhost new] $cd..

[fsy@localhost ~] $diff-Nuar old new > dir.diff

[fsy@localhost ~] $cat dir.diff

Diff-Nuar. / old/f1.c. / new/f1.c

-old/f1.c 2011-08-30 12 purl 32 purl 21.553737454 + 0800

+ new/f1.c 2011-08-30 12-14-8-30 12-14-4-8-12-12-12-8-12-12-12-8-12-12-12-14-8-12-12-12-14-8-12-12-14-8-12-12-12-14-8-12-12-14-12-12-12

@-1 + 1 @ @

-1

+ new 1

Diff-Nuar. / old/f2.c. / new/f2.c

-old/f2.c 2011-08-30 12 purl 32 purl 40.334207279 + 0800

+ new/f2.c 1970-01-01 08purl 0000000000000 + 0800

@ @-1 + 0prime0 @ @

-2

Diff-Nuar. / old/f3.c. / new/f3.c

-old/f3.c 1970-01-01 08VO 00.000000000 + 0800

+ new/f3.c 2011-08-30 12MU 34MU 42.331754293 + 0800

@ @-0pen 0 + 1 @ @

+ 3

# you can clearly see the differences between the files under the two folders

[fsy@localhost] $cd old.. You must go to the folder where you want to patch

[fsy@localhost old] $patch-p1 <.. / dir.diff

Patching file old/f1.c

Patching file old/f2.c

Patching file old/f3.c

[fsy@localhost old] $ls

F1.c f3.c

[fsy@localhost old] $cat f1.c

New 1

[fsy@localhost old] $cat f3.c

three

[fsy@localhost old] $

# f2.c is deleted, f1.c and f3.c are updated.

On how to use patch in Linux to generate patches to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report