- 工信部備案號 滇ICP備05000110號-1
- 滇公安備案 滇53010302000111
- 增值電信業務經營許可證 B1.B2-20181647、滇B1.B2-20190004
- 云南互聯網協會理事單位
- 安全聯盟認證網站身份V標記
- 域名注冊服務機構許可:滇D3-20230001
- 代理域名注冊服務機構:新網數碼
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主要是利用字節去對比,當然也可以比較二進制文件。
3、patch使用
將舊文件變成新文件,思路: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]#
通過本文對diff
、cmp
和patch
工具的介紹,我們了解到它們在文件比較、差異分析和文件更新方面的強大功能。這些工具在生產環境中有著廣泛的應用場景,掌握它們的使用方法,能讓我們在處理文件時更加高效、便捷。希望大家在實際操作中多加運用,進一步熟悉這些工具的特性。藍隊云官網上擁有完善的技術支持庫可供參考,大家可自行查閱,更多技術問題,可以直接咨詢。同時,藍隊云整理了運維必備的工具包免費分享給大家使用,需要的朋友可以直接咨詢。更多技術知識,藍隊云期待與你一起探索。
售前咨詢
售后咨詢
備案咨詢
二維碼
TOP