{"id":1878,"date":"2015-01-30T01:51:58","date_gmt":"2015-01-29T17:51:58","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1878"},"modified":"2015-01-30T01:51:58","modified_gmt":"2015-01-29T17:51:58","slug":"linux%e4%b8%8b%e7%9a%84diff%e5%92%8cpatch%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1878.html","title":{"rendered":"Linux\u4e0b\u7684diff\u548cpatch\u547d\u4ee4"},"content":{"rendered":"<h5>Linux\u4e0b\u7684diff\u548cpatch\u547d\u4ee4<\/h5>\n<h6>1.Patch\u547d\u4ee4\u7684\u57fa\u672c\u8bed\u6cd5<\/h6>\n<p>The patch command takes a patch file patchfile containing a difference listing produced by the diff program and applies those differences to one or more original files, producing patched versions. Normally the patched versions are put in place of the originals.\uff08patch\u662f\u901a\u8fc7\u7531diff\u547d\u4ee4\u4ea7\u751f\u7684\u8bb0\u5f55\u6587\u4ef6\u4e4b\u95f4\u4e0d\u540c\u4e4b\u5904\u7684\u8865\u4e01\u6587\u4ef6\uff1b\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u5728\u539f\u6587\u4ef6\u7684\u57fa\u7840\u4e4b\u4e0a\u8fdb\u884c\u4fee\u6539\u7684\uff09<\/p>\n<p>The basic syntax is as follows:<\/p>\n<pre class=\"lang:default decode:true\">$ patch &lt; patch.file\n$ patch source.code.file &lt; patch.file\n$ patch -p LEVEL &lt; {\/path\/to\/patch\/file}<\/pre>\n<p>To apply a patch, one could run the following command in a shell:<\/p>\n<pre class=\"lang:default decode:true\">$ patch &lt; \/path\/to\/file<\/pre>\n<p>In this example, patch foo.c with patch.diff file:<\/p>\n<pre class=\"lang:default decode:true\">$ patch foo.c &lt; patch.diff<\/pre>\n<p>Patches can be undone, or reversed, with the &#8216;-R&#8217; option\uff08\u4f7f\u7528\u201d-R\u201d\u9009\u9879\u64a4\u9500patch\u64cd\u4f5c\uff09:<\/p>\n<pre class=\"lang:default decode:true\">$ patch -R &lt; \/path\/to\/file<\/pre>\n<h6>2.\u521b\u5efapatch\u8865\u4e01\u6587\u4ef6<\/h6>\n<p>How do I create a patch? To create a patch, one could run the following diff command:<\/p>\n<pre class=\"lang:default decode:true\">$ diff -u oldfile-name-here newfile-name-here &gt; patch.diff<\/pre>\n<h6>3.\u5982\u4f55\u7ed9\u4e00\u4e2a\u6e90\u7801\u6811\u6253\u8865\u4e01\uff1f<\/h6>\n<p>A note about working on an entire source tree<\/p>\n<p>First, make a copy of the source tree:<\/p>\n<pre class=\"lang:default decode:true\">## Original source code is in lighttpd-1.4.35\/ directory ##\n$ cp -R lighttpd-1.4.35\/ lighttpd-1.4.35-new\/<\/pre>\n<p>Cd to lighttpd-1.4.35-new directory and make changes as per your requirements:<\/p>\n<pre class=\"lang:default decode:true\">$ cd lighttpd-1.4.35-new\/\n$ vi geoip-mod.c\n$ vi Makefile<\/pre>\n<p>Finally, create a patch with the following command:<\/p>\n<pre class=\"lang:default decode:true\">$ cd ..\n$ diff -rupN lighttpd-1.4.35\/ lighttpd-1.4.35-new\/ &gt; my.patch<\/pre>\n<p>You can use my.patch file to patch lighttpd-1.4.35 source code on a different computer\/server using patch command as discussed above:<\/p>\n<pre class=\"lang:default decode:true \">$ patch -p1 &lt; my.patch<\/pre>\n<p>&nbsp;<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/www.cyberciti.biz\/faq\/appy-patch-file-using-patch-command\/\" target=\"_blank\">http:\/\/www.cyberciti.biz\/faq\/appy-patch-file-using-patch-command\/<\/a><\/li>\n<li><a href=\"http:\/\/blog.chinaunix.net\/uid-9525959-id-2001542.html\" target=\"_blank\">Linux\u547d\u4ee4\u5b66\u4e60\u624b\u518c-patch\u547d\u4ee4<\/a><\/li>\n<li><a href=\"http:\/\/jungels.net\/articles\/diff-patch-ten-minutes.html\" target=\"_blank\">http:\/\/jungels.net\/articles\/diff-patch-ten-minutes.html<\/a><\/li>\n<li><a href=\"http:\/\/ftp.gnu.org\/gnu\/bash\/\" target=\"_blank\">http:\/\/ftp.gnu.org\/gnu\/bash\/<\/a><\/li>\n<li><a href=\"http:\/\/www.stevejenkins.com\/blog\/2014\/09\/how-to-manually-update-bash-to-patch-shellshock-bug-on-older-fedora-based-systems\/\" target=\"_blank\">http:\/\/www.stevejenkins.com\/blog\/2014\/09\/how-to-manually-update-bash-to-patch-shellshock-bug-on-older-fedora-based-systems\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u4e0b\u7684diff\u548cpatch\u547d\u4ee4 1.Patch\u547d\u4ee4\u7684\u57fa\u672c\u8bed\u6cd5 The patch command ta [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,12],"tags":[442,453],"class_list":["post-1878","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-tools","tag-diff","tag-patch"],"views":4245,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1878"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1878\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}