1.\" $OpenBSD: diff.1,v 1.37 2010/02/21 15:24:01 sobrado Exp $ 2.\" 3.\" Copyright (c) 1980, 1990, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)diff.1 8.1 (Berkeley) 6/30/93 31.\" 32.Dd $Mdocdate: February 21 2010 $ 33.Dt DIFF 1 34.Os 35.Sh NAME 36.Nm diff 37.Nd differential file and directory comparator 38.Sh SYNOPSIS 39.Nm diff 40.Op Fl abdilpTtw 41.Oo 42.Fl c | e | f | 43.Fl n | q | u 44.Oc 45.Op Fl I Ar pattern 46.Op Fl L Ar label 47.Ar file1 file2 48.Nm diff 49.Op Fl abdilpTtw 50.Op Fl I Ar pattern 51.Op Fl L Ar label 52.Fl C Ar number 53.Ar file1 file2 54.Nm diff 55.Op Fl abdiltw 56.Op Fl I Ar pattern 57.Fl D Ar string 58.Ar file1 file2 59.Nm diff 60.Op Fl abdilpTtw 61.Op Fl I Ar pattern 62.Op Fl L Ar label 63.Fl U Ar number 64.Ar file1 file2 65.Nm diff 66.Op Fl abdilNPprsTtw 67.Oo 68.Fl c | e | f | 69.Fl n | q | u 70.Oc 71.Op Fl I Ar pattern 72.Bk -words 73.Op Fl L Ar label 74.Op Fl S Ar name 75.Op Fl X Ar file 76.Op Fl x Ar pattern 77.Ek 78.Ar dir1 dir2 79.Sh DESCRIPTION 80The 81.Nm 82utility compares the contents of 83.Ar file1 84and 85.Ar file2 86and writes to the standard output the list of changes necessary to 87convert one file into the other. 88No output is produced if the files are identical. 89.Pp 90Output options (mutually exclusive): 91.Bl -tag -width Ds 92.It Fl C Ar number 93Like 94.Fl c 95but produces a diff with 96.Ar number 97lines of context. 98.It Fl c 99Produces a diff with 3 lines of context. 100With 101.Fl c 102the output format is modified slightly: 103the output begins with identification of the files involved and 104their creation dates and then each change is separated 105by a line with fifteen 106.Li * Ns 's . 107The lines removed from 108.Ar file1 109are marked with 110.Sq \&-\ \& ; 111those added to 112.Ar file2 113are marked 114.Sq \+\ \& . 115Lines which are changed from one file to the other are marked in 116both files with 117.Sq !\ \& . 118Changes which lie within 3 lines of each other are grouped together on 119output. 120.It Fl D Ar string 121Creates a merged version of 122.Ar file1 123and 124.Ar file2 125on the standard output, with C preprocessor controls included so that 126a compilation of the result without defining 127.Ar string 128is equivalent to compiling 129.Ar file1 , 130while defining 131.Ar string 132will yield 133.Ar file2 . 134.It Fl e 135Produces output in a form suitable as input for the editor utility, 136.Xr ed 1 , 137which can then be used to convert file1 into file2. 138.Pp 139Extra commands are added to the output when comparing directories with 140.Fl e , 141so that the result is a 142.Xr sh 1 143script for converting text files which are common to the two directories 144from their state in 145.Ar dir1 146to their state in 147.Ar dir2 . 148.It Fl f 149Identical output to that of the 150.Fl e 151flag, but in reverse order. 152It cannot be digested by 153.Xr ed 1 . 154.It Fl n 155Produces a script similar to that of 156.Fl e , 157but in the opposite order and with a count of changed lines on each 158insert or delete command. 159This is the form used by 160.Xr rcsdiff 1 . 161.It Fl q 162Just print a line when the files differ. 163Does not output a list of changes. 164.It Fl U Ar number 165Like 166.Fl u 167but produces a diff with 168.Ar number 169lines of context. 170.It Fl u 171Produces a 172.Em unified 173diff with 3 lines of context. 174A unified diff is similar to the context diff produced by the 175.Fl c 176option. 177However, unlike with 178.Fl c , 179all lines to be changed (added and/or removed) are present in 180a single section. 181.El 182.Pp 183Comparison options: 184.Bl -tag -width Ds 185.It Fl a 186Treat all files as 187.Tn ASCII 188text. 189Normally 190.Nm 191will simply print 192.Dq Binary files ... differ 193if files contain binary characters. 194Use of this option forces 195.Nm 196to produce a diff. 197.It Fl b 198Causes trailing blanks (spaces and tabs) to be ignored, and other 199strings of blanks to compare equal. 200.It Fl d 201Try very hard to produce a diff as small as possible. 202This may consume a lot of processing power and memory when processing 203large files with many changes. 204.It Fl I Ar pattern 205Ignores changes, insertions, and deletions whose lines match the 206extended regular expression 207.Ar pattern . 208Multiple 209.Fl I 210patterns may be specified. 211All lines in the change must match some pattern for the change to be 212ignored. 213See 214.Xr re_format 7 215for more information on regular expression patterns. 216.It Fl i 217Ignores the case of letters. 218E.g., 219.Dq A 220will compare equal to 221.Dq a . 222.It Fl L Ar label 223Print 224.Ar label 225instead of the first (and second, if this option is specified twice) 226file name and time in the context or unified diff header. 227.It Fl l 228Long output format; each text file 229.Nm diff Ns \'d 230is piped through 231.Xr pr 1 232to paginate it; 233other differences are remembered and summarized 234after all text file differences are reported. 235.It Fl p 236With unified and context diffs, show with each change 237the first 40 characters of the last line before the context beginning 238with a letter, an underscore or a dollar sign. 239For C source code following standard layout conventions, this will 240show the prototype of the function the change applies to. 241.It Fl T 242Print a tab rather than a space before the rest of the line for the 243normal, context or unified output formats. 244This makes the alignment of tabs in the line consistent. 245.It Fl t 246Will expand tabs in output lines. 247Normal or 248.Fl c 249output adds character(s) to the front of each line which may screw up 250the indentation of the original source lines and make the output listing 251difficult to interpret. 252This option will preserve the original source's indentation. 253.It Fl w 254Is similar to 255.Fl b 256but causes whitespace (blanks and tabs) to be totally ignored. 257E.g., 258.Dq if (\ \&a == b \&) 259will compare equal to 260.Dq if(a==b) . 261.El 262.Pp 263Directory comparison options: 264.Bl -tag -width Ds 265.It Fl N 266If a file is found in only one directory, act as if it was found in the 267other directory too but was of zero size. 268.It Fl P 269If a file is found only in 270.Ar dir2 , 271act as if it was found in 272.Ar dir1 273too but was of zero size. 274.It Fl r 275Causes application of 276.Nm 277recursively to common subdirectories encountered. 278.It Fl S Ar name 279Re-starts a directory 280.Nm 281in the middle, beginning with file 282.Ar name . 283.It Fl s 284Causes 285.Nm 286to report files which are the same, which are otherwise not mentioned. 287.It Fl X Ar file 288Exclude files and subdirectories from comparison whose basenames match 289lines in 290.Ar file . 291Multiple 292.Fl X 293options may be specified. 294.It Fl x Ar pattern 295Exclude files and subdirectories from comparison whose basenames match 296.Ar pattern . 297Patterns are matched using shell-style globbing via 298.Xr fnmatch 3 . 299Multiple 300.Fl x 301options may be specified. 302.El 303.Pp 304If both arguments are directories, 305.Nm 306sorts the contents of the directories by name, and then runs the 307regular file 308.Nm 309algorithm, producing a change list, 310on text files which are different. 311Binary files which differ, 312common subdirectories, and files which appear in only one directory 313are described as such. 314In directory mode only regular files and directories are compared. 315If a non-regular file such as a device special file or 316.Tn FIFO 317is encountered, a diagnostic message is printed. 318.Pp 319If only one of 320.Ar file1 321and 322.Ar file2 323is a directory, 324.Nm 325is applied to the non-directory file and the file contained in 326the directory file with a filename that is the same as the 327last component of the non-directory file. 328.Pp 329If either 330.Ar file1 331or 332.Ar file2 333is 334.Sq Fl , 335the standard input is 336used in its place. 337.Ss Output Style 338The default (without 339.Fl e , 340.Fl c , 341or 342.Fl n 343.\" -C 344options) 345output contains lines of these forms, where 346.Va XX , YY , ZZ , QQ 347are line numbers respective of file order. 348.Pp 349.Bl -tag -width "XX,YYcZZ,QQ" -compact 350.It Li XX Ns Ic a Ns Li YY 351At (the end of) line 352.Va XX 353of 354.Ar file1 , 355append the contents 356of line 357.Va YY 358of 359.Ar file2 360to make them equal. 361.It Li XX Ns Ic a Ns Li YY,ZZ 362Same as above, but append the range of lines, 363.Va YY 364through 365.Va ZZ 366of 367.Ar file2 368to line 369.Va XX 370of file1. 371.It Li XX Ns Ic d Ns Li YY 372At line 373.Va XX 374delete 375the line. 376The value 377.Va YY 378tells to which line the change would bring 379.Ar file1 380in line with 381.Ar file1 . 382.It Li XX,YY Ns Ic d Ns Li ZZ 383Delete the range of lines 384.Va XX 385through 386.Va YY 387in 388.Ar file1 . 389.It Li XX Ns Ic c Ns Li YY 390Change the line 391.Va XX 392in 393.Ar file1 394to the line 395.Va YY 396in 397.Ar file2 . 398.It Li XX,YY Ns Ic c Ns Li ZZ 399Replace the range of specified lines with the line 400.Va ZZ . 401.It Li XX,YY Ns Ic c Ns Li ZZ,QQ 402Replace the range 403.Va XX , Ns Va YY 404from 405.Ar file1 406with the range 407.Va ZZ , Ns Va QQ 408from 409.Ar file2 . 410.El 411.Pp 412These lines resemble 413.Xr ed 1 414subcommands to convert 415.Ar file1 416into 417.Ar file2 . 418The line numbers before the action letters pertain to 419.Ar file1 ; 420those after pertain to 421.Ar file2 . 422Thus, by exchanging 423.Ic a 424for 425.Ic d 426and reading the line in reverse order, one can also 427determine how to convert 428.Ar file2 429into 430.Ar file1 . 431As in 432.Xr ed 1 , 433identical 434pairs (where num1 = num2) are abbreviated as a single 435number. 436.Sh ENVIRONMENT 437.Bl -tag -width TMPDIR 438.It Ev TMPDIR 439If the environment variable 440.Ev TMPDIR 441exists, 442.Nm 443will use the directory specified by 444.Ev TMPDIR 445as the temporary directory. 446.El 447.Sh FILES 448.Bl -tag -width /tmp/diff.XXXXXXXX -compact 449.It Pa /tmp/diff. Ns Ar XXXXXXXX 450Temporary file used when comparing a device or the standard input. 451Note that the temporary file is unlinked as soon as it is created 452so it will not show up in a directory listing. 453.El 454.Sh DIAGNOSTICS 455The 456.Nm 457utility exits with one of the following values: 458.Pp 459.Bl -tag -width Ds -compact -offset indent 460.It 0 461No differences were found. 462.It 1 463Differences were found. 464.It \*(Gt1 465An error occurred. 466.El 467.Sh SEE ALSO 468.Xr cmp 1 , 469.Xr comm 1 , 470.Xr diff3 1 , 471.Xr ed 1 , 472.Xr pr 1 , 473.Xr sdiff 1 , 474.Xr fnmatch 3 , 475.Xr re_format 7 476.Sh STANDARDS 477The 478.Nm 479utility is compliant with the 480.St -p1003.1-2008 481specification. 482.Pp 483The flags 484.Op Fl aDdIiLlNnPpqSsTtwXx 485are extensions to that specification. 486.Sh HISTORY 487A 488.Nm 489command appeared in 490.At v6 . 491.Sh BUGS 492When comparing directories with the 493.Fl b , 494.Fl w 495or 496.Fl i 497options specified, 498.Nm 499first compares the files ala 500.Xr cmp 1 , 501and then decides to run the 502.Nm 503algorithm if they are not equal. 504This may cause a small amount of spurious output if the files 505then turn out to be identical because the only differences are 506insignificant whitespace or case differences. 507