1.\" $OpenBSD: diff.1,v 1.5 2003/06/25 18:52:46 jmc Exp $ 2.\" Copyright (c) 1980, 1990, 1993 3.\" The Regents of the University of California. All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 3. Neither the name of the University nor the names of its contributors 14.\" may be used to endorse or promote products derived from this software 15.\" without specific prior written permission. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" SUCH DAMAGE. 28.\" 29.\" @(#)diff.1 8.1 (Berkeley) 6/30/93 30.\" 31.Dd June 30, 1993 32.Dt DIFF 1 33.Os 34.Sh NAME 35.Nm diff 36.Nd differential file and directory comparator 37.Sh SYNOPSIS 38.Nm diff 39.Op Fl cefhn 40.Op Fl bitw 41.Ar file1 file2 42.Nm diff 43.Op Fl D Ns Ar string 44.Op Fl biw 45.Ar file1 file2 46.Nm diff 47.Op Fl l 48.Op Fl r 49.Op Fl s 50.Op Fl cefhn 51.Op Fl bitw 52.Op Fl S Ns Ar name 53.Ar dir1 dir2 54.Sh DESCRIPTION 55The 56.Nm 57utility compares the contents of 58.Ar file1 59and 60.Ar file2 61and writes to the standard output the list of changes necessary to 62convert one file into the other. 63No output is produced if the files are identical. 64.Pp 65Output options (mutually exclusive): 66.Bl -tag -width Ds 67.It Fl c 68Produces a diff with lines of context. 69The default is to present 3 lines of context and may be changed, e.g., to 10, 70by 71.Fl c10 . 72With 73.Fl c 74the output format is modified slightly: 75the output beginning with identification of the files involved and 76their creation dates and then each change is separated 77by a line with a dozen *'s. 78The lines removed from 79.Ar file1 80are marked with 81.Sq \-\ \& ; 82those added to 83.Ar file2 84are marked 85.Sq \+\ \& . 86Lines which are changed from one file to the other are marked in 87both files with 88.Sq !\ \& . 89Changes which lie within 90.Aq context 91lines of each other are grouped together on output. 92(This is a change from the previous 93.Dq diff -c 94but the resulting output is usually much easier to interpret.) 95.It Fl e 96Produces output in a form suitable as input for the editor utility, 97.Xr ed 1 , 98which can then be used to convert file1 into file2. 99.Pp 100Extra commands are added to the output when comparing directories with 101.Fl e , 102so that the result is a 103.Xr sh 1 104script for converting text files which are common to the two directories 105from their state in 106.Ar dir1 107to their state in 108.Ar dir2 . 109.It Fl f 110Identical output to that of the 111.Fl e 112flag, but in reverse order. 113It cannot be digested by 114.Xr ed 1 . 115.It Fl h 116Invokes an alternate algorithm which can handle files of very long lengths. 117There is a trade off. 118The algorithm can only deal with changes which are clearly delimited and brief. 119Long sections of changes and overlaps will confuse it. 120.It Fl n 121Produces a script similar to that of 122.Fl e , 123but in the opposite order and with a count of changed lines on each 124insert or delete command. 125This is the form used by 126.Xr rcsdiff 1 . 127.It Fl D Ns Ar string 128Creates a merged version of 129.Ar file1 130and 131.Ar file2 132on the standard output, with C preprocessor controls included so that 133a compilation of the result without defining 134.Ar string 135is equivalent to compiling 136.Ar file1 , 137while defining 138.Ar string 139will yield 140.Ar file2 . 141.El 142.Pp 143Comparison options: 144.Bl -tag -width Ds 145.It Fl b 146Causes trailing blanks (spaces and tabs) to be ignored, and other 147strings of blanks to compare equal. 148.It Fl i 149Ignores the case of letters. 150E.g., 151.Dq A 152will compare equal to 153.Dq a . 154.It Fl t 155Will expand tabs in output lines. 156Normal or 157.Fl c 158output adds character(s) to the front of each line which may screw up 159the indentation of the original source lines and make the output listing 160difficult to interpret. 161This option will preserve the original source's indentation. 162.It Fl w 163Is similar to 164.Fl b 165but causes whitespace (blanks and tabs) to be totally ignored. 166E.g., 167.Dq if (\ \&a == b \&) 168will compare equal to 169.Dq if(a==b) . 170.El 171.Pp 172Directory comparison options: 173.Bl -tag -width Ds 174.It Fl l 175Long output format; each text file 176.Nm diff Ns \'d 177is piped through 178.Xr pr 1 179to paginate it; 180other differences are remembered and summarized 181after all text file differences are reported. 182.It Fl r 183Causes application of 184.Nm 185recursively to common subdirectories encountered. 186.It Fl s 187Causes 188.Nm 189to report files which are the same, which are otherwise not mentioned. 190.It Fl S Ns Ar name 191Re-starts a directory 192.Nm 193in the middle, beginning with file 194.Ar name . 195.El 196.Pp 197If both arguments are directories, 198.Nm 199sorts the contents of the directories by name, and then runs the 200regular file 201.Nm 202algorithm, producing a change list, 203on text files which are different. 204Binary files which differ, 205common subdirectories, and files which appear in only one directory 206are described as such. 207.Pp 208If only one of 209.Ar file1 210and 211.Ar file2 212is a directory, 213.Nm 214is applied to the non-directory file and the file contained in 215the directory file with a filename that is the same as the 216last component of the non-directory file. 217.Pp 218If either 219.Ar file1 220or 221.Ar file2 222is 223.Sq Fl , 224the standard input is 225used in its place. 226.Ss Output Style 227The default (without 228.Fl e , 229.Fl c , 230or 231.Fl n 232.\" -C 233options) 234output contains lines of these forms, where 235.Va XX , YY , ZZ , QQ 236are line numbers respective of file order. 237.Pp 238.Bl -tag -width "XX,YYcZZ,QQ" -compact 239.It Li XX Ns Ic a Ns Li YY 240At (the end of) line 241.Va XX 242of 243.Ar file1 , 244append the contents 245of line 246.Va YY 247of 248.Ar file2 249to make them equal. 250.It Li XX Ns Ic a Ns Li YY,ZZ 251Same as above, but append the range of lines, 252.Va YY 253through 254.Va ZZ 255of 256.Ar file2 257to line 258.Va XX 259of file1. 260.It Li XX Ns Ic d Ns Li YY 261At line 262.Va XX 263delete 264the line. 265The value 266.Va YY 267tells to which line the change would bring 268.Ar file1 269in line with 270.Ar file1 . 271.It Li XX,YY Ns Ic d Ns Li ZZ 272Delete the range of lines 273.Va XX 274through 275.Va YY 276in 277.Ar file1 . 278.It Li XX Ns Ic c Ns Li YY 279Change the line 280.Va XX 281in 282.Ar file1 283to the line 284.Va YY 285in 286.Ar file2 . 287.It Li XX,YY Ns Ic c Ns Li ZZ 288Replace the range of specified lines with the line 289.Va ZZ . 290.It Li XX,YY Ns Ic c Ns Li ZZ,QQ 291Replace the range 292.Va XX , Ns YY 293from 294.Ar file1 295with the range 296.Va ZZ , Ns QQ 297from 298.Ar file2 . 299.El 300.Pp 301These lines resemble 302.Xr ed 1 303subcommands to convert 304.Ar file1 305into 306.Ar file2 . 307The line numbers before the action letters pertain to 308.Ar file1 ; 309those after pertain to 310.Ar file2 . 311Thus, by exchanging 312.Ic a 313for 314.Ic d 315and reading the line in reverse order, one can also 316determine how to convert 317.Ar file2 318into 319.Ar file1 . 320As in 321.Xr ed 1 , 322identical 323pairs (where num1 = num2) are abbreviated as a single 324number. 325.Sh ENVIRONMENT 326.Bl -tag -width TMPDIR 327.It Ev TMPDIR 328If the environment variable 329.Ev TMPDIR 330exists, 331.Nm 332will use the directory specified by 333.Ev TMPDIR 334as the temporary directory. 335.El 336.Sh FILES 337.Bl -tag -width /usr/bin/diffh -compact 338.It Pa /tmp/d????? 339.It Pa /usr/bin/diffh 340Alternate algorithm version (used by option 341.Fl h ) . 342.It Pa /usr/bin/diff 343For directory diffs. 344.It Pa /usr/bin/pr 345Used by the 346.Fl l 347option. 348.El 349.Sh DIAGNOSTICS 350The 351.Nm 352utility exits with one of the following values: 353.Pp 354.Bl -tag -width Ds -compact -offset indent 355.It \&0 356No differences were found. 357.It \&1 358Differences were found. 359.It \*[Gt]\&1 360An error occurred. 361.El 362.Sh SEE ALSO 363.Xr cc 1 , 364.Xr cmp 1 , 365.Xr comm 1 , 366.Xr diff3 1 , 367.Xr ed 1 368.Sh HISTORY 369A 370.Nm 371command appeared in 372.At v6 . 373.Sh BUGS 374The 375.Fl f 376and 377.Fl e 378options 379do not provide special handling for lines on which the 380first and only character is 381.Dq Li \&. . 382This can cause problems for 383.Xr ed 1 . 384.Pp 385When comparing directories with the 386.Fl b , 387.Fl w 388or 389.Fl i 390options specified, 391.Nm 392first compares the files ala 393.Xr cmp 1 , 394and then decides to run the 395.Nm 396algorithm if they are not equal. 397This may cause a small amount of spurious output if the files 398then turn out to be identical because the only differences are 399insignificant whitespace or case differences. 400