patch命令的使用

  1. 对单个文件打patch
    old.txt为原文件,new.txt 为已修改后的文件,现在要做的是:制作patch,更新old.txt文件
    (1)查看文件内容
    [wln@localhost 02] catold.txt123456qwertdasfgfdsaf[wln@localhost02] cat new.txt
    1123456
    qqwert
    dasfg
    dsaf
    (2)制作patch
    [wln@localhost 02] diffNaurold.txtnew.txt>a.patch[wln@localhost02] 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] patchp0<a.patchpatchingfileold.txtp0patchpatchp1patchpatchp2patchp0[wln@localhost02] cat old.txt
    1123456
    qqwert
    dasfg
    dsaf
    [wln@localhost 02] catnew.txt1123456qqwertdasfgdsaf5patchR[wln@localhost02] patch -R -p0 < a.patch
    patching file old.txt
    [wln@localhost 02] catold.txt123456qwertdasfgfdsaf[wln@localhost02] cat new.txt
    1123456
    qqwert
    dasfg
    dsaf

  2. 多个文件打patch
    (1)查看文件内容
    [wln@localhost patch] ll8drwxrwxrx3wlnwln4096060915:48newdrwxrwxrx3wlnwln4096060915:48old[wln@localhostpatch] 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] catold/a/a.txtfdfasfafes123356782345[wln@localhostpatch] cat old/a/b/foo.txt
new_line_1
new_line_2
[wln@localhost patch] catnew/a/a.txtfdfasfafes123356782345[wln@localhostpatch] cat new/a/b/foo.txt
new_line_1
new_line_2
[wln@localhost patch] old/patcha/a.txtpatchpatchpatchpatch4patchR[wln@localhostpatch] cd old/
[wln@localhost old]$ patch -R -p1 < ../a.patch
patching file a/a.txt
patching file a/b/foo.txt

转自:http://blog.csdn.net/huguangshanse00/article/details/35780081

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值