- 工信部备案号 滇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