The Linux diff command is used to compare the differences between files.
diff compares text files line by line to identify their similarities and differences. If directories are specified for comparison, diff will compare files with the same names within those directories, but it will not compare subdirectories.
Syntax
\ndiff \nParameters:
\n- \n
- - Specifies how many lines of text to display. This parameter must be used with the -c or -u parameter. \n
- -a or --text By default, diff only compares text files line by line. \n
- -b or --ignore-space-change Ignores differences in the amount of white space. \n
- -B or --ignore-blank-lines Ignores changes that consist only of blank lines. \n
- -c Displays all content and marks the differences. \n
- -C or --context Equivalent to executing the "-c-" command. \n
- -d or --minimal Uses a different algorithm to make comparisons with smaller units. \n
- -D or ifdef The output format of this parameter can be used for preprocessor macros. \n
- -e or --ed The output format of this parameter can be used for ed script files. \n
- -f or --forward-ed The output format is similar to ed script files, but displays differences in the original file order. \n
- -H or --speed-large-files Speeds up comparisons when dealing with large files. \n
- -I or --ignore-matching-lines If two files differ in certain lines, and these lines all contain the specified character or string from the option, the differences between the two files are not displayed. \n
- -i or --ignore-case Ignores differences in case. \n
- -l or --paginate Pipes the result through the pr program for pagination. \n
- -n or --rcs Displays the comparison results in RCS format. \n
- -N or --new-file When comparing directories, if file A exists only 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. \n
- -p If the files being compared are C source code files, displays the function name where the difference is located. \n
- -P or --unidirectional-new-file Similar to -N, but only compares a file with an empty file if it exists in the second directory but not in the first. \n
- -q or --brief Only displays whether there are differences, without detailed information. \n
- -r or --recursive Compares files in subdirectories. \n
- -s or --report-identical-files Displays a message even if no differences are found. \n
- -S or --starting-file When comparing directories, starts the comparison from the specified file. \n
- -t or --expand-tabs Expands tab characters in the output. \n
- -T or --initial-tab Prepends a tab character to each line for alignment. \n
- -u, -U or --unified= Displays file content differences in a unified format. \n
- -v or --version Displays version information. \n
- -w or --ignore-all-space Ignores all white space characters. \n
- -W or --width Specifies the column width when using the -y parameter. \n
- -x or --exclude Does not compare the specified files or directories. \n
- -X or --exclude-from You can store file or directory types in a text file and then specify this text file in =. \n
- -y or --side-by-side Displays the similarities and differences of files in a side-by-side format. \n
- --help Displays help information. \n
- --left-column When using the -y parameter, if a line's content is the same in both files, only the left column displays that line's content. \n
- --suppress-common-lines When using the -y parameter, only displays the differences. \n
Example 1: Comparing Two Files
\n[root@localhost test3]# diff log2014.log log2013.log\n3c3\n 2013-03\n8c8\n 2013-08\n11,12d10\n< 2013-11\n< 2013-12\nThe above "3c3" and "8c8" indicate that the content of log2014.log and log2013.log differs at lines 3 and 8; "11,12d10" indicates that the first file has two extra lines (11 and 12) compared to the second file.
\n\nExample 2: Side-by-Side Format Output
\n[root@localhost test3]# diff log2014.log log2013.log -y -W 50\n2013-01 2013-01\n2013-02 2013-02\n2014-03 | 2013-03\n2013-04 2013-04\n2013-05 2013-05\n2013-06 2013-06\n2013-07 2013-07\n2013-07 | 2013-08\n2013-09 2013-09\n2013-10 2013-10\n2013-11 <\n2013-12 2013-11\n > 2013-12\nExplanation:
\n- \n
- "|" indicates that the content of the two files differs at that line. \n
- "<" indicates that the file on the right has one less line of content than the file on the left. \n
- ">" indicates that the file on the right has one more line of content than the file on the left. \n
YouTip