linux基本命令示例_带有Linux示例的补丁命令教程

linux基本命令示例

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
Help
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
Create Patch File
Create Patch File
创建补丁文件

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
Apply Patch File
Apply Patch File
应用补丁文件

应用补丁之前先备份(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
Take Backup Before Applying Patch
Take Backup Before Applying 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
Set Backup File Version
Set Backup File Version
设置备份文件版本

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
Reverse Patch
Reverse Patch
反向补丁

As we can see that new changes are reverted back.

如我们所见,新的更改已还原。

LEARN MORE  CMake Tutorial To Build and Compile In Linux
了解更多在Linux上构建和编译的CMake教程

翻译自: https://www.poftut.com/patch-command-tutorial-with-examples-for-linux/

linux基本命令示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值