8.4.8 diff command (compares two files)
Syntax
diff[-a][-b][-i][-s][-w] [-cnum-lines|-C num-lines|-q|-unum-lines|-U num-lines| -y[-W output-width] [--suppress-common-lines]] [-L label] path-name-1 path-name-2 diff[-a][-b][-i][-r][-s][-w] [-cnum-lines|-C num-lines|-q|-unum-lines|-U num-lines| -y[-W output-width] [--suppress-common-lines]] [-L label] directory-name-1 directory-name-2
Description
This command compares two files.
Arguments
-a
- --text
Specifies that the files are to be handled as text files.
-b
- --ignore-space-change
Specifies that multiple consecutive spaces or tabs in a line are to be compressed and handled as a single space or tab. The -b and -w options cannot both be specified at the same time.
-i
- --ignore-case
Specifies that differences in case are not to be considered (uppercase alphabetic letters are not to be distinguished from lowercase alphabetic letters).
-s
- --report-identical-files
Specifies that a message (Files path-name-1 and path-name-2 are identical) is to be output if the contents of the files are identical.
-w
- --ignore-all-space
Specifies that all spaces and tabs in a line are to be ignored. The -w and -b options cannot both be specified at the same time.
-cnum-lines
-C num-lines
- --context[=num-lines]
Specifies that the path names being compared are to be output to the standard output with additions, deletions, and changes to lines indicated by the symbols +, -, and !, respectively.
The number of lines specified in num-lines are to be output for context before and after each difference. If num-lines is not specified in the -c and --context options, the default is 3 lines.
Note that when num-lines is specified in the -c option, there must not be any spaces between -c and num-lines.
-q
- --brief
Specifies that if there are differences between the files, only the message Files path-name-1 and path-name-2 differ is to be output.
-unum-lines
-U num-lines
- --unified[=num-lines]
Specifies that the path names being compared are to be output to the standard output with additions and deletions indicated by the symbols + and -, respectively. The old and new text are output together as a single section.
The number of lines specified in num-lines are to be output for context before and after each difference. If num-lines is not specified in the -u and --unified options, the default is 3 lines.
Note that when num-lines is specified in the -c option, there must not be any spaces between -u and num-lines.
-L label
- --label=label
When the -c, -u, -C, or -U option is specified, specifies that labels are to be output in place of the path names. If one label is specified, it is output in place of path-name-1. If two labels are specified, they are output in place of path-name-1 and path-name-2, respectively, in the order they are specified.
-y
- --side-by-side
Specifies that the symbols >, <, |, \, and / are to be used to indicate the difference information.
The >, <, |, \, and / symbols are output only for lines in which there is a difference in terms of line addition, deletion, or change or whether an end-of-line code is used; no symbol is output for lines in which there are no differences. The path-name-1 and path-name-2 lines are combined on one line and output side by side.
If the length of one combined line exceeds 130 columns, the lengths of the path-name-1 and path-name-2 lines are adjusted before the lines are output.
This option can be combined with the following options to change the output:
-W option
--suppress-common-lines option
-W output-width
- --width=output-width
Specifies a change in the output width (number of columns) to be output per line. This option take effect when it is specified together with the -y option.
- --suppress-common-lines
Specifies that lines that have no differences are not to be output. This option takes effect when it is specified together with the -y option.
- path-name-1
Specifies the path name of the comparison source file.
Specify - to read the contents to be compared from the standard input. A temporary file will be created to store input from the standard input. The temporary file is output to the following directory:
UNIX
Directory specified in the TMPDIR environment variable, or /var/tmp if no value has been set for TMPDIR.
Windows
common-application-data-folder\HITACHI\JP1AS\misc
- path-name-2
Specifies the path name of the comparison target file.
Specify - to read the contents to be compared from the standard input. A temporary file will be created to store input from the standard input. The temporary file is output to the following directory:
UNIX
Directory specified in the TMPDIR environment variable, or /var/tmp if no value has been set for TMPDIR.
Windows
common-application-data-folder\HITACHI\JP1AS\misc
-r
- --recursive
If directories are being compared, specifies that subdirectories are to be traversed recursively.
- directory-name-1
Specifies a directory as the comparison source. If you specify a file path name for either directory-name-1 or directory-name-2, the command searches for a file with the same name in the other directory. If no file with the same name is found, an error message (diff: path-name-to-compare: No such file or directory) is output.
- directory-name-2
Specifies a directory as the comparison target. If you specify a file path name for either directory-name-1 or directory-name-2, the command searches for a file with the same name in the other directory. If no file with the same name is found, an error message (diff: path-name-to-compare: No such file or directory) is output.
Display formats
The diff command provides the three display formats shown below for displaying the differences between files. The output format that is used depends on the specification of options.
Format |
Meaning |
---|---|
Traditional display format |
This output format is used when none of the -c, -C, -q, -u, -U and -y options is specified. This format displays the differences between the two files, as well as the start and end positions of the differences. In between the start and end positions, it displays the following symbols, which represent the differences:
If a difference extends across multiple lines, the numbers of the start and end lines are shown separated by the comma (,). A difference from path-name-1 and a difference from path-name-2 are displayed in this order separated from each other by ---. < at the beginning of a line indicates a deletion or change from the first input file, while > indicates an addition or change from the second input file. One space is output after < and >. |
Context format |
This output format is used when the -c or -C option is specified. In addition to displaying the differences between the two files, it also displays unchanged lines before and after each difference. You can specify the number of such context lines to be displayed. The default is three lines. Headers display the following information about the two files:
The differences are expressed as follows:
A single space is output after +, -, and !. Two spaces are output at the beginning of a line on which there are no differences. Adjacent changed lines are treated as a single change block. However, when there is a gap between changed lines, another 15 asterisks are displayed and then the differences are displayed. |
Unified format |
This output format is used when the -u or -U option is specified. It displays the context format output as a single section. You can specify the number of lines of context to be displayed. The default is three lines. Headers display the following information for the two files:
The differences are displayed as follows:
No space is output after + or -, unlike in the context format. One space is output at the beginning of an unchanged line. Only added and deleted lines represent differences. Adjacent changed lines are treated as a single change block. However, when there is a gap between changed lines, @@ are again used to identify lines that constitute the starting and ending positions of the differences between the two files, followed by the differences themselves. |
Side-by-side format |
This output format is used when the -y option is specified. The path-name-1 and path-name-2 lines are combined on one line and output side by side. By default, all lines are output regardless of whether there are any differences on lines. If the length of one combined line exceeds 130 columns, the lengths of the path-name-1 and path-name-2 lines are adjusted so that they can be displayed side by side within 130 columns. The following symbols are displayed before the lines to indicate the difference:
By combining the -y option with the -W and --suppress-common-lines options, you can change the output width per line and suppress output of lines that have no difference. |
Traditional display format example
The following example illustrates the traditional display format.
- Output example
C:\USR\JP1\oscmd\bin>diff file1 file2 1c1,2 <--1. < aaaaaaaaaaa <--2. --- <--3. > aaAAAAAaaaa <--4. > bbBBBBBbbbb <--4.
- Explanation
This shows the position of a difference between file1 and file2. To represent the difference between file1 and file 2, the symbol a means added, d means deleted, and c means changed. The line number from file1 is displayed before the symbol, and the line number from file2 is displayed after it. If there are differences that span multiple lines, the start line and end line numbers are separated by the comma (,).
This shows the difference line in file1.
This shows the boundary between the difference lines in file1 and file2.
This shows the difference lines in file2.
Context format example
- Output example
C:\USR\JP1\oscmd\bin>diff -c file1 file2 *** file1 Thu May 12 20:17:54 2011 <--1. --- file2 Thu May 12 20:18:29 2011 <--2. *************** <--3. *** 1,5 **** <--4. aaaaaaaaaaa <--5. ! bbbbbbbb <--5. cccccccccccc <--5. - dddddddddddd <--5. eeeeeeeee <--5. --- 1,5 ---- <--6. aaaaaaaaaaa <--7. ! bbbBBBbb <--7. cccccccccccc <--7. eeeeeeeee <--7. + ffffffffffffffffff <--7.
- Explanation
This shows the file name and most recent modification date and time as the information about file1.
This shows the file name and most recent modification date and time as the information about file2.
This shows a row of 15 asterisks (*), which represents the border between change blocks. Whenever changed lines are separated by three or more lines, this border is displayed again, and then the next change block showing differences between file1 and file2 is output.
This shows the start and end positions of the file1 differences, separated by the comma (,).
This shows the file1 differences. + means added, - means deleted, and ! means changed.
This shows the start and end positions of the file2 differences, separated by the comma (,).
This shows the file2 differences. + means added, - means deleted, and ! means changed.
Unified format example
- Output example
C:\USR\JP1\oscmd\bin>diff -u file1 file2 --- file1 Thu May 12 20:17:54 2011 <--1. +++ file2 Thu May 12 20:18:29 2011 <--2. @@ -1,5 +1,5 @@ <--3. aaaaaaaaaaa <--4. -bbbbbbbb <--4. +bbbBBBbb <--4. cccccccccccc <--4. -dddddddddddd <--4. eeeeeeeee <--4. +ffffffffffffffffff <--4.
- Explanation
This shows the file name and most recent modification date and time as the information about file1.
This shows the file name and most recent modification date and time as the information about file2.
This shows the start and end positions of the file1 changes, separated by the comma (,), a space, and then the start and end positions of the file2 changes, separated by the comma (,). The start and end positions of the file1 changes are prefixed with a -, and the start and end positions of the file2 changes are prefixed with a +.
This displays as a single section the differences between file1 and file2. + indicates a line that was added in file2 from file1, and - indicates a line that was deleted in file2 from file1. Deleted lines and added lines constitute the changed portions.
Side-by-side format example
- Output example
C:\USR\JP1\oscmd\bin>diff -y file1 file2 a a <--1. b | b1 <--1. c c <--1. d < <--1. e e <--1. > f <--1. g \ g <--1.
- Explanation
Each line of file1 and file2 is combined into one line and then output side by side.
> indicates a line that is not in file1 but was added in file2.
< indicates a line that is in file1 but was deleted from file2.
| indicates a line in file1 that has been changed in file2.
\ indicates a line in file1 that has no end-of-line code.
A line with no symbol has no differences.
Return codes
Return code |
Meaning |
---|---|
0 |
The files are identical. |
1 |
The files are different. |
2 or greater |
Error termination |
Notes
If more than one of the -c, -C, -q, -u, -U, and -y options is specified, the one specified last takes effect.
A file is considered to be binary if the first 8,192 bytes of the file includes any characters other than printable single-byte characters, spaces, tabs, backspaces, and multibyte characters.
Files whose character encoding differs from the local character encoding are considered to be binary files.
In Windows, input and output are performed in the binary mode for files and for the standard input and standard output. No conversion of end-of-line codes is performed.
If - was specified for a path name and you interrupt execution of the diff command while the standard input is being read from the terminal or while comparison processing is underway, a temporary file with the name shown below might remain. Delete such a temporary file manually.
- In Windows:
diff.XXXXXX (XXXXXX: any character string consisting of six characters)
- In UNIX:
diffppppp.XXXXXXXX (ppppp: process ID consisting of five or more digits; XXXXXXXX: any character string consisting of eight characters)
Examples
These usage examples illustrate the results of executing the diff command on files whose contents are shown below. represents a space and represents a tab character.
abc.txt
aaaaaaaaaaa bbbbbbbb cccccccccccccccc dddddddddddd eeeeeeeeeeee
abcd.txt
aaAAAAAaaaa bbBBBbbb cccccccccccccccc dddddddddddd eeeeeeeeeeee
wxy.txt
aaaaaaaaaaa bbbbbbbb xxxxxxxxxxxxxx cccccccccccccccc dddddddddddd eeeeeeeeeeee fffffffffffffff ggggggggg
wxyz.txt
aaaaaaaaaaa bbbBBBbb xxxxxxxxxxxxxx cccccccccccccccc dddddddddddd fffffffffffffff ggggggggg hhhhhhhhhhhhhhhhhh
The following examples illustrate the results of executing the command on the files shown above.
Display the default with no options specified.
C:\TEMP>%ADSH_OSCMD_DIR%\diff abc.txt abcd.txt 1c1 < aaaaaaaaaaa --- > aaAAAAAaaaa 3c3 < bbbbbbbb --- > bbBBBbbb 7,10c7,10 < < < < --- > > > > 12c12 < eeeeeeeeeeee --- > eeeeeeeeeeee
Specify the -b option to ignore differences in the number of spaces or tabs.
C:\TEMP>%ADSH_OSCMD_DIR%\diff -b abc.txt abcd.txt 1c1 < aaaaaaaaaaa --- > aaAAAAAaaaa 3c3 < bbbbbbbb --- > bbBBBbbb 12c12 < eeeeeeeeeeee --- > eeeeeeeeeeee
Specify the -i option to compare without distinguishing between uppercase and lowercase letters.
C:\TEMP>%ADSH_OSCMD_DIR%\diff -i abc.txt abcd.txt 7,10c7,10 < < < < --- > > > > 12c12 < eeeeeeeeeeee --- > eeeeeeeeeeee
Specify the -s option to report when the contents of the files are the same.
C:\TEMP>%ADSH_OSCMD_DIR%\diff -s abc.txt abc.txt Files abc.txt and abc.txt are identical
Specify the -w option to ignore all spaces and tabs in a line.
C:\TEMP>%ADSH_OSCMD_DIR%\diff -w abc.txt abcd.txt 1c1 < aaaaaaaaaaa --- > aaAAAAAaaaa 3c3 < bbbbbbbb --- > bbBBBbbb
Specify the -q option to only report whether the files are different, without displaying the differences.
C:\TEMP>%ADSH_OSCMD_DIR%\diff -q abc.txt abcd.txt Files abc.txt and abcd.txt differ
Specify the -c option to indicate added, deleted, and changed lines with the symbols +, -, and !.
C:\TEMP>%ADSH_OSCMD_DIR%\diff -c ..\dir1\wxy.txt ..\dir1\wxyz.txt *** wxy.txt Thu May 12 20:17:54 2011 --- wxyz.txt Thu May 12 20:18:29 2011 *************** *** 1,10 **** aaaaaaaaaaa ! bbbbbbbb xxxxxxxxxxxxxx cccccccccccccccc dddddddddddd - eeeeeeeeeeee fffffffffffffff ggggggggg --- 1,10 ---- aaaaaaaaaaa ! bbbBBBbb xxxxxxxxxxxxxx cccccccccccccccc dddddddddddd fffffffffffffff ggggggggg + hhhhhhhhhhhhhhhhhh
Specify the -u option to indicate added and deleted lines with the symbols + and -. The differences are displayed as a single section.
C:\TEMP>%ADSH_OSCMD_DIR%\diff -u ..\dir1\wxy.txt ..\dir1\wxyz.txt --- wxy.txt Thu May 12 20:17:54 2011 +++ wxyz.txt Thu May 12 20:18:29 2011 @@ -1,10 +1,10 @@ aaaaaaaaaaa -bbbbbbbb +bbbBBBbb xxxxxxxxxxxxxx cccccccccccccccc dddddddddddd -eeeeeeeeeeee fffffffffffffff ggggggggg +hhhhhhhhhhhhhhhhhh
Specify the -C option to display a single line for context before and after a difference.
C:\TEMP>%ADSH_OSCMD_DIR%\diff -C1 wxy.txt wxyz.txt *** wxy.txt Thu May 12 20:17:54 2011 --- wxyz.txt Thu May 12 20:18:29 2011 *************** *** 2,4 **** ! bbbbbbbb xxxxxxxxxxxxxx --- 2,4 ---- ! bbbBBBbb xxxxxxxxxxxxxx *************** *** 7,10 **** dddddddddddd - eeeeeeeeeeee fffffffffffffff ggggggggg --- 7,10 ---- dddddddddddd fffffffffffffff ggggggggg + hhhhhhhhhhhhhhhhhh
Specify the -U option to indicate added and deleted lines with the symbols + and -. The differences are displayed as a single section, with a single line of context before and after a difference.
C:\TEMP>%ADSH_OSCMD_DIR%\diff -U1 wxy.txt wxyz.txt --- wxy.txt Thu May 12 20:17:54 2011 +++ wxyz.txt Thu May 12 20:18:29 2011 @@ -2,3 +2,3 @@ -bbbbbbbb +bbbBBBbb xxxxxxxxxxxxxx @@ -7,4 +7,4 @@ dddddddddddd -eeeeeeeeeeee fffffffffffffff ggggggggg +hhhhhhhhhhhhhhhhhh
Specify the -y option and display line additions, deletions, and changes with the >, <, and | symbols.
C:\TEMP>%ADSH_OSCMD_DIR%\diff -y wxy.txt wxyz.txt aaaaaaaaaaa aaaaaaaaaaa bbbbbbbb | bbbBBBbb xxxxxxxxxxxxxx xxxxxxxxxxxxxx cccccccccccccccc cccccccccccccccc dddddddddddd dddddddddddd eeeeeeeeeeee < fffffffffffffff fffffffffffffff ggggggggg ggggggggg > hhhhhhhhhhhhhhhhhh
Specify the -y option to display lines in side-by-side format and also specify the --suppress-common-lines option to suppress output of lines that have no differences.
C:\TEMP>%ADSH_OSCMD_DIR%diff -y --suppress-common-lines wxy.txt wxyz.txt bbbbbbbb | bbbBBBbb eeeeeeeeeeee < > hhhhhhhhhhhhhhhhhh
Display the comparison source file name as a label specified with the -L option.
C:\TEMP>%ADSH_OSCMD_DIR%\diff -L name1 -c abc.txt abcd.txt *** name1 --- abcd.txt Thu May 12 20:36:44 2011 *************** *** 1,12 **** ! aaaaaaaaaaa ! bbbbbbbb cccccccccccccccc ! ! ! ! dddddddddddd ! eeeeeeeeeeee --- 1,12 ---- ! aaAAAAAaaaa ! bbBBBbbb cccccccccccccccc ! ! ! ! dddddddddddd ! eeeeeeeeeeee
Compare binary files without specifying the -a option.
C:\TEMP>%ADSH_OSCMD_DIR%\diff binaryfile1 binaryfile2 Binary files binaryfile1 and binaryfile2 differ
Display an option error message.
This message might vary depending on the platform on which the command is executed. The following shows an example in Windows:
C:\TEMP>%ADSH_OSCMD_DIR%\diff -z diff: illegal option -- z usage: diff [-abisw] [-c[number] | -C number | -q | -u[number] | -U number] | -y [-W columns] [--suppress-common-lines]] [-L label] file1 file2 diff [-abirsw] [-c[number] | -C number | -q | -u[number] | -U number] | -y [-W columns] [--suppress-common-lines]] [-L label] dir1 dir2
Display an error message if a file does not exist.
C:\TEMP>%ADSH_OSCMD_DIR%\diff file99 file123 diff: file99: No such file or directory