Linux diff command
Complete List of Linux Commands
The Linux diff command is used to compare differences between files.
diff compares text files line by line. If directories are specified for comparison, diff will compare files with the same name in the directories, but will not compare subdirectories within them.
Syntax
diff [-abBcdefHilnNpPqrstTuvwy][-<行数>][-C <行数>][-D <巨集名称>][-I <字符或字符串>][-S <文件>][-W <宽度>][-x <文件或目录>][-X <文件>][--help][--left-column][--suppress-common-line][文件或目录1][文件或目录2]
Parameters:
- -<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 By default, diff only compares text files line by line.
- -b or --ignore-space-change Does not check differences in space characters.
- -B or --ignore-blank-lines Does not check blank lines.
- -c Displays the complete content and marks the differences.
- -C<number of lines> or --context<number of lines> Same as executing the "-c-<number of lines>" command.
- -d or --minimal Uses a different algorithm to make comparisons in smaller 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 for ed script files.
- -f or --forward-ed The output format is similar to ed script files, but displays the differences in the order of the original file.
- -H or --speed-large-files Can speed up comparison when comparing large files.
- -I<character or string> or --ignore-matching-lines<character or string> If two files differ on certain lines, and these lines all contain the specified character or string from the option, the differences between the two files will not be displayed.
- -i or --ignore-case Does not check case differences.
- -l or --paginate Sends the results to the pr program for pagination.
- -n or --rcs Displays the comparison results in RCS format.
- -N or --new-file When comparing directories, if file A only appears in one directory, the default display is:
- Only in directory: file A. If the -N parameter is used, diff will compare file A with an empty file.
- -p If the files being compared are C language source code files, displays the name of the function where the difference is located.
- -P or --unidirectional-new-file Similar to -N, but only when the second directory contains a file that the first directory does not have will this file be compared with an empty file.
- -q or --brief Only shows whether there are differences, not detailed information.
- -r or --recursive Compares files in subdirectories.
- -s or --report-identical-files If no differences are found, still displays a message.
- -S<file> or --starting-file<file> When comparing directories, starts comparing from the specified file.
- -t or --expand-tabs Expands tab characters in the output.
- -T or --initial-tab Adds a tab character to the beginning of each line for alignment.
- -u, -U<number of columns> or --unified=<number of columns> Displays the differences in file contents in a merged format.
- -v or --version Displays version information.
- -w or --ignore-all-space Ignores all space characters.
- -W<width> or --width<width> When using the -y parameter, specifies the column width.
- -x<file name or directory> or --exclude<file name or directory> Does not compare the files or directories specified in the option.
- -X<file> or --exclude-from<file> You can save file or directory types into a text file, and then specify this text file in =<file>.
- -y or --side-by-side Displays the similarities and differences between files in a side-by-side format.
- --help Displays help.
- --left-column When using the -y parameter, if the content of a line in the two files is the same, the line content is only displayed in the left column.
- --suppress-common-lines When using the -y parameter, only displays the differences.
Example 1: Compare two files
[root@localhost test3]# diff log2014.log log2013.log 3c3 < 2014-03 --- > 2013-03 8c8 < 2013-07 --- > 2013-08 11,12d10 < 2013-11 < 2013-12
The "3c3" and "8c8" above indicate that the files log2014.log and log20143log have different content on lines 3 and 8; "11,12d10" indicates that the first file has lines 11 and 12 more than the second file.
Example 2: Side-by-side format output
[root@localhost test3]# diff log2014.log log2013.log -y -W 50
2013-01 2013-01
2013-02 2013-02
2014-03 | 2013-03
2013-04 2013-04
2013-05 2013-05
2013-06 2013-06
2013-07 2013-07
2013-07 | 2013-08
2013-09 2013-09
2013-10 2013-10
2013-11 <
2013-12 <
[root@localhost test3]# diff log2013.log log2014.log -y -W 50
2013-01 2013-01
2013-02 2013-02
2013-03 | 2014-03
2013-04 2013-04
2013-05 2013-05
2013-06 2013-06
2013-07 2013-07
2013-08 | 2013-07
2013-09 2013-09
2013-10 2013-10
> 2013-11
> 2013-12
Explanation:
- "|" indicates that the contents of the two files differ.
- "<" indicates that the latter file has one less line of content than the former file.
- ">" indicates that the latter file has one more line of content than the former file.