初识patch还是和Z同事沟通问题时认识的,回想起已经近半年。简单来说,打patch是对文件内容增增减减。由陌生到认识到熟悉,是一件愉快的事。
old.txt为原文件,new.txt 为已修改后的文件,现在要做的是:制作patch,更新old.txt文件
[wln@localhost 02] cat old.txt 123456 qwert dasfg fdsaf [wln@localhost 02] cat new.txt 1123456 qqwert dasfg dsaf
[wln@localhost 02]diff -Naur old.txt new.txt > a.patch [wln@localhost 02]
-N 选项确保补丁文件将正确地处理已经创建或删除文件的情况 -a 将所有文件都当做文本文件 -u 选项以统一格式创建补丁文件,这种格式比缺省格式更紧凑些 -r 是一个递归选项,设置了这个选项,diff会将两个不同版本源代码目录中的所有对应文件全部都进行一次比较,包括子目录文件
[wln@localhost 02]$ patch -p0 < a.patch patching file old.txt
这里 -p0表示执行patch所在的路径就是制作patch所在的路径。如果为-p1表示执行patch所在的路径为制作patch所在路径的子目录下,如果为 -p2 则为子目录的子目录下。
所以最好是在哪里制作在哪里执行patch(用-p0) [wln@localhost 02] cat old.txt 1123456 qqwert dasfg dsaf [wln@localhost 02] cat new.txt 1123456 qqwert dasfg dsaf
[wln@localhost 02] patch -R -p0 < a.patch patching file old.txt [wln@localhost 02]
[wln@localhost patch] ll 总计 8 drwxrwxr-x 3 wln wln 4096 06-09 15:48 new drwxrwxr-x 3 wln wln 4096 06-09 15:48 old [wln@localhost patch] 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]
[wln@localhost patch] diff -Naur old new > a.patch [wln@localhost 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] cat old/a/a.txt fdfas fafes 1233 5678 2345 [wln@localhost patch] cat old/a/b/foo.txt new_line_1 new_line_2 [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 [wln@localhost patch]
为什么要进入到old/下执行patch?
因为有新文件a/a.txt生成,如果直接在制作patch路径执行patch,会产生问题。
假如没有新文件生成,可以直接在制作patch的当前路径下执行patch
[wln@localhost patch] cd old/ [wln@localhost old] patch -R -p1 < ../a.patch patching file a/a.txt patching file a/b/foo.txt
(1)摘录:linux下打patch方法
http://blog.csdn.net/sunyubo458/article/details/6680840
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/198613.html原文链接:https://javaforall.cn