在 Linux 中,diff
命令用于比较文件的内容,并显示它们之间的差异。以下是一些常见的用法和示例:
基本用法
比较两个文件并显示它们的差异:
diff file1.txt file2.txt
常见选项
-u
:生成统一格式的输出,这种格式更容易阅读。-c
:生成上下文格式的输出,这种格式也比较容易阅读。-r
:递归地比较目录中的文件。-i
:忽略大小写的差异。-w
:忽略所有空白字符的差异。
示例
-
基本比较:
diff file1.txt file2.txt
-
生成统一格式的输出:
diff -u file1.txt file2.txt
输出示例:
--- file1.txt 2024-06-24 10:00:00.000000000 +0000 +++ file2.txt 2024-06-24 10:00:00.000000000 +0000 @@ -1,4 +1,4 @@ -This is line 1 of file1 +This is line 1 of file2 This is line 2 -This is line 3 of file1 +This is line 3 of file2 This is line 4
-
递归比较目录: