现在的位置: 首页 > 综合 > 正文

linux diff patch的应用

2013年08月03日 ⁄ 综合 ⁄ 共 1168字 ⁄ 字号 评论关闭

 对diff,patch的用法总是一知半解

在这里做个标记

diff [options] oldfile newfile
[options]
-r:递归选项,包含子目录文件
-u:以统一格式创建补丁文件,标准格式
-N:正确处理新创建或已删除文件的情况

cd ../old_dir
diff -urN old_dir new_dir >patchfile
例如:
$ cd ~/work
$ ls
  olduboot test temp
$ ls test

   newuboot
$ diff -urN olduboot test/newuboot >ubootpatch
注:diff时,应该是在olduboot的同级目录下进行。

patch [options] [originalfile] [patchfile]
[options]
-p0:从当前目录查找目标文件(夹)
-p1:忽略第一次目录,从当前目录开始查找
-E:若发现空文件,则删除
-R:在补丁文件中的“新”文件和“旧”文件调换过来。

例如:ubootpatch格式如下:
$ pwd
  ~/work #当前目录
$ head -3 ubootpatch
diff -urN olduboot/arch/arm/cpu/arm926ejs/sc8800g/check_reboot.c test/newuboot/arch/arm/cpu/arm926ejs/sc8800g/check_reboot.c
--- olduboot/arch/arm/cpu/arm926ejs/sc8800g/check_reboot.c      2011-09-27 14:55:50.000000000 +0800
+++ test/newuboot/arch/arm/cpu/arm926ejs/sc8800g/check_reboot.c 2011-09-27 14:25:25.000000000 +0800

-p0:表示从当前目录~/work下查找一个叫做olduboot的文件夹
-p1:表示忽略掉第一层目录(olduboot),从当前目录~/work下查找arch文件夹,继而遍历查找到check_reboot.c文件
在这里如果要用参数p1,则需要先进入olduboot文件夹中,再运行:patch -p1 < ubootpatch

##然后在需要应用patch的地方使用下述命令即可:
$ patch -p0 < ubootpatch
or
$ cd olduboot
$ patch -p1 < ../ubootpatch

如果你需要打补丁的文件夹olduboot在其他目录,如:~/temp/olduboot
则需要如下操作:
$ cd ~/work/ubootpatch ~/temp/
$ cd ~/temp
$ ls
  olduboot
$ patch -p0 <ubootpatch
or
$ cd olduboot
$ patch -p1 <../ubootpatch

 

抱歉!评论已关闭.