patch命令用于修补文件。该命令读取如何更改文件的源文件指示信息,然后应用这些更改。源文件包含由diff命令产生的产别列表。差别列表是比较两个文件和构建关于如何纠正差别指示信息的结果。在默认情况下,patch命令使用从标准输入读入源文件。
升级文件:
如果要将文件testfile升级,补丁为testfile.patch,该文件为testfile升级前和修改后文件通过diff命令生成的补丁文件。使用patch为testfile打包的命令为:
- ywx@ywx:~/desktop/test/test1$ cat testfile1
-
hello,this is the first file.
-
ywx@ywx:~/desktop/test/test1$ cat testfile2
-
hello,this is the second file.
-
ywx@ywx:~/desktop/test/test1$ diff testfile1 testfile2
-
1c1
-
< hello,this is the first file.
-
---
-
> hello,this is the second file.
-
ywx@ywx:~/desktop/test/test1$ diff testfile1 testfile2 > testfile.patch
-
ywx@ywx:~/desktop/test/test1$ cat testfile.patch
-
1c1
-
< hello,this is the first file.
-
---
-
> hello,this is the second file.
-
ywx@ywx:~/desktop/test/test1$ patch testfile1 < testfile.patch 使用补丁包升级testfile1文件
-
patching file testfile1
-
ywx@ywx:~/desktop/test/test1$ cat testfile1
-
hello,this is the second file.
-
ywx@ywx:~/desktop/test/test1$
我们可以使用两种方式来 打补丁
- ywx@ywx:~/desktop/test/test1$ cat testfile3
-
hello,this is the first file.
-
linux
-
ywx@ywx:~/desktop/test/test1$ patch testfile3 < testfile.patch
-
patching file testfile3
-
ywx@ywx:~/desktop/test/test1$ cat testfile3
-
hello,this is the second file.
-
linux
-
ywx@ywx:~/desktop/test/test1$ cat testfile4
-
hello,this is the first file.
-
linux
-
ywx@ywx:~/desktop/test/test1$ patch -p0 testfile4 testfile.patch
-
patching file testfile4
-
ywx@ywx:~/desktop/test/test1$ cat testfile4
-
hello,this is the second file.
-
linux
-
ywx@ywx:~/desktop/test/test1$
如果要打补丁的文件和patch不符合,补丁将失败
- ywx@ywx:~/desktop/test/test1$ cat testfile5
-
hi,this is the first file.
-
linux
-
ywx@ywx:~/desktop/test/test1$ patch -p0 testfile5 testfile.patch
-
patching file testfile5
-
Hunk #1 FAILED at 1.
-
1 out of 1 hunk FAILED -- saving rejects to file testfile5.rej 失败
-
ywx@ywx:~/desktop/test/test1$
阅读(1467) | 评论(0) | 转发(0) |