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【比较两个文件,并对差异文件打补丁】
- 生成补丁文件:
diff -ruN log1.txt log2.txt > patch.txt
- 打补丁:
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