Linux diff Command
The Linux diff command is used to compare the differences between files.
diff compares text files line by line to identify the similarities and differences. If directories are specified for comparison, diff will compare files with the same names within the directories but will not compare subdirectories.
Syntax
diff [-abBcdefHilnNpPqrstTuvwy][-<number of lines>][-C <number of lines>][-D <macro name>][-I <character or string>][-S <file>][-W <width>][-x <file or directory>][-X <file>][--help][--left-column][--suppress-common-lines][file or directory1][file or directory2]
Parameters:
- -<number of lines> Specifies the number of lines of text to display. This parameter must be used in conjunction with -c or -u.
- -a or --text diff defaults to line-by-line comparison of text files.
- -b or --ignore-space-change Ignores changes in whitespace characters.
- -B or --ignore-blank-lines Ignores blank lines.
- -c Displays the entire content and marks the differences.
- -C<number of lines> or --context<number of lines> Equivalent to the command "-c-<number of lines>".
- -d or --minimal Uses a different algorithm to compare in smaller units.
- -D<macro name> or ifdef<macro name> The output format can be used for preprocessor macros.
- -e or --ed The output format can be used for ed script files.
- -f or -forward-ed The output format is similar to ed script files but displays differences in the original file order.
- -H or --speed-large-files Speeds up comparison of large files.
- -I<character or string> or --ignore-matching-lines<character or string> If both files differ in certain lines and those lines contain the specified character or string, the differences are not shown.
- -i or --ignore-case Ignores differences in case.
- -l or --paginate Passes the result to the pr program for pagination.
- -n or --rcs Displays the comparison result in RCS format.
- -N or --new-file When comparing directories, if file A appears only in one directory, by default it shows: Only in directory: file A. With the -N parameter, diff compares file A with an empty file.
- -p If the compared files are C language source code, it shows the function name where the difference occurs.
- -P or --unidirectional-new-file Similar to -N, but only compares a file in the second directory with an empty file if it does not exist in the first directory.
- -q or --brief Only shows whether there are differences, without detailed information.
- -r or --recursive Compares files in subdirectories.
- -s or --report-identical-files Even if no differences are found, it still shows a message.
- -S<file> or --starting-file<file> Starts comparison from the specified file when comparing directories.
- -t or --expand-tabs Expands tab characters in the output.
- -T or --initial-tab Adds a tab character at the beginning of each line for alignment.
- -u, -U<columns> or --unified=<columns> Displays differences in a merged format.
- -v or --version Shows version information.
- -w or --ignore-all-space Ignores all whitespace characters.
- -W<width> or --width<width> Specifies the column width when using the -y parameter.
- -x<filename or directory> or --exclude<filename or directory> Excludes specified files or directories from comparison.
- -X<file> or --exclude-from<file> You can store file or directory types in a text file, and specify this text file in =<file>.
- -y or --side-by-side Displays differences side by side.
- --help Shows help.
- --left-column When using the -y parameter, if two files have identical content in a line, it only shows that line in the left column.
- --suppress-common-lines When using the -y parameter, only shows 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" indicate that log2014.log and log2013.log differ on lines 3 and 8, respectively. The "11,12d10" indicates that the first file has lines 11 and 12 that are not present in 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 are different.
"<" indicates that the latter file has one line less than the former file.
">" indicates that the latter file has one line more than the former file.