English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux diff command

Comprehensive List of Linux Commands

The Linux diff command is used to compare file differences.

diff compares the differences between text files line by line. If a directory is specified, diff will compare files with the same name in the directory but will not compare files in subdirectories.

Syntax

diff  [-abBcdefHilnNpPqrstTuvwy][-<line number>][-C  <line number>][-D  <macro name>][-I  <character or string>][-S  <file>][-W  <width>][-x  <file or directory>][-X  <file>][--help][--left-column][--suppress-common-line][file or directory1][file or directory2]

parameter:

  • -<line number>  Specify how many lines of text to display. This parameter must be used with-or-use the u parameter together.

  • -or--text  diff preset only compares text files line by line.

  • -b or--ignore-space-change  Does not check for differences in space characters.

    • -B or--ignore-blank-lines  Does not check for blank lines.

    • -c  Displays the full content and marks the differences.

    • -C<line number> or--context<line number>  With the execution"-c-<line number>"Command is the same.

    • -d or--minimal  Uses different algorithms to compare 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 the ed script file.

    • -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 file.

    • -H or--speed-large-files  Can speed up the process when comparing large files.

    • -l<character or string> or--ignore-matching-lines<character or string>  If two files differ in some lines, and these lines both contain the character or string specified in the option, then do not display the differences between these two files.

    • -i or--ignore-case  Does not check for differences in case.

    • -l or--paginate  Hands the result over to the pr program for pagination.

    • -n or--rcs  Displays the comparison result in the RCS format.

    • -N or--new-file  When comparing directories, if file A only appears in a certain directory, the default is to display:

    • Only in directory: If file A is used in-N parameter, then diff will compare file A with an empty file.

    • -p  If the compared file is a C program code file, displays the function name where the difference is located.

    • -P or--unidirectional-new-file  With-N similar, but only compares the file that the second directory contains and the first directory does not to an empty file.

    • -q or--brief  Displays only whether there are differences without showing detailed information.

    • -r or--recursive  Compares files in subdirectories.

    • -s or--report-identical-files  Displays information even if no differences are found.

    • -S<file> or--starting-file<file>  When comparing directories, starts the comparison from the specified file.

    • -t or--expand-tabs  Expands tab characters when outputting.

    • -T or--initial-tab  Adds a tab character at the beginning of each line for alignment.

    • -u,-U<column number> or--unified=<column number>  Displays the differences in file content in a merged manner.

    • -v or--version  Displays version information.

    • -w or--ignore-all-space  Ignore all space characters.

    • -W<width> or--width<width>  When using-When using y parameter, specify the column width.

    • -x<filename or directory> or--exclude<filename or directory>  Do not compare the specified files or directories 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 differences between files in parallel.

    • --help  Display help.

    • --left-column  When using-When using y parameter, if the content of a line in two files is the same, then only the content in the left column is displayed.

    • --suppress-common-lines  When using-When using y parameter, only the differences are displayed.

    Example1: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 above "3c3" and "8c8" indicates that the log2014.log and log20143log file is in3Line and the8Line content is different;11,12d10" indicates that the first file has more than the second file11And12Line.

    Example2Side-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

    Note:

    • "|" indicates the content before and after2There are different contents in the file

    • "<" indicates that the file after it is smaller than the file before it1Line Content

    • ">" indicates that the file after it is larger than the file before it1Line Content

    Comprehensive List of Linux Commands