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

发布了169 篇原创文章 · 获赞 199 · 访问量 42万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览