1.\" $OpenBSD: diff.1,v 1.47 2015/11/24 19:35:41 jmc 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.Dd February 26, 2024 31.Dt DIFF 1 32.Os 33.Sh NAME 34.Nm diff 35.Nd differential file and directory comparator 36.Sh SYNOPSIS 37.Nm diff 38.Op Fl aBbdipTtw 39.Oo 40.Fl c | e | f | 41.Fl n | q | u | y 42.Oc 43.Op Fl -brief 44.Op Fl -color Ns = Ns Ar when 45.Op Fl -changed-group-format Ar GFMT 46.Op Fl -ed 47.Op Fl -expand-tabs 48.Op Fl -forward-ed 49.Op Fl -ignore-all-space 50.Op Fl -ignore-case 51.Op Fl -ignore-space-change 52.Op Fl -initial-tab 53.Op Fl -minimal 54.Op Fl -no-ignore-file-name-case 55.Op Fl -normal 56.Op Fl -rcs 57.Op Fl -show-c-function 58.Op Fl -starting-file 59.Op Fl -speed-large-files 60.Op Fl -strip-trailing-cr 61.Op Fl -tabsize Ar number 62.Op Fl -text 63.Op Fl -unified 64.Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern 65.Op Fl F Ar pattern | Fl -show-function-line Ar pattern 66.Op Fl L Ar label | Fl -label Ar label 67.Ar file1 file2 68.Nm diff 69.Op Fl aBbdilpTtw 70.Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern 71.Op Fl F Ar pattern | Fl -show-function-line Ar pattern 72.Op Fl L Ar label | Fl -label Ar label 73.Op Fl -brief 74.Op Fl -color Ns = Ns Ar when 75.Op Fl -changed-group-format Ar GFMT 76.Op Fl -ed 77.Op Fl -expand-tabs 78.Op Fl -forward-ed 79.Op Fl -ignore-all-space 80.Op Fl -ignore-case 81.Op Fl -ignore-space-change 82.Op Fl -initial-tab 83.Op Fl -minimal 84.Op Fl -no-ignore-file-name-case 85.Op Fl -normal 86.Op Fl -paginate 87.Op Fl -rcs 88.Op Fl -show-c-function 89.Op Fl -speed-large-files 90.Op Fl -starting-file 91.Op Fl -strip-trailing-cr 92.Op Fl -tabsize Ar number 93.Op Fl -text 94.Fl C Ar number | -context Ar number 95.Ar file1 file2 96.Nm diff 97.Op Fl aBbdiltw 98.Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern 99.Op Fl -brief 100.Op Fl -color Ns = Ns Ar when 101.Op Fl -changed-group-format Ar GFMT 102.Op Fl -ed 103.Op Fl -expand-tabs 104.Op Fl -forward-ed 105.Op Fl -ignore-all-space 106.Op Fl -ignore-case 107.Op Fl -ignore-space-change 108.Op Fl -initial-tab 109.Op Fl -minimal 110.Op Fl -no-ignore-file-name-case 111.Op Fl -normal 112.Op Fl -paginate 113.Op Fl -rcs 114.Op Fl -show-c-function 115.Op Fl -speed-large-files 116.Op Fl -starting-file 117.Op Fl -strip-trailing-cr 118.Op Fl -tabsize Ar number 119.Op Fl -text 120.Fl D Ar string | Fl -ifdef Ar string 121.Ar file1 file2 122.Nm diff 123.Op Fl aBbdilpTtw 124.Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern 125.Op Fl F Ar pattern | Fl -show-function-line Ar pattern 126.Op Fl L Ar label | Fl -label Ar label 127.Op Fl -brief 128.Op Fl -color Ns = Ns Ar when 129.Op Fl -changed-group-format Ar GFMT 130.Op Fl -ed 131.Op Fl -expand-tabs 132.Op Fl -forward-ed 133.Op Fl -ignore-all-space 134.Op Fl -ignore-case 135.Op Fl -ignore-space-change 136.Op Fl -initial-tab 137.Op Fl -minimal 138.Op Fl -no-ignore-file-name-case 139.Op Fl -normal 140.Op Fl -paginate 141.Op Fl -rcs 142.Op Fl -show-c-function 143.Op Fl -speed-large-files 144.Op Fl -starting-file 145.Op Fl -strip-trailing-cr 146.Op Fl -tabsize Ar number 147.Op Fl -text 148.Fl U Ar number | Fl -unified Ar number 149.Ar file1 file2 150.Nm diff 151.Op Fl aBbdilNPprsTtw 152.Oo 153.Fl c | e | f | 154.Fl n | q | u 155.Oc 156.Op Fl -brief 157.Op Fl -color Ns = Ns Ar when 158.Op Fl -changed-group-format Ar GFMT 159.Op Fl -context 160.Op Fl -ed 161.Op Fl -expand-tabs 162.Op Fl -forward-ed 163.Op Fl -ignore-all-space 164.Op Fl -ignore-case 165.Op Fl -ignore-space-change 166.Op Fl -initial-tab 167.Op Fl -minimal 168.Op Fl -new-file 169.Op Fl -no-ignore-file-name-case 170.Op Fl -normal 171.Op Fl -paginate 172.Op Fl -rcs 173.Op Fl -recursive 174.Op Fl -report-identical-files 175.Op Fl -show-c-function 176.Op Fl -speed-large-files 177.Op Fl -strip-trailing-cr 178.Op Fl -tabsize Ar number 179.Op Fl -text 180.Op Fl -unidirectional-new-file 181.Op Fl -unified 182.Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern 183.Op Fl F Ar pattern | Fl -show-function-line Ar pattern 184.Bk -words 185.Op Fl L Ar label | Fl -label Ar label 186.Op Fl S Ar name | Fl -starting-file Ar name 187.Op Fl X Ar file | Fl -exclude-from Ar file 188.Op Fl x Ar pattern | Fl -exclude Ar pattern 189.Ek 190.Ar dir1 dir2 191.Nm diff 192.Op Fl aBbditwW 193.Op Fl -color Ns = Ns Ar when 194.Op Fl -expand-tabs 195.Op Fl -ignore-all-blanks 196.Op Fl -ignore-blank-lines 197.Op Fl -ignore-case 198.Op Fl -minimal 199.Op Fl -no-ignore-file-name-case 200.Op Fl -strip-trailing-cr 201.Op Fl -suppress-common-lines 202.Op Fl -tabsize Ar number 203.Op Fl -text 204.Op Fl -width 205.Fl y | Fl -side-by-side 206.Ar file1 file2 207.Nm diff 208.Op Fl -help 209.Op Fl -version 210.Sh DESCRIPTION 211The 212.Nm 213utility compares the contents of 214.Ar file1 215and 216.Ar file2 217and writes to the standard output the list of changes necessary to 218convert one file into the other. 219No output is produced if the files are identical. 220.Pp 221Output options (mutually exclusive): 222.Bl -tag -width Ds 223.It Fl C Ar number Fl -context Ar number 224Like 225.Fl c 226but produces a diff with 227.Ar number 228lines of context. 229.It Fl c 230Produces a diff with 3 lines of context. 231With 232.Fl c 233the output format is modified slightly: 234the output begins with identification of the files involved and 235their creation dates and then each change is separated 236by a line with fifteen 237.Li * Ns 's . 238The lines removed from 239.Ar file1 240are marked with 241.Sq \&-\ \& ; 242those added to 243.Ar file2 244are marked 245.Sq +\ \& . 246Lines which are changed from one file to the other are marked in 247both files with 248.Sq !\ \& . 249Changes which lie within 3 lines of each other are grouped together on 250output. 251.It Fl D Ar string Fl -ifdef Ar string 252Creates a merged version of 253.Ar file1 254and 255.Ar file2 256on the standard output, with C preprocessor controls included so that 257a compilation of the result without defining 258.Ar string 259is equivalent to compiling 260.Ar file1 , 261while defining 262.Ar string 263will yield 264.Ar file2 . 265.It Fl e -ed 266Produces output in a form suitable as input for the editor utility, 267.Xr ed 1 , 268which can then be used to convert file1 into file2. 269.Pp 270Extra commands are added to the output when comparing directories with 271.Fl e , 272so that the result is a 273.Xr sh 1 274script for converting text files which are common to the two directories 275from their state in 276.Ar dir1 277to their state in 278.Ar dir2 . 279.It Fl f -forward-ed 280Identical output to that of the 281.Fl e 282flag, but in reverse order. 283It cannot be digested by 284.Xr ed 1 . 285.It Fl -help 286This option prints a summary to stdout and exits with status 0. 287.It Fl n 288Produces a script similar to that of 289.Fl e , 290but in the opposite order and with a count of changed lines on each 291insert or delete command. 292This is the form used by rcsdiff. 293.It Fl q -brief 294Just print a line when the files differ. 295Does not output a list of changes. 296.It Fl U Ar number Fl -unified Ar number 297Like 298.Fl u 299but produces a diff with 300.Ar number 301lines of context. 302.It Fl u 303Produces a 304.Em unified 305diff with 3 lines of context. 306A unified diff is similar to the context diff produced by the 307.Fl c 308option. 309However, unlike with 310.Fl c , 311all lines to be changed (added and/or removed) are present in 312a single section. 313.It Fl -version 314This option prints a version string to stdout and exits with status 0. 315.It Fl y Fl -side-by-side 316Output in two columns with a marker between them. 317The marker can be one 318of the following: 319.Pp 320.Bl -tag -width Ds -offset indent -compact 321.It space 322Corresponding lines are identical. 323.It '|' 324Corresponding lines are different. 325.It '<' 326Files differ and only the first file contains the line. 327.It '>' 328Files differ and only the second file contains the line. 329.El 330.El 331.Pp 332Comparison options: 333.Bl -tag -width Ds 334.It Fl a -text 335Treat all files as ASCII text. 336Normally 337.Nm 338will simply print 339.Dq Binary files ... differ 340if files contain binary characters. 341Use of this option forces 342.Nm 343to produce a diff. 344.It Fl B Fl -ignore-blank-lines 345Causes chunks that include only blank lines to be ignored. 346.It Fl b -ignore-space-change 347Causes trailing blanks (spaces and tabs) to be ignored, and other 348strings of blanks to compare equal. 349.It Fl -color= Ns Oo Ar when Oc 350Color the additions green, and removals red, or the value in the 351.Ev DIFFCOLORS 352environment variable. 353The possible values of 354.Ar when 355are 356.Dq Cm never , 357.Dq Cm always 358and 359.Dq Cm auto . 360.Cm auto 361will use color if the output is a tty and the 362.Ev COLORTERM 363environment variable is set to a non-empty string. 364.It Fl d -minimal 365Try very hard to produce a diff as small as possible. 366This may consume a lot of processing power and memory when processing 367large files with many changes. 368.It Fl F Ar pattern, Fl -show-function-line Ar pattern 369Like 370.Fl p, 371but display the last line that matches provided pattern. 372.It Fl I Ar pattern Fl -ignore-matching-lines Ar pattern 373Ignores changes, insertions, and deletions whose lines match the 374extended regular expression 375.Ar pattern . 376Multiple 377.Fl I 378patterns may be specified. 379All lines in the change must match some pattern for the change to be 380ignored. 381See 382.Xr re_format 7 383for more information on regular expression patterns. 384.It Fl i -ignore-case 385Ignores the case of letters. 386E.g., 387.Dq A 388will compare equal to 389.Dq a . 390.It Fl l -paginate 391Pass the output through 392.Xr pr 1 393to paginate it. 394.It Fl L Ar label Fl -label Ar label 395Print 396.Ar label 397instead of the first (and second, if this option is specified twice) 398file name and time in the context or unified diff header. 399.It Fl p -show-c-function 400With unified and context diffs, show with each change 401the first 40 characters of the last line before the context beginning 402with a letter, an underscore or a dollar sign. 403For C and Objective-C source code following standard layout conventions, this 404will show the prototype of the function the change applies to. 405.It Fl T -initial-tab 406Print a tab rather than a space before the rest of the line for the 407normal, context or unified output formats. 408This makes the alignment of tabs in the line consistent. 409.It Fl t -expand-tabs 410Will expand tabs in output lines. 411Normal or 412.Fl c 413output adds character(s) to the front of each line which may screw up 414the indentation of the original source lines and make the output listing 415difficult to interpret. 416This option will preserve the original source's indentation. 417.It Fl w -ignore-all-blanks 418Is similar to 419.Fl b -ignore-space-change 420but causes whitespace (blanks and tabs) to be totally ignored. 421E.g., 422.Dq if (\ \&a == b \&) 423will compare equal to 424.Dq if(a==b) . 425.It Fl W Ar number Fl -width Ar number 426Output at most 427.Ar number 428columns when using side by side format. 429The default value is 130. 430Note that unless 431.It Fl t 432was specified, 433.Nm 434will always align the second column to a tab stop, so values of 435.Fl -width 436smaller than approximately five times the value of 437.Fl -tabsize 438may yield surprising results. 439.It Fl -changed-group-format Ar GFMT 440Format input groups in the provided 441.Pp 442the format is a string with special keywords: 443.Bl -tag -width %< 444.It %< 445lines from FILE1 446.It %< 447lines from FILE2 448.El 449.It Fl -ignore-file-name-case 450ignore case when comparing file names 451.It Fl -no-ignore-file-name-case 452do not ignore case when comparing file names (default) 453.It Fl -normal 454default diff output 455.It Fl -speed-large-files 456stub option for compatibility with GNU diff 457.It Fl -strip-trailing-cr 458strip carriage return on input files 459.It Fl -suppress-common-lines 460Do not output common lines when using the side by side format 461.It Fl -tabsize Ar number 462Number of spaces representing a tab (default 8) 463.El 464.Pp 465Directory comparison options: 466.Bl -tag -width Ds 467.It Fl N -new-file 468If a file is found in only one directory, act as if it was found in the 469other directory too but was of zero size. 470.It Fl P -unidirectional-new-file 471If a file is found only in 472.Ar dir2 , 473act as if it was found in 474.Ar dir1 475too but was of zero size. 476.It Fl r -recursive 477Causes application of 478.Nm 479recursively to common subdirectories encountered. 480.It Fl S Ar name Fl -starting-file Ar name 481Re-starts a directory 482.Nm 483in the middle, beginning with file 484.Ar name . 485.It Fl s -report-identical-files 486Causes 487.Nm 488to report files which are the same, which are otherwise not mentioned. 489.It Fl X Ar file Fl -exclude-from Ar file 490Exclude files and subdirectories from comparison whose basenames match 491lines in 492.Ar file . 493Multiple 494.Fl X 495options may be specified. 496.It Fl x Ar pattern Fl -exclude Ar pattern 497Exclude files and subdirectories from comparison whose basenames match 498.Ar pattern . 499Patterns are matched using shell-style globbing via 500.Xr fnmatch 3 . 501Multiple 502.Fl x 503options may be specified. 504.El 505.Pp 506If both arguments are directories, 507.Nm 508sorts the contents of the directories by name, and then runs the 509regular file 510.Nm 511algorithm, producing a change list, 512on text files which are different. 513Binary files which differ, 514common subdirectories, and files which appear in only one directory 515are described as such. 516In directory mode only regular files and directories are compared. 517If a non-regular file such as a device special file or FIFO is encountered, 518a diagnostic message is printed. 519.Pp 520If only one of 521.Ar file1 522and 523.Ar file2 524is a directory, 525.Nm 526is applied to the non-directory file and the file contained in 527the directory file with a filename that is the same as the 528last component of the non-directory file. 529.Pp 530If either 531.Ar file1 532or 533.Ar file2 534is 535.Sq - , 536the standard input is 537used in its place. 538.Ss Output Style 539The default (without 540.Fl e , 541.Fl c , 542or 543.Fl n -rcs 544.\" -C 545options) 546output contains lines of these forms, where 547.Va XX , YY , ZZ , QQ 548are line numbers respective of file order. 549.Pp 550.Bl -tag -width "XX,YYcZZ,QQ" -compact 551.It Li XX Ns Ic a Ns Li YY 552At (the end of) line 553.Va XX 554of 555.Ar file1 , 556append the contents 557of line 558.Va YY 559of 560.Ar file2 561to make them equal. 562.It Li XX Ns Ic a Ns Li YY,ZZ 563Same as above, but append the range of lines, 564.Va YY 565through 566.Va ZZ 567of 568.Ar file2 569to line 570.Va XX 571of file1. 572.It Li XX Ns Ic d Ns Li YY 573At line 574.Va XX 575delete 576the line. 577The value 578.Va YY 579tells to which line the change would bring 580.Ar file1 581in line with 582.Ar file2 . 583.It Li XX,YY Ns Ic d Ns Li ZZ 584Delete the range of lines 585.Va XX 586through 587.Va YY 588in 589.Ar file1 . 590.It Li XX Ns Ic c Ns Li YY 591Change the line 592.Va XX 593in 594.Ar file1 595to the line 596.Va YY 597in 598.Ar file2 . 599.It Li XX,YY Ns Ic c Ns Li ZZ 600Replace the range of specified lines with the line 601.Va ZZ . 602.It Li XX,YY Ns Ic c Ns Li ZZ,QQ 603Replace the range 604.Va XX , Ns Va YY 605from 606.Ar file1 607with the range 608.Va ZZ , Ns Va QQ 609from 610.Ar file2 . 611.El 612.Pp 613These lines resemble 614.Xr ed 1 615subcommands to convert 616.Ar file1 617into 618.Ar file2 . 619The line numbers before the action letters pertain to 620.Ar file1 ; 621those after pertain to 622.Ar file2 . 623Thus, by exchanging 624.Ic a 625for 626.Ic d 627and reading the line in reverse order, one can also 628determine how to convert 629.Ar file2 630into 631.Ar file1 . 632As in 633.Xr ed 1 , 634identical 635pairs (where num1 = num2) are abbreviated as a single 636number. 637.Sh ENVIRONMENT 638.Bl -tag -width DIFFCOLORS 639.It Ev DIFFCOLORS 640The value of this variable is the form 641.Ar add Ns : Ns Ar rm , 642where 643.Ar add 644is the ASCII escape sequence for additions and 645.Ar rm 646is the ASCII escape sequence for deletions. 647If this is unset, 648.Nm 649uses green for additions and red for removals. 650.El 651.Sh FILES 652.Bl -tag -width /tmp/diff.XXXXXXXX -compact 653.It Pa /tmp/diff.XXXXXXXX 654Temporary file used when comparing a device or the standard input. 655Note that the temporary file is unlinked as soon as it is created 656so it will not show up in a directory listing. 657.El 658.Sh EXIT STATUS 659The 660.Nm 661utility exits with one of the following values: 662.Pp 663.Bl -tag -width Ds -offset indent -compact 664.It 0 665No differences were found. 666.It 1 667Differences were found. 668.It >1 669An error occurred. 670.El 671.Pp 672The 673.Fl -help 674and 675.Fl -version 676options exit with a status of 0. 677.Sh EXAMPLES 678Compare 679.Pa old_dir 680and 681.Pa new_dir 682recursively generating an unified diff and treating files found only in one 683of those directories as new files: 684.Bd -literal -offset indent 685$ diff -ruN /path/to/old_dir /path/to/new_dir 686.Ed 687.Pp 688Same as above but excluding files matching the expressions 689.Dq *.h 690and 691.Dq *.c : 692.Bd -literal -offset indent 693$ diff -ruN -x '*.h' -x '*.c' /path/to/old_dir /path/to/new_dir 694.Ed 695.Pp 696Show a single line indicating if the files differ: 697.Bd -literal -offset indent 698$ diff -q /boot/loader.conf /boot/defaults/loader.conf 699Files /boot/loader.conf and /boot/defaults/loader.conf differ 700.Ed 701.Pp 702Assuming a file named 703.Pa example.txt 704with the following contents: 705.Bd -literal -offset indent 706FreeBSD is an operating system 707Linux is a kernel 708OpenBSD is an operating system 709.Ed 710.Pp 711Compare stdin with 712.Pa example.txt 713excluding from the comparison those lines containing either 714.Qq Linux 715or 716.Qq Open : 717.Bd -literal -offset indent 718$ echo "FreeBSD is an operating system" | diff -q -I 'Linux|Open' example.txt - 719.Ed 720.Sh SEE ALSO 721.Xr cmp 1 , 722.Xr comm 1 , 723.Xr diff3 1 , 724.Xr ed 1 , 725.Xr patch 1 , 726.Xr pr 1 , 727.Xr sdiff 1 728.Rs 729.%A James W. Hunt 730.%A M. Douglas McIlroy 731.%T "An Algorithm for Differential File Comparison" 732.%J Computing Science Technical Report 733.%Q Bell Laboratories 41 734.%D June 1976 735.Re 736.Sh STANDARDS 737The 738.Nm 739utility is compliant with the 740.St -p1003.1-2008 741specification. 742.Pp 743The flags 744.Op Fl aDdIiLlNnPpqSsTtwXxy 745are extensions to that specification. 746.Sh HISTORY 747A 748.Nm 749command appeared in 750.At v6 . 751The 752.Nm 753implimentation in 754.Fx 1.0 755until 756.Fx 11.4 757was GNU diff. This was replaced in 758.Fx 12.0 759by a BSD licensed diff implimentation. Some GNU'isms were lost in process. 760