测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Linux----diff命令详细使用方法
收藏本文
作者:redrose2100 类别: 日期:2022-12-16 09:26:31 阅读:839 次 消耗积分:0 分
[【原文链接】Linux----diff命令详细使用方法](http://devops-dev.com/article/508) # 一、diff命令使用方法 ## 1.1 diff命令格式 ```bash diff [选项] 文件1 文件2 ``` ## 1.2 diff命令常用选项 * -b: 不检查空格 * -B: 不检查空白行 * -i: 不检查大小写 * -w: 忽略所有的空格 * -\-normal: 正常格式显示(默认) * -c: 上下文格式显示 * -u: 合并格式显示 # 二、diff命令使用实例 首先准备两个文件,demo1.txt内容如下: ``` aaa bbb hello world Hello World 111 222 1111111 ``` demo2.txt文件内容如下: ```bash aaa bbb Hello World HelloWorld 111 222 1111111 ``` ## 2.1 普通模式下比较两个文件 比较两个文件,结果如下: ```bash [root@jiayi-centos-01 opt]# diff demo1.txt demo2.txt 2d1 # 第一个文件第2行删除(delete)和第个文件的第一行保持一致 < bbb # < 表示第一个文件的内容第2行 4c3 # 第一个文件第四行需要修改(change)后才能与第二个文件的第三行一致 < hello world # < 表示这是第一个文件的第4行 --- > bbb # > 表示第二个文件的 8,9c7 # 第一个文件的第8行和第9行需要修改(change)之后才能和第二个文件的第7行一致 < 111 222 # < 表示第一个文件的第8行 < 1111111 # < 表示第一个文件的第9行 --- > HelloWorld # > 表示第二个文件的第7行 10a9,11 # 第一个文件的第10行需要修改(change)之后才可能和第二个文件的第9至第11行相同,因为第一个文件只有9行,所以下面不再显示第一个文件的第10行,二直接显示第二文件的 > 111 222 # > 表示第二个文件的第9行 > # > 表示第二个文件的第10行 > 1111111 # > 表示第二个文件的第11行 [root@jiayi-centos-01 opt]# ``` ## 2.2 上下文模式下比较两个文件 ```bash [root@jiayi-centos-01 opt]# diff -c demo1.txt demo2.txt *** demo1.txt 2022-12-16 14:29:06.658081952 +0800 --- demo2.txt 2022-12-16 14:29:23.941081130 +0800 *************** *** 1,10 **** aaa - bbb # 表示第一个文件需要删除此行 ! hello world # 表示第一个文件此行需要修改,标记修改1 Hello World ! 111 222 # 表示第一个文件此行需要修改,标记修改2 ! 1111111 # 表示第一个文件此行需要修改,标记修改3 --- 1,11 ---- aaa ! bbb # 表示第一个文件修改1位置需要修改为此行的内容,才可以与第二文件一致 Hello World ! HelloWorld # 表示第一个文件中的修改2和修改3处需要修改为此行的内容,才可以与第二个文件一致 + 111 222 # 第一个文件需要增加此行才可以与第二个文件一致 + # 第一个文件需要增加此行才可以与第二个文件一致 + 1111111 # 第一个文件需要增加此行才可以与第二个文件一致 [root@jiayi-centos-01 opt]# ``` ## 2.3 忽略大小写比较两个文件 ```bash [root@jiayi-centos-01 opt]# diff -i demo1.txt demo2.txt 1a2 # 第一个文件的第一行后需要增加(add)才能和第二个文件的第二行一致 > 6c7 # 第一个文件的第6行需要修改(change)才能和第二个文件的第7行一致 < Hello World # 第一个文件的第6行的内容 --- > HelloWorld # 第二个文件的第7行的内容 8,9c9 # 第一个文件的第8行第9行需要修改(change)才能与第二个文件的第9行一致 < 111 222 # 第一个文件的第8行 < 1111111 # 第一个文件的第9行 --- > 111 222 # 第二个文件的第9行 10a11 # 第一个文件的第10行需要增加(add)才能u第二个文件的第11行内容一直 > 1111111 # 第二个文件的第11行的内容 [root@jiayi-centos-01 opt]# ``` ## 2.4 不检查空格比较两个文件 如下所示,这里就不再详细解释了,通过-b参数即可 ```bash [root@jiayi-centos-01 opt]# diff -b demo1.txt demo2.txt 2d1 < bbb 4c3 < hello world --- > bbb 7a7,8 > HelloWorld > 9d9 < 1111111 10a11 > 1111111 [root@jiayi-centos-01 opt]# ``` ## 2.5 不检查空白行比较两个文件的内容 通过-B参数即可 ```bash [root@jiayi-centos-01 opt]# diff -B demo1.txt demo2.txt 2d1 < bbb 4c3 < hello world --- > bbb 8,9c7 < 111 222 < 1111111 --- > HelloWorld 10a9,11 > 111 222 > > 1111111 [root@jiayi-centos-01 opt]# ``` ## 2.6 忽略所有空格比较两个文件 通过-w参数 ```bash [root@jiayi-centos-01 opt]# diff -w demo1.txt demo2.txt 1a2 > 4c5 < hello world --- > Hello World 9d9 < 1111111 10a11 > 1111111 [root@jiayi-centos-01 opt]# ``` ## 2.7 以合并的方式比较两个文件 通过-u参数 ```bash [root@jiayi-centos-01 opt]# diff -u demo1.txt demo2.txt --- demo1.txt 2022-12-16 14:29:06.658081952 +0800 +++ demo2.txt 2022-12-16 14:29:23.941081130 +0800 @@ -1,10 +1,11 @@ aaa -bbb # 表示第一个文件需要将bbb这一行删除才能和第二个文件一致 -hello world # 表示第一个文件需要删除此行才可以与第二个文件一致 +bbb # 表示第一个文件需要增加此行才可以与第二个文件一致 Hello World -111 222 # 表示第一个文件需要删除此行才可以与第二个文件一致 -1111111 # 表示第一个文件需要删除此行才可以与第二个文件一致 +HelloWorld # 表示第一个文件需要增加此行才可以与第二个文件一致 +111 222 # 表示第一个文件需要增加此行才可以与第二个文件一致 + # 表示第一个文件需要增加此行才可以与第二个文件一致 +1111111 # 表示第一个文件需要增加此行才可以与第二个文件一致 [root@jiayi-centos-01 opt]# ``` ## 2.8 比较两个目录中文件名的不同 首先准备两个文件夹,并在文件夹中分别创建5个文件,如下 ```bash [root@jiayi-centos-01 opt]# mkdir dir1 [root@jiayi-centos-01 opt]# mkdir dir2 [root@jiayi-centos-01 opt]# touch dir1/file{1..5} [root@jiayi-centos-01 opt]# touch dir2/file{1..3} [root@jiayi-centos-01 opt]# touch dir2/test{1..2} [root@jiayi-centos-01 opt]# ``` 查看此时dir1和dir2两个目录 ```bash [root@jiayi-centos-01 opt]# ll dir1 total 0 -rw-r--r-- 1 root root 0 Dec 19 09:31 file1 -rw-r--r-- 1 root root 0 Dec 19 09:31 file2 -rw-r--r-- 1 root root 0 Dec 19 09:31 file3 -rw-r--r-- 1 root root 0 Dec 19 09:31 file4 -rw-r--r-- 1 root root 0 Dec 19 09:31 file5 [root@jiayi-centos-01 opt]# ll dir2 total 0 -rw-r--r-- 1 root root 0 Dec 19 09:31 file1 -rw-r--r-- 1 root root 0 Dec 19 09:31 file2 -rw-r--r-- 1 root root 0 Dec 19 09:31 file3 -rw-r--r-- 1 root root 0 Dec 19 09:31 test1 -rw-r--r-- 1 root root 0 Dec 19 09:31 test2 [root@jiayi-centos-01 opt]# ``` 通过 -q 命令比较两个目录的不同 ```bash [root@jiayi-centos-01 opt]# diff -q dir1 dir2 Only in dir1: file4 Only in dir1: file5 Only in dir2: test1 Only in dir2: test2 [root@jiayi-centos-01 opt]# ``` ## 2.9 使用diff和patch打补丁 比如正在运行的程序中配置文件demo1.conf 内容如下 ```bash name=test ip=127.0.0.1 user=admin password=admin ``` 而此时需要对demo1.conf文件修改配置,修改为如下内容 ```bash name=test domain=test ip=127.0.0.1 area=china user=admin password=admin role=admin ``` 传统方式直接修改,则肯定是要停止服务的,这里就可以通过打补丁的方式,首先将上述内容存入demo2.conf文件 如下,首先将两个文件的不同输入到一个新文件 ```bash diff -uN demo1.conf demo2.conf > demo.patch ``` 此时查看demo.patch文件内容如下 ```bash [root@jiayi-centos-01 opt]# cat demo.patch --- demo1.conf 2022-12-19 10:38:12.842422564 +0800 +++ demo2.conf 2022-12-19 10:39:33.766418718 +0800 @@ -1,4 +1,7 @@ name=test +domain=test ip=127.0.0.1 +area=china user=admin password=admin +role=admin [root@jiayi-centos-01 opt]# ``` 然后将demo.patch补丁文件打入demo1.conf配置文件 ```bash patch demo1.conf demo.patch ``` 此时查看demo1.conf,内容如下,可见已经将demo2.conf中新增的内容添加到demo1.conf中了 ```bash [root@jiayi-centos-01 opt]# cat demo1.conf name=test domain=test ip=127.0.0.1 area=china user=admin password=admin role=admin [root@jiayi-centos-01 opt]# ``` 当然此时也可以再次使用diff工具比较此时demo1.conf和demo2.conf这两个文件的异同,如下没有任何输出,表明此时两个文件已经完全相同了 ```bash [root@jiayi-centos-01 opt]# diff demo1.conf demo2.conf [root@jiayi-centos-01 opt]# ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/508
上一篇:
Linux----tee命令详细使用方法
下一篇:
云计算----什么是云计算
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件