linux下patch文件使用

发布时间:2025-12-09 14:09:00 浏览次数:4

初识patch还是和Z同事沟通问题时认识的,回想起已经近半年。简单来说,打patch是对文件内容增增减减。由陌生到认识到熟悉,是一件愉快的事。

1. 对单个文件打patch

old.txt为原文件,new.txt 为已修改后的文件,现在要做的是:制作patch,更新old.txt文件

(1)查看文件内容

[wln@localhost 02]$ cat old.txt
123456
qwert
dasfg
fdsaf
[wln@localhost 02]$ cat new.txt
1123456
qqwert
dasfg
dsaf

(2)制作patch

[wln@localhost 02]$
diff -Naur old.txt new.txt > a.patch

[wln@localhost 02]$ cat a.patch

— old.txt 2014-06-09 15:35:34.000000000 +0800

+++ new.txt 2014-06-09 15:35:34.000000000 +0800

@@ -1,4 +1,4 @@

-123456

-qwert

+1123456

+qqwert

dasfg

-fdsaf

+dsaf

(3)diff参数解读

-N 选项确保补丁文件将正确地处理已经创建或删除文件的情况
-a 将所有文件都当做文本文件
-u 选项以统一格式创建补丁文件,这种格式比缺省格式更紧凑些
-r 是一个递归选项,设置了这个选项,diff会将两个不同版本源代码目录中的所有对应文件全部都进行一次比较,包括子目录文件

(4)执行patch

[wln@localhost 02]$ patch -p0 < a.patch
patching file old.txt

这里 -p0表示执行patch所在的路径就是制作patch所在的路径。如果为-p1表示执行patch所在的路径为制作patch所在路径的子目录下,如果为 -p2 则为子目录的子目录下。

所以最好是在哪里制作在哪里执行patch(用-p0)
[wln@localhost 02]$ cat old.txt
1123456
qqwert
dasfg
dsaf
[wln@localhost 02]$ cat new.txt
1123456
qqwert
dasfg
dsaf

(5)打好patch,发现有问题,想返回,则执行-R

[wln@localhost 02]$ patch -R -p0 < a.patch
patching file old.txt
[wln@localhost 02]$ cat old.txt
123456
qwert
dasfg
fdsaf
[wln@localhost 02]$ cat new.txt
1123456
qqwert
dasfg
dsaf

2. 多个文件打patch

(1)查看文件内容

[wln@localhost patch]$ ll
总计 8
drwxrwxr-x 3 wln wln 4096 06-09 15:48 new
drwxrwxr-x 3 wln wln 4096 06-09 15:48 old
[wln@localhost patch]$ tree
.
|– new
| `– a
| |– a.txt
| `– b
| `– foo.txt
`– old
`– a
`– b
`– foo.txt

[wln@localhost patch]$ cat old/a/b/foo.txt
old_line_1
old_line_2
12324
[wln@localhost patch]$ cat new/a/a.txt
fdfas
fafes
1233
5678
2345
[wln@localhost patch]$ cat new/a/b/foo.txt
new_line_1
new_line_2

(2)制作patch

[wln@localhost patch]$ diff -Naur old new > a.patch
[wln@localhost patch]$ cat a.patch
diff -Naur old/a/a.txt new/a/a.txt
— old/a/a.txt 1970-01-01 08:00:00.000000000 +0800
+++ new/a/a.txt 2014-06-09 15:48:45.000000000 +0800
@@ -0,0 +1,5 @@
+fdfas
+fafes
+1233
+5678
+2345
diff -Naur old/a/b/foo.txt new/a/b/foo.txt
— old/a/b/foo.txt 2014-06-09 15:48:49.000000000 +0800
+++ new/a/b/foo.txt 2014-06-09 15:48:45.000000000 +0800
@@ -1,3 +1,2 @@
-old_line_1
-old_line_2
-12324
+new_line_1
+new_line_2

(3)执行patch

[wln@localhost patch]$ cd old/
[wln@localhost old]$ patch -p1 < ../a.patch
patching file a/a.txt
patching file a/b/foo.txt
[wln@localhost old]$ cd ../
[wln@localhost patch]$ tree
.
|– a.patch
|– new
| `– a
| |– a.txt
| `– b
| `– foo.txt
`– old
`– a
|– a.txt
`– b
`– foo.txt

6 directories, 5 files
[wln@localhost patch]$ cat old/a/a.txt
fdfas
fafes
1233
5678
2345
[wln@localhost patch]$ cat old/a/b/foo.txt
new_line_1
new_line_2
[wln@localhost patch]$ cat new/a/a.txt
fdfas
fafes
1233
5678
2345
[wln@localhost patch]$ cat new/a/b/foo.txt
new_line_1
new_line_2
[wln@localhost patch]$

为什么要进入到old/下执行patch?

因为有新文件a/a.txt生成,如果直接在制作patch路径执行patch,会产生问题。

假如没有新文件生成,可以直接在制作patch的当前路径下执行patch

(4)打好patch,发现有问题,想返回,则执行-R

[wln@localhost patch]$ cd old/
[wln@localhost old]$ patch -R -p1 < ../a.patch
patching file a/a.txt
patching file a/b/foo.txt

参考:

(1)摘录:linux下打patch方法

http://blog.csdn.net/sunyubo458/article/details/6680840

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477