[Translated] Linux diff Command Explained With Examples

This commit is contained in:
geekpi 2013-11-21 02:48:41 +00:00
parent 75db9a52f5
commit bcf533468d
2 changed files with 176 additions and 178 deletions

View File

@ -1,178 +0,0 @@
Translating--------------geekpi
Linux diff Command Explained With Examples
================================================================================
![](http://linoxide.com/wp-content/uploads/2013/11/linux-diff-command.png)
When it comes to file comparison, GUI based software are mostly used. Very few people actually use a command line utility for this purpose. Though I wont say that using command line for file/directory comparison is as easy as a cake walk but if you are a Linux user then I think you should know how to compare files through command line as it is definitely a quick method once you are used to it.
In this article, we will learn how to use the diff command through some practical examples.
### Linux diff Command ###
Lets understand Linux diff command through some practical examples.
Suppose we have these two files (file1 and file2) :
$ cat file1
Hi,
Hello,
How are you?
I am fine,
Thank you.
$ cat file2
Hello,
Hi,
How are you?
I am fine.
You can see that both the files have some minor differences. Now, lets see how the diff command can be used to bring out these differences.
Here is how you run diff command :
$ diff file1 file2
1d0
< Hi, 2a2 > Hi,
4,5c4
< I am fine,
< Thank you.
> I am fine.
So you can see that the diff command was provided with both the file names as command line arguments and it produced the differences in the output. The output by no means is easily comprehend-able. Reason being, it was designed to be used by the computers and not the humans. Nonetheless, lets decode the output piece by piece:
**NOTE** The files file1 and file2 will be addressed as old file and new file in the following text.
1d0
< Hi,
Here, the line 1d0 means that the 1st line of the old file should be deleted (d) in order to sync up both the files beginning at line 0. The line to be deleted in old file is mentioned just below with < mark.
2a2
> Hi,
Here, the line 2a2 means that the second line from the new file should be added after second line of old file. The line to be added is displayed in the next line of the output with > mark.
4,5c4
< I am fine,
< Thank you.
> I am fine.
Here, the line 4,5c4 means that the line numbers ranging from 4 to 5 in the old file are now changed and should be replaced with the 4th line of the new file. The lines to be added and deleted are marked with > and < respectively.
So, to conclude,
- The first argument to diff command is regarded as old file while the second argument becomes new file.
- Expressions like 1d0 2a2, 4,5c4 can be decoded with the syntax **[line number or range from old file][action][line number or range from new file]**. Where, action can be append, delete or changed-so-replace.
- The mark < represents the line to be deleted while > represents the line to be added.
In addition to files, the diff command can also be used to compare two directories. Lets learn this through an example.
Here are the contents of a directory named new_dir :
$ ls new_dir/
file file2 frnd frnd1.cpp log1.txt log3.txt log5.txt
file1 file name with spaces frnd1 frnd.cpp log2.txt log4.txt
and here are the contents of a directory named old_dir :
$ ls orig_dir/
file1 file name with spaces frnd1 frnd.cpp log2.txt log4.txt test
file2 frnd frnd1.cpp log1.txt log3.txt log5.txt
Now, here is the output when diff command was executed :
$ diff new_dir/ orig_dir/
Only in new_dir/: file
Only in orig_dir/: test
So you can see that when diff command is executed to compare these two directories, it easily displays the missing files in the directories.
Here are some of the commonly used command line options :
### 1. Use -i to ignore case differences ###
If two files contain same text but in different cases, the diff command still reports it as a difference by default.
For example :
$ cat file1
HI
$ cat file2
hi
$ diff file1 file2
1c1
< HI > hi
So you can see that the diff command reported the case difference in the output.
To get away with this default behaviour, use -i option.
Here is an example :
$ diff -i file1 file2
$
So you can see that no output was produced, which is the default behaviour when both the files are same.
### 2. Report That The Files Are Same Using -s Option ###
Towards the end of the example 1, we saw that diff doesnt report anything if files are same. Though this default behaviour is fine but it could still confuse many, especially the newbies. So, if you want the diff command to report explicitly that the files are same then use -s command line option.
Here is an example :
$ diff -is file1 file2
Files file1 and file2 are identical
So you can see that I added the -s option in the command that we used in last example and this time the diff command explicitly reported that both the files are identical.
### 3. Use -b To Ignore Spaces ###
Another common use case is the varying amount of spaces for which the diff command reports the files as different.
Here is an example :
$ cat file1
Hi, how are you?
$ cat file2
Hi, how are you?
Observe that the only difference between the tow files is the extra space between the words are and you in file2. Now, when diff command is used to compare these two files, here is the output :
$ diff file1 file2
1c1
< Hi, how are you?
> Hi, how are you?
So you can see that the diff command reported the difference. But if you want diff to ignore these spaces, use -b option :
$ diff -b file1 file2
$
So you can see that due to -b option, the diff command reported both the files as same.
The diff command provides many more command line options. Read its [man page][1] for the complete list.
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-command/linux-diff-command-examples/
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://unixhelp.ed.ac.uk/CGI/man-cgi?diff

View File

@ -0,0 +1,176 @@
举例说明Linux diff 命令
================================================================================
![](http://linoxide.com/wp-content/uploads/2013/11/linux-diff-command.png)
在比较文件的时候通常会用GUI软件。很少有人真正会为了这个目的使用命令行工具。虽然不会说使用命令行就像散步一样简单但是如果你是一个Linux使用者那么我想你应该知道如何通过命令行比较文件因为你一旦使用了它它绝对是一个快速的方法。
在本篇中我们将学习如何使用diff命令通过一些实际的例子。
### Linux diff 命令 ###
让我们通过一些实际的例子理解diff命令。
假设我们有两个文件(file1 和 file2)
$ cat file1
Hi,
Hello,
How are you?
I am fine,
Thank you.
$ cat file2
Hello,
Hi,
How are you?
I am fine.
你可以看见两个文件有些小的不同。现在让我们看看diff命令如何找出两者的不同的。
像这样运行diff命令:
$ diff file1 file2
1d0
< Hi, 2a2 > Hi,
4,5c4
< I am fine,
< Thank you.
> I am fine.
你可以看见diff后面跟了两个文件的名字作为命令行的参数并且它在输出中生成了不同。输出并不容易理解。理由是这是被计算机使用的而不是为了人类。尽管如此让我们一步步解码输出:
**注意** 在下面的文本中file1和file2将被作为旧文件和新文件。
1d0
< Hi,
这里1d0这一行意味着旧文件的第一行应该被删除(d)以使两个文件的第一行同步。旧文件中需要被删除的行以'<'标记。
2a2
> Hi,
这里2a2行意味着新文件中的第二行应该加到老文件的第二行后。要添加的行显示在输出的下一行用'>'标记。
4,5c4
< I am fine,
< Thank you.
> I am fine.
这里4,5c4这一行意味着在旧文件中的4到5行现在已被改变并且需要用新文件中的第一行代替。添加和删除的行分别用'>'和'<'表示。
那么,来总结一下,
- 首先diff命令的第一个参数被视为旧文件而第二个参数被视为新文件。
- 像1d0 2a2, 4,5c4这种表达式可以用语法解码为 **[旧文件的行号或者行的范围][行为][新文件的行号或者行的范围]**。这里的'行为'可以使追加,删除或者改变替换。
- '<'代表删除的行,而'>'代表添加的行。
除了文件外diff命令还可以比较两个目录。让我们通过一个例子学习。
这里是'new_dir'目录包含的内容:
$ ls new_dir/
file file2 frnd frnd1.cpp log1.txt log3.txt log5.txt
file1 file name with spaces frnd1 frnd.cpp log2.txt log4.txt
这是'orig_dir'目录含有的内容:(译注原文为and here are the contents of a directory named old_dir ,其中'old_dir'应为笔误。)
$ ls orig_dir/
file1 file name with spaces frnd1 frnd.cpp log2.txt log4.txt test
file2 frnd frnd1.cpp log1.txt log3.txt log5.txt
下面是diff命令执行后的输出
$ diff new_dir/ orig_dir/
Only in new_dir/: file
Only in orig_dir/: test
你可以看到当diff命令被用来比较这两个目录时很容易就会显示两个文件中缺失的文件。
下面是在命令行下常用的选项:
### 1. 用 -i 忽略大小写 ###
如果两个文件含有相同的文本但是大小写不同diff命令仍会默认报告它不同。
比如:
$ cat file1
HI
$ cat file2
hi
$ diff file1 file2
1c1
< HI > hi
你可以看见diff命令在输出中报告了大小写不同。
要去除这个默认行为,使用-i选项。
以下是例子:
$ diff -i file1 file2
$
这样你可以看到没有生成输出,这是当两个文件相同时的默认行为。
### 2. 用 -s 选项报告两个文件相同 ###
在例子1的后面我们看到如果文件相同dif不会生成报告。虽然这个默认行为不错但是它仍可能会造成很大疑惑特别对于新手而言。因此如果你像样diff命令明确地报告两个文件不同那么就使用-s命令选项。
这是个例子:
$ diff -is file1 file2
Files file1 and file2 are identical
你可以看到我加了-s选项在后面的例子中这次diff命令会明确地报告两个文件是相同的。
### 3. 使用 -b 忽略空格 ###
另外一个常用的是diff报告文件存在不同数量的空格。
以下是例子:
$ cat file1
Hi, how are you?
$ cat file2
Hi, how are you?
观察这两个文件唯一的不同是'are'和'you'之间额外的空格。现在当使用diff命令比较两个文件时这个是输出
$ diff file1 file2
1c1
< Hi, how are you?
> Hi, how are you?
因此你可以看到diff命令报告了不同。但是如果你想要忽略这些空格使用 -b 选项。
$ diff -b file1 file2
$
这样你可以看到由于-b选项diff命令报告这两个文件是相同的。
diff命令还提供了更多的命令行选项。阅读[man page][1]来获取完整的列表。
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-command/linux-diff-command-examples/
译者:[geekpi](https://github.com/geekpi) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://unixhelp.ed.ac.uk/CGI/man-cgi?diff