diff
command is used to compare the differences between two files or directories. It compares the contents of files line by line and displays differences on different lines. diff
The usage and options of the command are as follows :
Basic syntax:
diff [选项] 文件1 文件2
Common options:
-c
or--context
: show context differences (3 lines by default)-u
or--unified
: show diffs in uniform format-r
or--recursive
: recursively compare files in directories-q
Or--brief
: only display whether there is a difference in the file, do not display the specific content of the difference-i
or--ignore-case
: ignore differences in case-w
or--ignore-all-space
: ignore all whitespace characters for differences-B
or--ignore-blank-lines
: ignore differences on blank lines--ignore-file-name-case
: Ignore differences in filename case
Example usage:
-
Compare the differences of two files, and display the contextual differences (default 3 lines):
diff file1.txt file2.txt
-
Display file differences using a uniform format:
diff -u file1.txt file2.txt
-
Compare the differences of two directories (recursive comparison):
diff -r dir1 dir2
-
Only show whether there is a difference in the file, and do not show the specific content of the difference:
diff -q file1.txt file2.txt
diff
The output format of the command is as follows:
>
symbol indicates that the line only appears in the second file.<
The symbol indicates that the line only appears in the first file.---
marks between different paragraphs of the two documents.***
Marks between identical paragraphs in both documents.
When using diff
the command to compare files, the output shows the differences between the files in text form. Here is an example of the output:
4c4
< This is line 4 in file1.txt.
---
> This is line 4 in file2.txt.
The above output indicates that there is a difference in the fourth line in the two files:
<
Indicates that the line only appears in the first file (file1.txt).>
Indicates that the line only appears in the second file (file2.txt).-
The delimiter indicates the difference between two files.- The number before the first arrow indicates the line number in the first file.
- The number after the second arrow indicates the line number in the second file.
In the example above, the fourth line file1.txt
would read "This is line 4 in file1.txt." in the and file2.txt
read "This is line 4 in file2.txt." in the .