xref: /illumos-gate/usr/src/man/man1/diff.1 (revision 6597d6fcfd3058014e6518bb93c97eb0a42b4c3e)
1.\"
2.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3.\" permission to reproduce portions of its copyrighted documentation.
4.\" Original documentation from The Open Group can be obtained online at
5.\" http://www.opengroup.org/bookstore/.
6.\"
7.\" The Institute of Electrical and Electronics Engineers and The Open
8.\" Group, have given us permission to reprint portions of their
9.\" documentation.
10.\"
11.\" In the following statement, the phrase ``this text'' refers to portions
12.\" of the system documentation.
13.\"
14.\" Portions of this text are reprinted and reproduced in electronic form
15.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16.\" Standard for Information Technology -- Portable Operating System
17.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20.\" between these versions and the original IEEE and The Open Group
21.\" Standard, the original IEEE and The Open Group Standard is the referee
22.\" document.  The original Standard can be obtained online at
23.\" http://www.opengroup.org/unix/online.html.
24.\"
25.\" This notice shall appear on any product containing this material.
26.\"
27.\" The contents of this file are subject to the terms of the
28.\" Common Development and Distribution License (the "License").
29.\" You may not use this file except in compliance with the License.
30.\"
31.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32.\" or http://www.opensolaris.org/os/licensing.
33.\" See the License for the specific language governing permissions
34.\" and limitations under the License.
35.\"
36.\" When distributing Covered Code, include this CDDL HEADER in each
37.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38.\" If applicable, add the following below this CDDL HEADER, with the
39.\" fields enclosed by brackets "[]" replaced with your own identifying
40.\" information: Portions Copyright [yyyy] [name of copyright owner]
41.\"
42.\"
43.\" Copyright 1989 AT&T
44.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
45.\" Copyright (c) 2004, Sun Microsystems, Inc.  All Rights Reserved.
46.\" Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
47.\"
48.Dd February 23, 2022
49.Dt DIFF 1
50.Os
51.Sh NAME
52.Nm diff
53.Nd compare two files
54.Sh SYNOPSIS
55.Nm
56.Op Fl biqtw
57.Op Fl c | e | f | h | n | u
58.Ar file1
59.Ar file2
60.Nm
61.Op Fl biqtw
62.Op Fl C Ar number | Fl U Ar number
63.Ar file1
64.Ar file2
65.Nm
66.Op Fl biqtw
67.Op Fl D Ar string
68.Ar file1
69.Ar file2
70.Nm
71.Op Fl biqtw
72.Op Fl c | e | f | h | n | u
73.Op Fl l
74.Op Fl r
75.Op Fl s
76.Op Fl S Ar name
77.Ar directory1
78.Ar directory2
79.Sh DESCRIPTION
80The
81.Nm
82utility will compare the contents of
83.Ar file1
84and
85.Ar file2
86and write to standard output a list of changes necessary to convert
87.Ar file1
88into
89.Ar file2 .
90This list should be minimal.
91Except in rare circumstances,
92.Nm
93finds a smallest sufficient set of file differences.
94No output will be produced if the files are identical.
95.Pp
96The normal output contains lines of these forms:
97.Pp
98.Bl -item -offset indent -compact
99.Sm off
100.It
101.Ar n1 Sy a Ar n3 , n4
102.It
103.Ar n1 , n2 Sy d Ar n3
104.It
105.Ar n1 , n2 Sy c Ar n3 , n4
106.Sm on
107.El
108.Pp
109where
110.Ar n1
111and
112.Ar n2
113represent lines in
114.Ar file1
115and
116.Ar n3
117and
118.Ar n4
119represent lines in
120.Ar file2
121These lines resemble
122.Xr ed 1
123commands to convert
124.Ar file1
125to
126.Ar file2 .
127By exchanging
128.Sy a
129for
130.Sy d
131and reading backwards,
132.Ar file2
133can be converted to
134.Ar file1 .
135As in
136.Xr ed 1 ,
137identical pairs, where
138.Ar n1 Ns = Ns Ar n2
139or
140.Ar n3 Ns = Ns Ar n4
141are abbreviated as a single number.
142.Pp
143Following each of these lines come all the lines that are affected in the first
144file flagged by
145.Sq < ,
146then all the lines that are affected in the second file flagged by
147.Sq > .
148.Sh OPTIONS
149The following options are supported:
150.Bl -tag -width Ds
151.It Fl b
152Ignores trailing blanks
153.Pq spaces and tabs
154and treats other strings of blanks as equivalent.
155.It Fl i
156Ignores the case of letters.
157For example, `
158.Sq A
159will compare equal to
160.Sq a .
161.It Fl q
162report only when files differ
163.It Fl t
164Expands TAB characters in output lines.
165Normal or
166.Fl c
167output adds character(s) to the front of each line that may adversely affect
168the indentation of the original source lines and make the output lines
169difficult to interpret.
170This option will preserve the original source's indentation.
171.It Fl w
172Ignores all blanks
173.Pq SPACE and TAB characters
174and treats all other strings of blanks as equivalent.
175For example,
176.Ql if ( a == b )
177will compare equal to
178.Ql if (a==b) .
179.El
180.Pp
181The following options are mutually exclusive:
182.Bl -tag -width Ds
183.It Fl c
184Produces a listing of differences with three lines of context.
185With this option, output format is modified slightly.
186That is, output begins with identification of the files involved and their
187creation dates, then each change is separated by a line with a dozen
188asterisks
189.Pq \&* .
190The lines removed from
191.Ar file1
192are marked with
193.Sq -- .
194The lines added to
195.Ar file2
196are marked
197.Sq \&+ .
198Lines that are changed from one file to the other are marked in both files with
199.Sq \&! .
200.It Fl C Ar number
201Produces a listing of differences identical to that produced by
202.Fl c
203with
204.Ar number
205lines of context.
206.It Fl D Ar string
207Creates a merged version of
208.Ar file1
209and
210.Ar file2
211with C preprocessor controls included so that a compilation of the result
212without defining
213.Ar string
214is equivalent to compiling
215.Ar file1 ,
216while defining
217.Ar string
218will yield
219.Ar file2 .
220.It Fl e
221Produces a script of only
222.Sy a ,
223.Sy c ,
224and
225.Sy d
226commands for the editor
227.Xr ed 1 ,
228which will recreate
229.Ar file2
230from
231.Ar file1 .
232In connection with the
233.Fl e
234option, the following shell program may help maintain multiple versions of a
235file.
236Only an ancestral file
237.Pq $1
238and a chain of version-to-version
239.Sy ed
240scripts
241.Pq $2,$3,...
242made by
243.Nm
244need be on hand.
245A
246.Dq latest version
247appears on the standard output.
248.Pp
249.Dl (shift; cat $*; echo a'1,$p') | ed - $1
250.It Fl f
251Produces a similar script, not useful with
252.Xr ed 1 ,
253in the opposite order.
254.It Fl h
255Does a fast, half-hearted job.
256It works only when changed stretches are short and well separated, but does
257work on files of unlimited length.
258Options
259.Fl c ,
260.Fl C ,
261.Fl D ,
262.Fl e ,
263.Fl f ,
264and
265.Fl n
266are unavailable with
267.Fl h .
268.Nm
269does not descend into directories with this option.
270.It Fl n
271Produces a script similar to
272.Fl e ,
273but in the opposite order and with a count of changed lines on each insert or
274delete command.
275.It Fl u
276Produces a listing of differences with three lines of context.
277The output is similar to that of the
278.Fl c
279option, except that the context is
280.Dq unified .
281Removed and changed lines in
282.Ar file1
283are marked by a
284.Sq \&-
285while lines added or changed in
286.Ar file2
287are marked by a
288.Sq \&+ .
289Both versions of changed lines appear in the output, while added, removed, and
290context lines appear only once.
291The identification of
292.Ar file1
293and
294.Ar file2
295is different, with
296.Dq ---
297and
298.Dq +++
299being printed where
300.Dq ***
301and
302.Dq ---
303would appear with the
304.Fl c
305option.
306Each change is separated by a line of the form
307.Pp
308.D1 Cm @@ Cm \&- Ns Ar n1 , Ns Ar n2 Cm + Ns Ar n3 , Ns Ar n4 Cm @@
309.It Fl U Ar number
310Produces a listing of differences identical to that produced by
311.Fl u
312with
313.Ar number
314lines of context.
315.El
316.Pp
317The following options are used for comparing directories:
318.Bl -tag -width Ds
319.It Fl l
320Produces output in long format.
321Before the
322.Nm ,
323each text file is piped through
324.Xr pr 1
325to paginate it.
326Other differences are remembered and summarized after all text file differences
327are reported.
328.It Fl r
329Applies
330.Nm
331recursively to common subdirectories encountered.
332.It Fl s
333Reports files that are identical.
334These identical files would not otherwise be mentioned.
335.It Fl S Ar name
336Starts a directory
337.Nm
338in the middle, beginning with the file
339.Ar name .
340.El
341.Sh OPERANDS
342The following operands are supported:
343.Pp
344.Bl -tag -offset Ds -width directory1 -compact
345.It Ar file1
346.It Ar file2
347A path name of a file or directory to be compared.
348If either
349.Ar file1
350or
351.Ar file2
352is
353.Sq \&- ,
354the standard input will be used in its place.
355.Pp
356.It Ar directory1
357.It Ar directory2
358A path name of a directory to be compared.
359.El
360.Pp
361If only one of
362.Ar file1
363and
364.Ar file2
365is a directory,
366.Nm
367will be applied to the non-directory file and the file contained in the
368directory file with a filename that is the same as the last component of the
369non-directory file.
370.Sh USAGE
371See
372.Xr largefile 7
373for the description of the behavior of
374.Nm
375when encountering files greater than or equal to 2 Gbyte
376.Pq  2^31 bytes .
377.Sh FILES
378.Bl -tag -width Ds
379.It Pa /tmp/d?????
380temporary file used for comparison
381.It Pa /usr/lib/diffh
382executable file for
383.Fl h
384option
385.El
386.Sh EXIT STATUS
387The following exit values are returned:
388.Bl -tag -width Ds
389.It 0
390No differences were found.
391.It 1
392Differences were found.
393.It >1
394An error occurred.
395.El
396.Sh EXAMPLES
397.Sy Example 1 No Typical output of the diff command
398.Pp
399In the following command,
400.Ar dir1
401is a directory containing a directory named
402.Pa x ,
403.Ar dir2
404is a directory containing a directory named
405.Pa x ,
406.Pa dir1/x
407and
408.Pa dir2/x
409both contain files named
410.Pa date.out ,
411and
412.Pa dir2/x
413contains a file named
414.Pa y :
415.Bd -literal -offset 4n
416example% diff -r dir1 dir2
417Common subdirectories: dir1/x and dir2/x
418Only in dir2/x: y
419diff -r dir1/x/date.out dir2/x/date.out
4201c1
421< Mon Jul  2 13:12:16 PDT 1990
422---
423> Tue Jun 19 21:41:39 PDT 1990
424.Ed
425.Sh ENVIRONMENT VARIABLES
426See
427.Xr environ 7
428for descriptions of the following environment variables that affect the
429execution of
430.Nm :
431.Ev LANG ,
432.Ev LC_ALL ,
433.Ev LC_CTYPE ,
434.Ev LC_MESSAGES ,
435.Ev LC_TIME ,
436and
437.Ev NLSPATH .
438.Bl -tag -width Ds
439.It Ev TZ
440Determines the locale for affecting the timezone used for calculating file
441timestamps written with the
442.Fl C
443and
444.Fl c
445options.
446.El
447.Sh INTERFACE STABILITY
448The command line interface of
449.Nm
450is
451.Sy Committed .
452The output of
453.Nm
454is
455.Sy Committed .
456.Sh SEE ALSO
457.Xr bdiff 1 ,
458.Xr cmp 1 ,
459.Xr comm 1 ,
460.Xr dircmp 1 ,
461.Xr ed 1 ,
462.Xr pr 1 ,
463.Xr sdiff 1 ,
464.Xr attributes 7 ,
465.Xr environ 7 ,
466.Xr largefile 7 ,
467.Xr standards 7
468.Sh NOTES
469Editing scripts produced under the
470.Fl e
471or
472.Fl f
473options are na\(:ive about creating lines consisting of a single dot
474.Sq \&. .
475.Pp
476Missing NEWLINE at end of file indicates that the last line of the file in
477question did not have a NEWLINE.
478If the lines are different, they will be flagged and output, although the
479output will seem to indicate they are the same.
480