linux基本命令示例
Patch is a command that is used to apply patch files to the files like source code, configuration. Patch files holds the difference between original file and new file. In order to get the difference or patch we use diff
tool.
修补程序是用于将修补程序文件应用到源代码,配置等文件的命令。 补丁文件包含原始文件和新文件之间的差异。 为了获得差异或补丁,我们使用diff
工具。
Software is consist of a bunch of source code. The source code is developed by developers and changes in time. Getting whole new file for each change is not a practical and fast way. So distributing only changes is the best way. The changes applied to the old file and than new file or patched file is compiled for new version of software.
软件由一堆源代码组成。 源代码由开发人员开发,并且会随时间变化。 为每个更改获取完整的新文件不是一种实用且快速的方法。 因此,仅分发更改是最好的方法。 应用于旧文件的更改以及新文件或修补文件的更改将针对新版本的软件进行编译。
句法 (Syntax)
patch [options] [originalfile [patchfile]]
patch -pnum <patchfile
帮帮我 (Help)
$ patch --help
创建补丁文件(Create Patch File)
Now we will create patch file in this step but we need some simple source code with two different version. We call the source code file name as myapp.c
.
现在,我们将在此步骤中创建补丁文件,但是我们需要一些具有两个不同版本的简单源代码。 我们将源代码文件名为myapp.c
。
myapp_old.c (myapp_old.c)
#include <stdio.h>
void main(){
printf("Hi poftut");
}
myapp.c (myapp.c)
#include <stdio.h>
void main(){
printf("Hi poftut");
printf("This is new line as a patch");
}
Now we will create a patch file named myapp.patch
.
现在,我们将创建一个名为myapp.patch
的补丁文件。
$ diff -u myapp_old.c myapp.c > myapp.patch
We can print myapp.patch
file with following command
我们可以使用以下命令打印myapp.patch
文件
$ cat myapp.patch
应用补丁文件 (Apply Patch File)
Now we have a patch file and we assume we have transferred this patch file to the system which holds the old source code which is named myapp_old.patch
. We will simply apply this patch file. Here is what the patch file contains
现在我们有了一个补丁文件,并假设我们已经将该补丁文件传输到了包含旧源代码myapp_old.patch
。 我们将仅应用此补丁文件。 这是补丁文件包含的内容
- the name of the patched file修补文件的名称
- the different content不同的内容
$ patch < myapp.patch
应用补丁之前先备份(Take Backup Before Applying Patch)
One of the useful feature is taking backups before applying patches. We will use -b
option to take backup. In our example we will patch our source code file with myapp.patch
.
有用的功能之一是在应用补丁程序之前进行备份。 我们将使用-b
选项进行备份。 在我们的示例中,我们将使用myapp.patch
修补源代码文件。
$ patch -b < myapp.patch
The backup name will be the same as source code file just adding the .orig
extension. So backup file name will be myapp.c.orig
备份名称将与源代码文件相同,只是添加.orig
扩展名。 因此备份文件名为myapp.c.orig
设置备份文件版本 (Set Backup File Version)
While taking backup there may be all ready an backup file. So we need to save multiple backup files without overwriting. There is -V
option which will set the versioning mechanism of the original file. In this example we will use numbered
versioning.
进行备份时,可能已经准备好所有备份文件。 因此,我们需要保存多个备份文件而不进行覆盖。 有-V
选项,它将设置原始文件的版本控制机制。 在此示例中,我们将使用numbered
版本控制。
$ patch -b -V numbered < myapp.patch
As we can see from screenshot the new backup file is named as number like myapp.c.~1~
从屏幕快照中可以看到,新备份文件的名称类似于myapp.c.~1~
无需下载或试运行即可验证补丁文件 (Validate Patch File Without Applying or Dry run)
We may want to only validate or see the result of the patching. There is a option for this feature. We will use --dry-run
option to only emulate patching process but not change any file really.
我们可能只想验证或查看修补结果。 此功能有一个选项。 我们将使用--dry-run
选项仅模拟修补过程,而不会真正更改任何文件。
$ patch --dry-run < myapp.patch
反向补丁 (Reverse Patch)
Some times we may need to patch in reverse order. So the apply process will be in reverse. We can use -R
parameter for this operation. In the example we will patch myapp_old.c
rather than myapp.c
有时我们可能需要以相反的顺序打补丁。 因此,申请过程将是相反的。 我们可以使用-R
参数进行此操作。 在示例中,我们将修补myapp_old.c
而不是myapp.c
$ patch -R myapp_old.c < myapp.patch
As we can see that new changes are reverted back.
如我们所见,新的更改已还原。
翻译自: https://www.poftut.com/patch-command-tutorial-with-examples-for-linux/
linux基本命令示例