Linux diff命令制作patch文件

diff命令可以把两个相关文件的不同制作成patch,然后用patch命令和这个patch文件对两个原文件进行相互还原。

比如两个相关文件分别为test1.txt,test2.txt

$ cat test1.txt
this is test1 file
$ cat test2.txt
this is test2 file
$
用diff命令制作patch文件的基本格式为:$diff -Nuar test1.txt test2.txt > test.patch
  • N --new-file  把不存在的文件当做空
  • u 表示加入被比较文件的信息
  • a 表示把所有的文件都当作文本文件
  • r  递归比较
$ diff -Naur /home/test/diff/test1.txt /home/test/diff/test2.txt > test.patch
$ cat test.patch
--- /home/test/diff/test1.txt      2014-06-16 23:06:46.209546723 +0800
+++ /home/test/diff/test2.txt      2014-06-16 23:06:53.047539848 +0800
@@ -1 +1 @@
-this is test1 file
+this is test2 file
$

用patch命令与patch文件更新test1.txt到test2.txt  $patch -pnum < test.patch加上-b选项的话,就可以同时制作backup文件,num表示数字,表示拿掉几层目录的意思,其实就是拿掉几个slash(/)的意思

用patch命令还原test2.txt到test1.txt  $patch -R -pnum < test.patch

如果test.patch里面记录的相关文件信息像这样。*** /home/test/diff/test*.txt

  • 如果num等于0,表示更新的档案就是 /home/test/diff/test*.txt

  • 如果num等于1,表示更新的档案就是 home/test//diff/test*.txt

  • 如果num等于2,表示更新的档案就是 test/diff/test*.txt

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值