Linux:Patch补丁、Diff使用

文章介绍了Linux系统中的`diff`和`patch`命令,它们用于检测文件之间的差异并创建或应用补丁。`diff`用于比较文件,`patch`则将`diff`的结果应用于目标文件。文章详细解释了命令选项和用途,包括自动生成补丁文件和恢复文件至先前状态。
摘要由CSDN通过智能技术生成

what的问题

diff命令,记录两个文件的差别,通过diff得到一个patch文件,也应用patch到另外一个文件,通过patch命令

diff and patch are intended to be used on text files.

why的问题

Reason 1: diff can be useful by itself to see what has changed between files, even if you never use patch.

Reason 2: Sometimes you can get patches from third parties and apply them to your files. This can be beneficial in cases when the files being patched are large, but the number of changes is relatively small: transferring a patch file is more efficient than transferring the entire file.

Reason 3: You can apply patches to files that don't perfectly match the original file used in diff.

Diff命令

diff [options] [original filename] [changed filename]
[root@localhost patch_diff]# diff hello1 hello2
2c2
< hello
---
> h9ello
4c4
< lll
---
> ll0l

[root@localhost patch_diff]# diff -u  hello1 hello2
--- hello1	2024-03-25 22:36:35.270690767 +0800
+++ hello2	2024-03-25 22:38:45.174768675 +0800
@@ -1,5 +1,6 @@
 
-hello
+h9ello
 world
-lll
+ll0l
 llll
+gap
[root@localhost patch_diff]# diff -y hello1 hello2

hello							      |	h9ello
world								world
lll							      |	ll0l
llll								llll
							      >	gap
OptionDescription
-NIgnores absent files
-r

Recursively executes diff through a directory. Used to compare multiple files at once. Note: this is not the same as -R, which is a patch option

一次性比较多个文件

-uDisplays output in an easier to read format. This may remove some information, such as context lines.
-yForces output to display differences side by side.

阅读结果:

LetterMeaning
cContent was replaced
aContent was added or appended
dContent was deleted

上面表示,11和12行被第替换为了第二个文件的11行

Patch命令

patch is a command that takes the output from the diff and puts it into a file. Then, it can take the filed output and overwrite another file with with the changes. For example, a common use is to use the patch to transfer changes from the changed file to the original file, thus making them identical. While this can also be accomplished by copy/pasting the updated file into the original file, patch is much faster and efficient.

作用:

1、将文件的改变应用到原始文件,使得两个文件相同;

2、复制粘贴更新的文件到原文件;

-bCreates a backup of the original file
-iForces the command to read the patch from the .patch file instead of from standard input
-p[#]Instructs the command to strip # number of slashes from the filepath to the filename. You’ll see in most of our examples, we use -p0 so that no slashes are stripped away
-RReverses the previous patch
-sRuns the command silently. It will only show process if there are errors
[root@localhost patch_diff]# diff -u hello1 hello2 > patchfile.patch
[root@localhost patch_diff]# cat patchfile.patch 
--- hello1	2024-03-25 22:36:35.270690767 +0800
+++ hello2	2024-03-25 22:38:45.174768675 +0800
@@ -1,5 +1,6 @@
 
-hello
+h9ello
 world
-lll
+ll0l
 llll
+gap

Overwrite文件的改变,hello1和hello2一样了

[root@localhost patch_diff]# patch hello1 patchfile.patch
patching file hello1
[root@localhost patch_diff]# cat hello
cat: hello: 没有那个文件或目录
[root@localhost patch_diff]# cat hello1

h9ello
world
ll0l
llll
gap

回滚patch

patch -p0 -R -i patchfile.patch

您提供的命令是用于Linux系统中的补丁应用命令。这个命令使用`patch`程序来应用一个补丁文件(在您的例子中是`patchfile.patch`)到现有的文件中。下面是这个命令的详细解释:

  • - `patch`: 这是执行补丁操作的命令本身。
  • - `-p0`: 这个选项告诉`patch`程序在应用补丁时不剥离(strip)任何前导目录。通常,补丁文件会包含相对路径,`-p0`意味着不会对这些路径进行任何修改。
  • - `-R`: 这个选项指示`patch`程序反向应用补丁。这通常用于撤销补丁所做的更改。
  • - `-i`: 这个选项后面跟着的是补丁文件的名字,`patch`程序会读取这个文件来应用或撤销更改。
  • - `patchfile.patch`: 这是补丁文件的名称,`patch`程序将根据这个文件来修改目标文件。

所以,如果您在一个Linux环境中执行这个命令,它将尝试反向应用(撤销)`patchfile.patch`中包含的更改。如果您是想要正常应用补丁,而不是撤销,那么您应该去掉`-R`选项。如果您是想要应用补丁,命令应该是这样的:

```bash
patch -p0 -i patchfile.patch
```

请确保您在正确的目录中执行这个命令,且`patchfile.patch`文件存在于当前目录或者您提供了正确的路径。同时,您需要有适当的权限来修改目标文件。

[root@localhost patch_diff]# diff -p2 hello1_bak hello2_bak 
*** hello1_bak	2024-03-25 22:54:55.104445692 +0800
--- hello2_bak	2024-03-25 22:54:50.144448378 +0800
***************
*** 1,5 ****
  
! hello
  world
! lll
  llll
--- 1,6 ----
  
! h9ello
  world
! ll0l
  llll
+ gap


如果你想严格指定是 应用补丁 可以使用下面命令(就是增加N参数):

# patch -Np0 < foo.patch

如果你想严格指定是 还原补丁 可以使用下面命令(就是增加R参数):

# patch -Rp0 < foo.patch

命令 `diff -Naur old new > foo.patch` 是一个在类 Unix 系统中使用的命令行工具,用于生成一个补丁文件,该文件包含了两个目录或文件之间的差异。这个命令的各个部分具体含义如下:

- `diff`: 这是用来比较文件或目录差异的命令。

- `-N`: 这个选项告诉 `diff`,如果新文件(new)在比较中不存在,应该将其视为空文件,而不是报错。这在生成补丁时很有用,因为你可能想要更新或添加新文件。

- `-a`: 这个选项表示 "all",它告诉 `diff` 以 ASCII 格式输出结果,这样可以更好地显示二进制文件的差异(如果有的话)。

- `-u`: 这个选项表示 "unified",它会以统一格式输出差异,这种格式在应用补丁时非常有用。每个差异都会被标记为特定的行号,以及是添加(+)还是删除(-)。

- `-r`: 这个选项表示 "recursive",它会递归地比较目录中的所有文件。

- `old`: 这是你要比较的旧目录或文件的路径。

- `new`: 这是你要比较的新目录或文件的路径。

- `>`: 这是重定向操作符,它将命令的标准输出(stdout)重定向到一个文件中。

- `foo.patch`: 这是生成的补丁文件的名称。

综合起来,`diff -Naur old new > foo.patch` 命令会递归地比较 `old` 目录(或文件)和 `new` 目录(或文件)之间的差异,并将这些差异以统一格式输出到 `foo.patch` 文件中。这个补丁文件之后可以用 `patch` 命令应用到其他系统或环境中,以同步更新。

请注意,命令中的 `old` 和 `new` 应该是你想要比较的两个实体的路径。如果你想要比较的是文件而不是目录,那么这两个参数应该是文件的路径。如果你想要比较的是目录,那么这两个参数应该是目录的路径,并且你可能需要确保你在正确的父目录下执行命令。

实际问题

// TODO 待补充

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值