TrumanWong

diff

Compares the differences between two given files

Supplementary instructions

diff command In the simplest case, compares the differences between two given files. If "-" is used instead of the "file" argument, the content to be compared will come from standard input. The diff command compares the similarities and differences of text files line by line. If the command specifies a directory comparison, files with the same file name in the directory will be compared, without any comparison of subdirectory files.

grammar

diff(options)(parameters)

Options

-<Number of lines>: Specify how many lines of text to display. This parameter must be used together with the -c or -u parameter;
-a or --text: The diff default will only compare text files line by line;
-b or --ignore-space-change: Do not check for differences in space characters;
-B or --ignore-blank-lines: Do not check blank lines;
-c: Display the entire content and mark the differences;
-C<number of lines> or --context<number of lines>: the same as executing the "-c-<number of lines>" command;
-d or --minimal: Use different algorithms to compare in small units;
-D<macro name> or ifdef<macro name>: The output format of this parameter can be used for preprocessor macros;
-e or --ed: The output format of this parameter can be used in ed script files;
-f or -forward-ed: The output format is similar to the ed script file, but the differences are displayed in the order of the original files;
-H or --speed-large-files: When comparing large files, it can speed up;
-l<Character or string> or --ignore-matching-lines<Character or string>: If the two files are different in certain lines, and both files contain the characters or characters specified in the option string, the differences between the two files will not be displayed;
-i or --ignore-case: Do not check case differences;
-l or --paginate: Pass the results to the pr program for pagination;
-n or --rcs: Display the comparison results in RCS format;
-N or --new-file: When comparing directories, if file A only appears in a certain directory, the default display will be: Only in directory, file A. If the -N parameter is used, diff will compare file A with a Empty file comparison;
-p: If the compared file is a C language program code file, display the function name where the difference lies;
-P or --unidirectional-new-file: Similar to -N, but only when the second directory contains a file that is not found in the first directory, this file will be compared with a blank file;
-q or --brief: only displays the differences and does not display detailed information;
-r or --recursive: compare files in subdirectories;
-s or --report-identical-files: If no differences are found, the information is still displayed;
-S<file> or --starting-file<file>: When comparing directories, start the comparison from the specified file;
-t or --expand-tabs: Expand tab characters during output;
-T or --initial-tab: Add tab characters in front of each line for alignment;
-u, -U <number of columns> or --unified=<number of columns>: Display the differences in file content in a merged manner;
-v or --version: display version information;
-w or --ignore-all-space: Ignore all space characters;
-W<width> or --width<width>: Specify the column width when using the -y parameter;
-x<file name or directory> or --exclude<file name or directory>: Do not compare the files or directories specified in the options;
-X<file> or --exclude-from<file>; you can save the file or directory type as a text file, and then specify this text file in =<file>;
-y or --side-by-side: Display the similarities and differences of files side by side;
--help: display help;
--left-column: When using the -y parameter, if the content of a certain line in the two files is the same, only the content of the line will be displayed in the left column;
--suppress-common-lines: When using the -y parameter, only show differences.

Parameters

  • File 1: Specify the first file to be compared;
  • File 2: Specify the second file to compare.

Example

Compare differences in normal mode

diff a.txt b.txt

Compare differences in context mode

diff -c a.txt b.txt
*** a1.txt 2012-08-29 16:45:41.000000000 +0800
--- a2.txt 2012-08-29 16:45:51.000000000 +0800
***************
*** 1,7 ****
a
a
a
!a
a
a
a
--- 1,7 ----
a
a
a
!b
a
a
a

Compare differences in unified mode

diff -u a.txt b.txt
--- a.txt 2012-08-29 16:45:41.000000000 +0800
+++ b.txt 2012-08-29 16:45:51.000000000 +0800
​@@ -1,7 +1,7 @@
a
a
a
-a
+b
a
a
a

Compare differences between multiple files

Compare the file "test.txt" in the directory /usr/li with the file "test.txt" in the current directory, enter the following command:

diff /usr/li test.txt #Use the diff command to compare files

After the above command is executed, the differences after comparison will be listed in the specified form, as shown below:

n1 a n3,n4
n1,n2 d n3
n1,n2 c n3,n4

Among them, the letters "a", "d", and "c" represent add, delete, and modify operations respectively. "n1" and "n2" represent the line numbers in file 1, and "n3" and "n4" represent the line numbers in file 2.

Note: The above instructions specify different line numbers in the two files and their corresponding operations. In the output form, each line will be followed by several affected lines. Among them, the lines starting with < belong to file 1, and the lines starting with > belong to file 2.