Linux diff command
The Linux diff command is used to compare differences between files.
diff compares text files line by line for similarities and differences. If directories are specified for comparison, diff compares files with the same names in those directories but does not compare their subdirectories.
Syntax
diff [-abBcdefHilnNpPqrstTuvwy][-<行数>][-C <行数>][-D <巨集名称>][-I <字符或字符串>][-S <文件>][-W <宽度>][-x <文件或目录>][-X <文件>][--help][--left-column][--suppress-common-line][文件或目录1][文件或目录2]
Parameters:
- -
Specify how many lines of text to display. This argument must be used together with -c or -u. - -a or --text By default, diff only compares text files line by line.
- -b or --ignore-space-change Do not check for differences in whitespace.
- -B or --ignore-blank-lines Do not check for blank lines.
- -c Display all text and mark the differences.
- -C
or --context Same as executing the "-c- " command. - -d or --minimal Use a different algorithm to compare in smaller units.
- -D
or ifdef The output format of this argument is suitable for preprocessor macros. - -e or --ed The output format of this argument is suitable for ed script files.
- -f or -forward-ed The output format is similar to an ed script file, but shows differences in the order of the original file.
- -H or --speed-large-files Speeds up comparison of large files.
- -I
or --ignore-matching-lines If two files differ on certain lines, and those lines all contain the character or string specified in the option, then the differences are not shown. - -i or --ignore-case Do not check for differences in case.
- -l or --paginate Pass the result to the pr program for pagination.
- -n or --rcs Display the comparison result in RCS format.
- -N or --new-file When comparing directories, if file A appears in only one directory, by default it shows:
- Only in directory: If the -N option is used, diff compares file A with an empty file.
- -p When the compared files are C source code files, display the name of the function where the difference occurs.
- -P or --unidirectional-new-file Similar to -N, but only when the second directory contains a file not present in the first directory will that file be compared with an empty file.
- -q or --brief Only report whether there are differences, without showing detailed information.
- -r or --recursive Compare files in subdirectories.
- -s or --report-identical-files Still display a message even if no differences are found.
- -S
or --starting-file When comparing directories, start the comparison from the specified file. - -t or --expand-tabs Expand tab characters in the output.
- -T or --initial-tab Prepend a tab character to each line for alignment.
- -u,-U
or --unified= Display file content differences in a unified format. - -v or --version Display version information.
- -w or --ignore-all-space Ignore all whitespace characters.
- -W
or --width When using the -y option, specify the column width. - -x
or --exclude Do not compare the file or directory specified in the option. - -X
or --exclude-from You can save the files or directories to exclude in a text file, then specify . - -y or --side-by-side Display the differences side by side.
- --help Display help.
- --left-column When using -y, if a line is identical in both files, display that line only in the left column.
- --suppress-common-lines When using -y, display only 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 differ on lines 3 and 8; "11,12d10" means the first file has 2 extra lines, lines 11 and 12, compared to the second.
Example 2: Side-by-side 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 two files differ
- "<" indicates that the latter file has one line less than the former
- ">" indicates that the latter file has one line more than the former
Linux Command Reference