linux命令学习-diff命令

diff命令的使用

diff 命令是 Unix 和类 Unix 操作系统中的一个工具,用于比较文件的内容并显示它们之间的差异。它可以用来比较两文件的不同,也可以用来比较目录中的文件。diff 常用于版本控制和文件的变更跟踪。

文件准备

// log1.txt
Line 1: This is the first line. 
Line 2: This is the second line. 
Line 3: This is the third line. 
Line 4: This is the fourth line. 
Line 5: This is the fifth line.  

// log2.txt
Line 1: This is the first line. 
Line 2: This is the modified second line. 
Line 3: This is the third line. 
Line 4: This is an additional line. 
Line 5: This is the fourth line. 
Line 6: This is the fifth line.  

命令1【diff 文件1 文件2】

diff log1.txt log2.txt
// 输出内容
2c2
< Line 2: This is the second line.
---
> Line 2: This is the modified second line.
4,5c4,6
< Line 4: This is the fourth line.
< Line 5: This is the fifth line.
---
> Line 4: This is an additional line.
> Line 5: This is the fourth line.
> Line 6: This is the fifth line.

// 2c2代表文件1和文件2,第二行不一样,4,5c4,6指的是4到5行与4到6行不一样
a - add
c - change
d - delete 

比如:"11,12d10"表示第一个文件比第二个文件多了第11和12行。

命令2【diff 文件1 文件2 -y -W 100】

// 并排输出,-W是指定宽,配合-y,可以并列展示不同文件的差别
Line 1: This is the first line.                Line 1: This is the first line.
Line 2: This is the second line.              |Line 2: This is the modified second line.
Line 3: This is the third line.                Line 3: This is the third line.
Line 4: This is the fourth line.              |Line 4: This is an additional line.
Line 5: This is the fifth line.               |Line 5: This is the fourth line.
                                              >Line 6: This is the fifth line.


“|”表示前后2个文件内容有不同
“<”表示后面文件比前面文件少了1行内容
“>”表示后面文件比前面文件多了1行内容

命令3【diff 文件1 文件2 -c】

*** log1.txt        2024-08-10 13:28:57.534490029 +0800
--- log2.txt        2024-08-10 13:23:33.175801448 +0800
***************
*** 1,5 ****
  Line 1: This is the first line.
! Line 2: This is the second line.
  Line 3: This is the third line.
! Line 4: This is the fourth line.
! Line 5: This is the fifth line.
--- 1,6 ----
  Line 1: This is the first line.
! Line 2: This is the modified second line.
  Line 3: This is the third line.
! Line 4: This is an additional line.
! Line 5: This is the fourth line.
! Line 6: This is the fifth line.

“+” 比较的文件的后者比前着多一行
“-” 比较的文件的后者比前着少一行
“!” 比较的文件两者有差别的行

命令4【diff 文件1 文件2 -u】

// 统一输出
--- log1.txt        2024-08-10 13:28:57.534490029 +0800
+++ log2.txt        2024-08-10 13:23:33.175801448 +0800
@@ -1,5 +1,6 @@
 Line 1: This is the first line.
-Line 2: This is the second line.
+Line 2: This is the modified second line.
 Line 3: This is the third line.
-Line 4: This is the fourth line.
-Line 5: This is the fifth line.
+Line 4: This is an additional line.
+Line 5: This is the fourth line.
+Line 6: This is the fifth line.


---表示变动前的文件,也就是log1.txt
+++表示变动后的文件,也就是log2.txt
@@表示行的起始和结束
-1,5意思是:-表示变动前的文件log1.txt, 1,5代表1到5行;
+1,6意思是:+表示变动后的文件log2.txt, 1,6代表1到6行;

命令5【比较两个文件,并对差异文件打补丁】

  1. 生成补丁文件:diff -ruN log1.txt log2.txt > patch.txt
  2. 打补丁:patch log1.txt patch.txt
打补丁后的文件log1.txt如下:
Line 1: This is the first line.
Line 2: This is the modified second line.
Line 3: This is the third line.
Line 4: This is an additional line.
Line 5: This is the fourth line.
Line 6: This is the fifth line.     

跟log2.txt是一致的。                      

其他参考:https://www.cnblogs.com/peida/archive/2012/12/12/2814048.html

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SAO&asuna

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值