diff 和 patch 雙劍合璧

來源:互聯網
上載者:User

標籤:diff patch

diff可以比較兩個檔案的不同,也可以比較兩個檔案夾的不同,並可以產生補丁檔案

# 準備倆不同的檔案[[email protected] tmp]# cat oldA:Hello,Good morningB:Hello,How are you? I am Hai Meimei.A:Fine. Tank you, and you?B:I am fine too.[[email protected] tmp]# cat newA:Hello,Good morningB:Hello,How are you? I am Hai Meimei.A:Fine. Thank you, and you? I am Li Lei.B:I am fine too.# 預設顯示格式是--normal,可預設[[email protected] tmp]# diff old new3c3        # 兩個檔案第三行不同,分別是什麼< A:Fine. Tank you, and you?---> A:Fine. Thank you, and you? I am Li Lei.# 比對結果重新導向輸出到檔案就是一個補丁檔案,可以這個補丁檔案給老檔案打補丁[[email protected] tmp]# diff old new > 1.patch[[email protected] tmp]# cat 1.patch 3c3< A:Fine. Tank you, and you?---> A:Fine. Thank you, and you? I am Li Lei.# -c --context 以上下文格式,顯示的比較全,但也比較繁多[[email protected] tmp]# diff -c old new*** old2014-08-14 03:45:34.524343092 +0800--- new2014-08-14 03:45:42.268157302 +0800****************** 1,4 ****  A:Hello,Good morning  B:Hello,How are you? I am Hai Meimei.! A:Fine. Tank you, and you?  B:I am fine too.--- 1,4 ----  A:Hello,Good morning  B:Hello,How are you? I am Hai Meimei.! A:Fine. Thank you, and you? I am Li Lei.  B:I am fine too.  # -u --unified 合并的上下文,簡潔明了多了[[email protected] tmp]# diff -u old new--- old2014-08-14 03:45:34.524343092 +0800+++ new2014-08-14 03:45:42.268157302 +0800@@ -1,4 +1,4 @@ A:Hello,Good morning B:Hello,How are you? I am Hai Meimei.-A:Fine. Tank you, and you?+A:Fine. Thank you, and you? I am Li Lei. B:I am fine too.  # 每種風格做出的補丁打上去的結果都必須是一樣的 [[email protected] tmp]# cp old old.bak[[email protected] tmp]# diff -c old new > 2.patch[[email protected] tmp]# cat 2.patch *** old2014-08-14 03:45:34.524343092 +0800--- new2014-08-14 03:45:42.268157302 +0800****************** 1,4 ****  A:Hello,Good morning  B:Hello,How are you? I am Hai Meimei.! A:Fine. Tank you, and you?  B:I am fine too.--- 1,4 ----  A:Hello,Good morning  B:Hello,How are you? I am Hai Meimei.! A:Fine. Thank you, and you? I am Li Lei.  B:I am fine too.[[email protected] tmp]# diff -u old new > 3.patch[[email protected] tmp]# cat 3.patch --- old2014-08-14 03:45:34.524343092 +0800+++ new2014-08-14 03:45:42.268157302 +0800@@ -1,4 +1,4 @@ A:Hello,Good morning B:Hello,How are you? I am Hai Meimei.-A:Fine. Tank you, and you?+A:Fine. Thank you, and you? I am Li Lei. B:I am fine too.[[email protected] tmp]# patch old 1.patch patching file old[[email protected] tmp]# cat oldA:Hello,Good morningB:Hello,How are you? I am Hai Meimei.A:Fine. Thank you, and you? I am Li Lei.B:I am fine too.[[email protected] tmp]# diff old new[[email protected] tmp]# rm -f old[[email protected] tmp]# cp old.bak old[[email protected] tmp]# patch old 2.patch patching file old[[email protected] tmp]# diff old new[[email protected] tmp]# rm -f old[[email protected] tmp]# cp old.bak old[[email protected] tmp]# patch old 3.patch patching file old[[email protected] tmp]# diff old new[[email protected] tmp]#

對整個檔案夾打補丁

diff 命令對檔案夾做對比會分別對兩個檔案夾中的每一個檔案做對比,最後產生的是一個補丁檔案,打補丁的時候則依次應用到每一個檔案。

# 對於有子檔案夾的需要用-r --recusive 進行遞迴比較[[email protected] tmp]# diff -ru test test1# 注意這裡的結構,有用patch打補丁時對於有子目錄的情況要加 -p | --strip說明要跳過幾個‘/’# 來尋找,如果用-p0 則在目前的目錄下先找test目錄,然後依次向下找dir1目錄,如果是 -p1 則跳過# 第1個‘/’直接在目前的目錄下找dir1,然後接著往下找。diff -ru test/dir1/file1 test1/dir1/file1--- test/dir1/file12014-08-14 07:18:42.486913887 +0800+++ test1/dir1/file12014-08-14 06:58:16.393590115 +0800@@ -1 +1 @@-file 1 +file 1 newdiff -ru test/oldfile test1/oldfile--- test/oldfile2014-08-14 07:18:42.486913887 +0800+++ test1/oldfile2014-08-14 07:04:54.662944704 +0800@@ -1,2 +1,2 @@-我是一個兵。+我是一個槍騎兵。 i am a soldier.diff -ru test/testfile test1/testfile--- test/testfile2014-08-14 07:18:42.486913887 +0800+++ test1/testfile2014-08-14 03:44:11.330355773 +0800@@ -1,4 +1,4 @@ A:Hello,Good morning B:Hello,How are you? I am Hai Meimei.-A:Fine. Tank you, and you?+A:Fine. Thank you, and you? I am Li Lei. B:I am fine too.[[email protected] tmp]# diff -ru test test1 > test.patch# 切到test目錄,-p1 跳過第1個‘/‘從dir1開始[[email protected] tmp]# cd test[[email protected] test]# patch -p1 < ../test.patchpatching file dir1/file1patching file oldfilepatching file testfile[[email protected] test]# cd ..[[email protected] tmp]# diff -ru test test1[[email protected] tmp]# rm -rf test/*[[email protected] tmp]# cp -r testbak/* test/[[email protected] tmp]# cd test# 重新來一次,但是這次不加-p1預設的就是-p0了,分類樹對不上對應的子目錄下的檔案補丁就打不上了 [[email protected] test]# patch < ../test.patchcan‘t find file to patch at input line 4Perhaps you should have used the -p or --strip option?The text leading up to this was:--------------------------|diff -ru test/dir1/file1 test1/dir1/file1|--- test/dir1/file12014-08-14 07:18:42.486913887 +0800|+++ test1/dir1/file12014-08-14 06:58:16.393590115 +0800--------------------------File to patch: Skip this patch? [y] ySkipping patch.1 out of 1 hunk ignoredpatching file oldfilepatching file testfile[[email protected] test]# diff -ru ../test ../test1diff -ru ../test/dir1/file1 ../test1/dir1/file1--- ../test/dir1/file12014-08-14 07:20:54.180727280 +0800+++ ../test1/dir1/file12014-08-14 06:58:16.393590115 +0800@@ -1 +1 @@-file 1 +file 1 new# 如果做補丁檔案的時候用的是全路徑如下,那麼打補丁時再切到test下的時候就注意要-p3略過2個‘/’也就是路過tmp和test兩級目錄[[email protected] tmp]# diff -ru /tmp/test /tmp/test1diff -ru /tmp/test/dir1/file1 /tmp/test1/dir1/file1--- /tmp/test/dir1/file12014-08-14 07:34:23.189151910 +0800+++ /tmp/test1/dir1/file12014-08-14 06:58:16.393590115 +0800@@ -1 +1 @@-file 1 +file 1 newdiff -ru /tmp/test/oldfile /tmp/test1/oldfile--- /tmp/test/oldfile2014-08-14 07:34:23.189151910 +0800+++ /tmp/test1/oldfile2014-08-14 07:04:54.662944704 +0800@@ -1,2 +1,2 @@-我是一個兵。+我是一個槍騎兵。 i am a soldier.diff -ru /tmp/test/testfile /tmp/test1/testfile--- /tmp/test/testfile2014-08-14 07:34:23.189151910 +0800+++ /tmp/test1/testfile2014-08-14 03:44:11.330355773 +0800@@ -1,4 +1,4 @@ A:Hello,Good morning B:Hello,How are you? I am Hai Meimei.-A:Fine. Tank you, and you?+A:Fine. Thank you, and you? I am Li Lei. B:I am fine too. [[email protected] test]# patch -p3 < ../test2.patch patching file dir1/file1patching file oldfilepatching file testfile[[email protected] tmp]# diff -ru test test1[[email protected] tmp]#

patch 不僅可以把老版本檔案升級成新版本檔案,同樣可以降級回去,逆生長,咱這一點也不示弱

-R | --reverse 加-R參數就可以實現逆生長

-E | --remove-empty-files 如果遇到空檔案則直接刪除,如果需要的話

# 今年32明年23,逆生長成功[[email protected] test]# patch -p1 -R < ../test.patchpatching file dir1/file1patching file oldfilepatching file testfile[[email protected] test]# diff -ru ../test ../test1diff -ru ../test/dir1/file1 ../test1/dir1/file1--- ../test/dir1/file12014-08-14 11:40:40.985576969 +0800+++ ../test1/dir1/file12014-08-14 06:58:16.393590115 +0800@@ -1 +1 @@-file 1 +file 1 newdiff -ru ../test/oldfile ../test1/oldfile--- ../test/oldfile2014-08-14 11:40:40.985576969 +0800+++ ../test1/oldfile2014-08-14 07:04:54.662944704 +0800@@ -1,2 +1,2 @@-我是一個兵。+我是一個槍騎兵。 i am a soldier.diff -ru ../test/testfile ../test1/testfile--- ../test/testfile2014-08-14 11:40:40.985576969 +0800+++ ../test1/testfile2014-08-14 03:44:11.330355773 +0800@@ -1,4 +1,4 @@ A:Hello,Good morning B:Hello,How are you? I am Hai Meimei.-A:Fine. Tank you, and you?+A:Fine. Thank you, and you? I am Li Lei. B:I am fine too.


本文出自 “不懂IT的中醫不是好IT” 部落格,請務必保留此出處http://zhishen.blog.51cto.com/1612050/1540681

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.