linux diff命令
The Linux diff command analyses a file line by line and gives an output of a list of changes made between two files.
Linux diff命令逐行分析文件,并输出两个文件之间所做更改的列表。
The command diff is short for difference, which in essence gives the differences between two files.
diff是差异的缩写,本质上是两个文件之间的差异 。
Unlike the cmp
(compare) and comm
commands, diff highlights the specific line that needs to be changed to match the other files.
与cmp
(比较)和comm
命令不同,diff突出显示需要更改以匹配其他文件的特定行。
One thing Linux administrators cannot forget is that diff uses symbols and instructions to make it more effective.
Linux管理员不能忘记的一件事是,diff使用符号和说明来使其更加有效。
The specific symbols instruct the command to change the file in a particular way to match the second.
特定符号指示命令以特定方式更改文件以匹配第二种。
Special Symbols in the diff command output:
diff命令输出中的特殊符号:
- a: add 答:添加
- c: change c:变更
- d: delete d:删除
Linux diff命令语法 (Linux diff Command Syntax)
diff [options] File1 File2
Let’s have a look at diff command example usages.
让我们看一下diff命令示例用法。
情况1:变更 (Case 1: Change)
Assume we have two files: x.txt and y.txt
假设我们有两个文件: x.txt和y.txt
x.txt contains the following content.
x.txt包含以下内容。
California
Miami
Ohio
Kansas
Texas
y.txt contains the following content.
y.txt包含以下内容。
California
Nevada
Georgia
Kansas
Texas
Now, use the diff command to make the comparison between the two files as shown.
现在,使用diff命令在两个文件之间进行比较,如图所示。
$ diff x.txt y.txt
Sample output
样品输出
Let’s examine the output in more detail.
让我们更详细地检查输出。
The 2,3c2,3
line implies that lines 2 and 3 from the first file need to be changed in order to match lines 2 and 3 from the second text file.
2,3c2,3
行意味着第一个文件中的第2行和第3行需要更改,以匹配第二个文本文件中的第2行和第3行。
diff命令输出符号说明 (diff Command Output Symbols explained)
Let’s briefly look at the various symbols encountered in the output.
让我们简要地看一下输出中遇到的各种符号。
- The less than symbol indicates lines from the first line 小于符号表示从第一行开始的行
- The greater than symbol indicates lines from the second line 大于符号表示从第二行开始的行
a - Denotes that text was added to the file
c - Denotes that changes were made in the file
d - Indicates that the line was expunged or deleted
Let’s take a look at another example.
让我们看另一个例子。
情况2:添加 (Case 2: Adding)
We have two text files:
我们有两个文本文件:
The file file 1.txt has the following content.
文件1.txt具有以下内容。
Linux is a great operating system
It's free and opensource
It's light and stable
I would highly recommend it
file2.txt has the following content.
file2.txt具有以下内容。
Linux is a great operating system
It's free and opensource
It's light and stable
Oh ! It can be installed on almost any PC hardware
I would highly recommend it
Use the diff command to compare both files.
使用diff命令比较两个文件。
$ diff file1.txt file2.txt
Sample output
样品输出
From the output above, 3a4
implies that after line 3 from the first file, another line needs to be added to match line 4 from the second file.
从上面的输出中, 3a4
表示在第一个文件的第3行之后,需要添加另一行以匹配第二个文件的第4行 。
情况3:删除 (Case 3: Deletion)
Consider two files as below.
考虑以下两个文件。
file3.txt with the following content.
file3.txt具有以下内容。
Apples
Oranges
Mangoes
Peaches
Bananas
Grapes
And file4.txt which has the following content.
而file4.txt具有以下内容。
Apples
Oranges
Mangoes
Bananas
Grapes
Again, use the diff command to compare both files.
同样,使用diff命令比较两个文件。
$ diff file3.txt file4.txt
Sample output
样品输出
Here the output 4d3 means that delete the 4th line from the first file to sync up with the second file at line number 3.
这里的输出4d3表示从第一个文件中删除第4行,以与第2行的第二个文件同步。
结论 (Conclusion)
Linux diff command is a simple utility to compare two files. It’s useful in checking for the changes between two versions of a text file.
Linux diff命令是一个比较两个文件的简单实用程序。 在检查文本文件的两个版本之间的更改时很有用。
翻译自: https://www.journaldev.com/28936/linux-diff-command-examples
linux diff命令