手机看片精品高清国产日韩,色先锋资源综合网,国产哺乳奶水91在线播放,乱伦小说亚洲色图欧洲电影

幫助中心 >  行業資訊 >  云計算 >  Linux中的文件對比工具diffc、mp、patch

Linux中的文件對比工具diffc、mp、patch

2025-02-25 10:34:05 1397

1、diff的語法和參數


 參數就不做翻譯了,舉幾個生產環境中最常用的例子說明


[root@achao liqingzhao]# diff --help
 Usage: diff [OPTION]... FILES
 Compare FILES line by line.
 截取部分展示  
 -i, --ignore-case                          ignore case differences in file contents  
 -E, --ignore-tab-expansion         ignore changes due to tab expansion 
  -Z, --ignore-trailing-space         ignore white space at line end  
  -b, --ignore-space-change         ignore changes in the amount of white space  
  -w, --ignore-all-space                 ignore all white space  
  -B, --ignore-blank-lines              ignore changes where lines are all blank  
  -I, --ignore-matching-lines=RE  ignore changes where all lines match RE


 案例展示

    案例1

測試用例
[root@achao liqingzhao]# cat test1.txt
 libai
 dufu
 wangwei
 baijuyi
 [root@achao liqingzhao]# cat test2.txt 
 libai
 dufu
 taoyuanming
 baijuyi

     不使用任何參數直接比較兩個文件

[root@achao liqingzhao]# diff test1.txt test2.txt
 3c3
 < wangwei
 ---
 > taoyuanming


 案例2

測試用例
[root@achao liqingzhao]# cat test1.txt
 libai
 hello dufu
 wangwei
 baijuyi
 [root@achao liqingzhao]# cat test2.txt
  libai
  hello        dufu
  taoyuanming
  baijuyi

    

使用-b選項忽略一行中的空白差異

[root@achao liqingzhao]# diff b test1.txt test2.txt 

3c3

< wangwei

---

> taoyuanming


 案例3 

測試用例1
[root@achao liqingzhao]# cat test1.txt
libai

hello dufu
wangwei
baijuyi


測試用例2
[root@achao liqingzhao]# cat test2.txt 
libai
hello        dufu
taoyuanming
baijuyi


  使用-B選項忽略空白行

[root@achao liqingzhao]# diff -Bb test1.txt test2.txt 
4c3
< wangwei
---
> taoyuanming

    生產環境中【-b】和【-B】參數還是蠻重要的嘞!


 案例4

測試用例[root@achao liqingzhao]# cat test1.txt 
libai
hello dufu
[root@achao liqingzhao]# cat test2.txt
 LIBAI
 hello        dufu


 使用-i忽略大小寫

[root@achao liqingzhao]# diff -i test1.txt test2.txt 
2c2
< hello dufu
---
> hello        dufu
[root@achao liqingzhao]# diff -ib test1.txt test2.txt 
[root@achao liqingzhao]#


 選項【-i】使用場景不是很多,因為Linux中配置文件中對大小寫還是敏感的,但多學習一個參數影響不是很大的哈!僅此介紹一下。


 注意diff可以比較兩個不同目錄下的文件,一般用在ASCII純文本文件上,以行為比較單位。


 2、cmp使用


測試用例
[root@achao liqingzhao]# cat test1.txt
 libai
 hello dufu
 [root@achao liqingzhao]# cat test2.txt
  LIBAI
  hello        dufu


案例展示

[root@achao liqingzhao]# cmp test1.txt test2.txt 
test1.txt test2.txt differ: byte 1, line 1
[root@achao liqingzhao]# cmp -l test1.txt test2.txt 
 1 154 114 
 2 151 111 
 3 142 102 
 4 141 101 
 5 151 111
 13 144  40
 14 165  40
 15 146  40
 16 165  40
 17  12  40
 cmp: EOF on test1.txt

  

cmp主要是利用字節去對比,當然也可以比較二進制文件。


 3patch使用

    將舊文件變成新文件,思路:1先比較新舊文件差異,并生成補丁文件;2 將補丁更新為新文件即可。


  案例展示

[root@achao liqingzhao]# cat test1.txt
libai
hello dufu
taoyuanming
[root@achao liqingzhao]# cat test2.txt
libai
hello        dufu
dufu


  生成補丁文件test.patch


[root@achao liqingzhao]# diff -Naur test1.txt test2.txt > test.patch
[root@achao liqingzhao]# cat test.patch
 --- test1.txt  2024-06-11 00:58:45.097361101 +0800
 +++ test2.txt  2024-06-11 00:33:27.894034522 +0800
 @@ -1,3 +1,3 @@ 
 libai
 -hello dufu
 -taoyuanming
 +hello        dufu
 +dufu


  補丁文件test.patch升級為新文件test2.txt  

[root@achao liqingzhao]# patch -p0 < test.patch 
patching file test1.txt
[root@achao liqingzhao]# diff test1.txt test2.txt
[root@achao liqingzhao]# cat test1.txt
 libai
 hello        dufu
 dufu
 [root@achao liqingzhao]# cat test2.txt
  libai
  hello        dufu
  dufu
  [root@achao liqingzhao]#


通過本文對diffcmppatch工具的介紹,我們了解到它們在文件比較、差異分析和文件更新方面的強大功能。這些工具在生產環境中有著廣泛的應用場景,掌握它們的使用方法,能讓我們在處理文件時更加高效、便捷。希望大家在實際操作中多加運用,進一步熟悉這些工具的特性。藍隊云官網上擁有完善的技術支持庫可供參考,大家可自行查閱,更多技術問題,可以直接咨詢。同時,藍隊云整理了運維必備的工具包免費分享給大家使用,需要的朋友可以直接咨詢。更多技術知識,藍隊云期待與你一起探索。


提交成功!非常感謝您的反饋,我們會繼續努力做到更好!

這條文檔是否有幫助解決問題?

非常抱歉未能幫助到您。為了給您提供更好的服務,我們很需要您進一步的反饋信息:

在文檔使用中是否遇到以下問題: