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