用diff和patch生成patch并应用

本文介绍了如何利用diff生成patch文件,以及如何将这个patch应用到原文件上,实现快速更新。

当我们想在原文件和新文件之间生成patch,并用这个patch文件,应用在原文件上快速完成更新时,可以使用以下步骤:

  • 用diff生成patch
    $ echo "12345\ntesting"> old_file 
    $ echo "125testing\ntesting1234"> new_file
    $ # We have 2 common ways to get the patch
    $ # The first way is :
    $ diff old_file new_file > file_patch
    $ # We can see the patch are generated by lines
    $ cat file_patch
    1c1
    < 12345\ntesting
    ---
    > 125testing\ntesting1234
    $ # The second way is :
    $ diff -c old_file new_file > c_file_patch
    $ # Then we can see the patch are generated by contents
    $ cat c_file_patch
    *** old_file    2018-12-27 21:06:57.922518425 +0800
    --- new_file    2018-12-27 21:07:11.551401751 +0800
    ***************
    *** 1 ***
    ! 12345\ntesting
    --- 1 ----
    ! 125testing\ntesting1234
    $ # We also can get one whole patch for files of the whole directories
    $ mkdir -p new_dir old_dir
    $ echo "123" > new_dir/test_1.txt
    $ echo "789testing" > new_dir/test_2.txt
    $ echo "356" > old_dir/test_1.txt
    $ echo "35678" > old_dir/test_2.txt
    $ diff -c -r old_dir/ new_dir > dir_patch
    $ # let use the output
    $ cat dir_patch
    diff -c -r old_dir/test_1.txt new_dir/test_1.txt
    *** old_dir/test_1.txt  2018-12-27 21:55:38.654094697 +0800
    --- new_dir/test_1.txt  2018-12-27 21:55:18.719294247 +0800
    ***************
    *** 1 ****
    ! 356
    --- 1 ----
    ! 123
    diff -c -r old_dir/test_2.txt new_dir/test_2.txt
    *** old_dir/test_2.txt  2018-12-27 21:55:48.594995187 +0800
    --- new_dir/test_2.txt  2018-12-27 21:55:29.630185028 +0800
    ***************
    *** 1 ****
    ! 35678
    --- 1 ----
    ! 789testing
    diff -c -r old_dir/test_1.txt new_dir/test_1.txt
    *** old_dir/test_1.txt  2018-12-27 21:55:38.654094697 +0800
    --- new_dir/test_1.txt  2018-12-27 21:55:18.719294247 +0800
    ***************
    *** 1 ****
    ! 356
    --- 1 ----
    ! 123
    diff -c -r old_dir/test_2.txt new_dir/test_2.txt
    *** old_dir/test_2.txt  2018-12-27 21:55:48.594995187 +0800
    --- new_dir/test_2.txt  2018-12-27 21:55:29.630185028 +0800
    ***************
    *** 1 ****
    ! 35678
    --- 1 ----
    ! 789testing

     

  • 应用patch改变原来的文件
    $ # patch single file
    $ patch old_file -i c_file_patch 
    patching file old_file
    $ diff old_file new_file 
    $ # No difference
    $ # patch multiple files for directories
    $ patch -i dir_patch -p0
    patching file old_dir/test_1.txt
    patching file old_dir/test_2.txt
    $ diff -c -r old_dir/ new_dir/
    $ # No difference
    $ # Let us see how the '-p' means here
    $ # Let us prepare one directoy
    $ tree old_dir/
    old_dir/
    |-- sub_1
    |   |-- sub_2
    |   |   `-- sub_4
    |   |       `-- test_4.txt
    |   |-- sub_3
    |   |   `-- test_3.txt
    |   `-- test_2.txt
    `-- test_1.txt
    
    4 directories, 4 files
    $ tree new_dir/
    new_dir/
    |-- sub_1
    |   |-- sub_2
    |   |   |-- sub_4
    |   |   |   `-- test_4.txt
    |   |   `-- test_5.txt
    |   |-- sub_3
    |   |   `-- test_3.txt
    |   `-- test_2.txt
    `-- test_1.txt
    
    4 directories, 5 files
    $ # get the patch
    $ diff -c -r old_dir/ new_dir/
    diff -c -r old_dir/sub_1/sub_2/sub_4/test_4.txt new_dir/sub_1/sub_2/sub_4/test_4.txt
    *** old_dir/sub_1/sub_2/sub_4/test_4.txt        2018-12-27 22:19:20.479215655 +0800
    --- new_dir/sub_1/sub_2/sub_4/test_4.txt        2018-12-27 22:21:53.496338977 +0800
    ***************
    *** 1 ****
    ! 123457878786dddddd
    --- 1 ----
    ! 12d3457878786dddddd
    Only in new_dir/sub_1/sub_2: test_5.txt
    diff -c -r old_dir/sub_1/test_2.txt new_dir/sub_1/test_2.txt
    *** old_dir/sub_1/test_2.txt    2018-12-27 22:18:15.191596826 +0800
    --- new_dir/sub_1/test_2.txt    2018-12-27 22:21:26.359494452 +0800
    ***************
    *** 1 ****
    --- 1,2 ----
      123456
    + helloworld
    diff -c -r old_dir/test_1.txt new_dir/test_1.txt
    *** old_dir/test_1.txt  2018-12-27 22:17:45.537770449 +0800
    --- new_dir/test_1.txt  2018-12-27 22:21:14.283563638 +0800
    ***************
    *** 1 ****
    ! 123
    --- 1 ----
    ! 123dddd
    $ # Let us see without the '-p' parameter
    $ patch -i dir_patch 
    can not find file to patch at input line 4
    Perhaps you should have used the -p or --strip option?
    The text leading up to this was:
    --------------------------
    |diff -c -r old_dir/sub_1/sub_2/sub_4/test_4.txt new_dir/sub_1/sub_2/sub_4/test_4.txt
    |*** old_dir/sub_1/sub_2/sub_4/test_4.txt       2018-12-27 22:19:20.479215655 +0800
    |--- new_dir/sub_1/sub_2/sub_4/test_4.txt       2018-12-27 22:21:53.496338977 +0800
    --------------------------
    File to patch: 
    $ # It shows us to use the '-p' parameter
    $ patch -i dir_patch -p0
    patching file old_dir/sub_1/sub_2/sub_4/test_4.txt
    patching file old_dir/sub_1/test_2.txt
    patching file old_dir/test_1.txt
    $ # Let us confirm the result
    $ diff -c -r old_dir/ new_dir/
    Only in new_dir/sub_1/sub_2: test_5.txt
    $ # We can see the existing files are patched. Patch does not create new files
    $ # Why we use 'p0' here?
    $ # Because we create patches and apply patches under the same path
    $ # If we do not do all thing under the same path, we need to specify the '-p' parameters. Let us see the following example 
    $ diff -r -c hello/old_dir/sub_1/test_2.txt hello/new_dir/sub_1/test_2.txt
    *** hello/old_dir/sub_1/test_2.txt      2018-12-27 22:33:01.610511164 +0800
    --- hello/new_dir/sub_1/test_2.txt      2018-12-27 22:21:26.359494452 +0800
    ***************
    *** 1 ****
    --- 1,2 ----
      123456
    + helloworld
    $ diff -r -c hello/old_dir/sub_1/test_2.txt hello/new_dir/sub_1/test_2.txt > p_dir_patch
    $ cp p_dir_patch hello
    $ cd hello
    $ patch -i p_dir_patch -p1
    patching file old_dir/sub_1/sub_2/sub_4/test_4.txt
    patching file old_dir/sub_1/test_2.txt
    $ # Here we use the 'p1', because we need to omit the first slash 'hello'. We have been on the 'hello' directory
    
     

     

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值