- 工信部備案號 滇ICP備05000110號-1
- 滇公安備案 滇53010302000111
- 增值電信業(yè)務(wù)經(jīng)營許可證 B1.B2-20181647、滇B1.B2-20190004
- 云南互聯(lián)網(wǎng)協(xié)會理事單位
- 安全聯(lián)盟認(rèn)證網(wǎng)站身份V標(biāo)記
- 域名注冊服務(wù)機(jī)構(gòu)許可:滇D3-20230001
- 代理域名注冊服務(wù)機(jī)構(gòu):新網(wǎng)數(shù)碼
1、diff的語法和參數(shù)
參數(shù)就不做翻譯了,舉幾個(gè)生產(chǎn)環(huán)境中最常用的例子說明
[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
不使用任何參數(shù)直接比較兩個(gè)文件
[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選項(xiàng)忽略一行中的空白差異
[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選項(xiàng)忽略空白行
[root@achao liqingzhao]# diff -Bb test1.txt test2.txt
4c3
< wangwei
---
> taoyuanming
生產(chǎn)環(huán)境中【-b】和【-B】參數(shù)還是蠻重要的嘞!
案例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]#
選項(xiàng)【-i】使用場景不是很多,因?yàn)長inux中配置文件中對大小寫還是敏感的,但多學(xué)習(xí)一個(gè)參數(shù)影響不是很大的哈!僅此介紹一下。
注意diff可以比較兩個(gè)不同目錄下的文件,一般用在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主要是利用字節(jié)去對比,當(dāng)然也可以比較二進(jìn)制文件。
3、patch使用
將舊文件變成新文件,思路:1先比較新舊文件差異,并生成補(bǔ)丁文件;2 將補(bǔ)丁更新為新文件即可。
案例展示
[root@achao liqingzhao]# cat test1.txt
libai
hello dufu
taoyuanming
[root@achao liqingzhao]# cat test2.txt
libai
hello dufu
dufu
生成補(bǔ)丁文件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
補(bǔ)丁文件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]#
通過本文對diff
、cmp
和patch
工具的介紹,我們了解到它們在文件比較、差異分析和文件更新方面的強(qiáng)大功能。這些工具在生產(chǎn)環(huán)境中有著廣泛的應(yīng)用場景,掌握它們的使用方法,能讓我們在處理文件時(shí)更加高效、便捷。希望大家在實(shí)際操作中多加運(yùn)用,進(jìn)一步熟悉這些工具的特性。藍(lán)隊(duì)云官網(wǎng)上擁有完善的技術(shù)支持庫可供參考,大家可自行查閱,更多技術(shù)問題,可以直接咨詢。同時(shí),藍(lán)隊(duì)云整理了運(yùn)維必備的工具包免費(fèi)分享給大家使用,需要的朋友可以直接咨詢。更多技術(shù)知識,藍(lán)隊(duì)云期待與你一起探索。
售前咨詢
售后咨詢
備案咨詢
二維碼
TOP