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