DIFF(1POSIX)

๐Ÿ’– DIFF(1POSIX) ๐Ÿ’–

okay so bestie, welcome to the POSIX Programmer's Manual โ€” this page lives in the POSIX Programmer's Manual, and heads up: the Linux version of this interface might do its own little thing (so like, go check the matching Linux man page for the tea โ˜•), or it might not even be implemented on Linux at all. iykyk


โœจ NAME โœจ

diff โ€” compare two files ๐Ÿซถ


โœจ SYNOPSIS โœจ

diff [-c|-e|-f|-u|-C n|-U n] [-br] file1 file2

โœจ DESCRIPTION โœจ

So the diff utility compares what's inside file1 and file2 and writes to standard output a list of the changes you'd need to make to turn file1 into file2. This list should be minimal, bestie โ€” no filler, we're efficient.

And if the files are literally identical? No output. Nothing. Silence. Iconic. ๐Ÿ’…


โœจ OPTIONS โœจ

The diff utility shall conform to the Base Definitions volume of POSIX.1โ€2017, Section 12.2, Utility Syntax Guidelines. (yes we're being official about it, slay)

The following options shall be supported:

  • -b โ€” white-space forgiveness era. Cause any amount of white space at the end of a line to be treated as a single <newline> (meaning: the white-space characters sitting right before the <newline> get ignored), and other strings of white-space characters โ€” not including <newline> characters โ€” to compare equal.
  • -c โ€” Produce output in a form that provides three lines of copied context. ๐ŸŽ€
  • -C n โ€” Produce output in a form that provides n lines of copied context (where n shall be interpreted as a positive decimal integer).
  • -e โ€” Produce output in a form suitable as input for the ed utility, which can then be used to convert file1 into file2.
  • -f โ€” Produce output in an alternative form, similar in format to -e, but not intended to be suitable as input for the ed utility, and in the opposite order. ๐Ÿ”„
  • -r โ€” Apply diff recursively to files and directories of the same name when file1 and file2 are both directories.

The diff utility shall detect infinite loops; that is, entering a previously visited directory that is an ancestor of the last file encountered. When it detects an infinite loop, diff shall write a diagnostic message to standard error and shall either recover its position in the hierarchy or terminate. (not the loop bestie, we do NOT do infinite loops ๐Ÿ’€)

  • -u โ€” Produce output in a form that provides three lines of unified context. โญ
  • -U n โ€” Produce output in a form that provides n lines of unified context (where n shall be interpreted as a non-negative decimal integer).
๐ŸŒธ careful bestie: -C n wants a positive decimal integer, but -U n is out here accepting non-negative. Different vibes, same option shape. Don't mix them up!

โœจ OPERANDS โœจ

The following operands shall be supported:

  • file1, file2 โ€” A pathname of a file to be compared. If either the file1 or file2 operand is '-', the standard input shall be used in its place. ๐Ÿฆ‹

If both file1 and file2 are directories, diff shall not compare block special files, character special files, or FIFO special files to any files, and shall not compare regular files to directories. Further details are as specified in Diff Directory Comparison Format. The behavior of diff on other file types is implementation-defined when found in directories. (so like... it depends on your implementation, don't @ me)

If only one of file1 and file2 is a directory, diff shall be applied to the non-directory file and the file contained in the directory file with a filename that is the same as the last component of the non-directory file. ๐ŸŽฏ


โœจ STDIN โœจ

The standard input shall be used only if one of the file1 or file2 operands references standard input. See the INPUT FILES section. ๐Ÿ’œ


โœจ INPUT FILES โœจ

The input files may be of any type. literally anything, we don't judge.


โœจ ENVIRONMENT VARIABLES โœจ

The following environment variables shall affect the execution of diff:

  • LANG โ€” Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1โ€2017, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.) ๐ŸŒˆ
  • LC_ALL โ€” If set to a non-empty string value, override the values of all the other internationalization variables. (she's the main character, she overrides everyone)
  • LC_CTYPE โ€” Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments and input files).
  • LC_MESSAGES โ€” Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error and informative messages written to standard output. ๐Ÿ’ฌ
  • LC_TIME โ€” Determine the locale for affecting the format of file timestamps written with the -C and -c options. ๐Ÿ•
  • NLSPATH โ€” Determine the location of message catalogs for the processing of LC_MESSAGES.
  • TZ โ€” Determine the timezone used for calculating file timestamps written with a context format. If TZ is unset or null, an unspecified default timezone shall be used. ๐ŸŒ

โœจ ASYNCHRONOUS EVENTS โœจ

Default. (nothing spicy here, we move on) โœจ

๐ŸŒˆ STDOUT ๐ŸŒˆ

Bestie, this is where it gets juicy, because diff has like a million little output formats and they're all iconic in their own way. Let's go ๐Ÿ’…

๐ŸŽ€ Diff Directory Comparison Format ๐ŸŽ€

If file1 and file2 are both directories, then the following output formats shall be used, no cap.

In the POSIX locale, every file that's present in only one directory gets reported like this:

"Only in %s: %s\n", <directory pathname>, <filename>

In the POSIX locale, subdirectories that the two directories have in common may be reported like this:

"Common subdirectories: %s and %s\n", <directory1 pathname>,
    <directory2 pathname>

Now for each file that's common to both directories: if the two files are not to be compared โ€” like if they have the same device ID and file serial number, or they're both block special files pointing at the same device, or they're both character special files pointing at the same device โ€” then in the POSIX locale the output format is unspecified ๐Ÿ™ˆ. Otherwise, in the POSIX locale an unspecified format shall be used that contains the pathnames of the two files.

For each file common to both directories, if the files are compared and they're identical? No output shall be written. Silence. Peace. ๐Ÿ•Š๏ธ But if the two files differ, this format gets written:

"diff %s %s %s\n", <diff_options>, <filename1>, <filename2>

where diff_options are the options as specified on the command line, fr fr.

Heads up: all directory pathnames listed in this section shall be relative to the original command line arguments. And all the other file names listed here shall be filenames (pathname components). Don't get it twisted ๐Ÿซถ

๐ŸŒธ Diff Binary Output Format ๐ŸŒธ

In the POSIX locale, if one or both of the files being compared are not text files, it's implementation-defined whether diff uses the binary file output format or the other formats specified below. The binary file output format shall contain the pathnames of the two files being compared and the string "differ".

If both files being compared are text files, then depending on the options you picked, one of the following formats shall be used to write out the differences. Let's meet the whole family ๐Ÿ’–


โœจ Diff Default Output Format โœจ

The default (so, without -e, -f, -c, -C, -u, or -U options) diff utility output shall contain lines of these forms:

"%da%d\n", <num1>, <num2>

"%da%d,%d\n", <num1>, <num2>, <num3>

"%dd%d\n", <num1>, <num2>

"%d,%dd%d\n", <num1>, <num2>, <num3>

"%dc%d\n", <num1>, <num2>

"%d,%dc%d\n", <num1>, <num2>, <num3>

"%dc%d,%d\n", <num1>, <num2>, <num3>

"%d,%dc%d,%d\n", <num1>, <num2>, <num3>, <num4>

These lines are giving ed subcommand energy, except their whole job is converting file1 into file2. The line numbers before the action letters shall pertain to file1; the ones after shall pertain to file2. So if you swap a for d and read the line in reverse order, you can also figure out how to convert file2 into file1. Iconic, honestly. And just like in ed, identical pairs (where num1 = num2) get abbreviated as a single number.

After each of these lines, diff shall write to standard output all lines affected in the first file using the format:

"< %s", <line>

and all lines affected in the second file using the format:

"> %s", <line>

If there are lines affected in both file1 and file2 (like with the c subcommand), the changes get separated with a line consisting of three <hyphen-minus> characters:

"---\n"

๐Ÿ’œ Diff -e Output Format ๐Ÿ’œ

With the -e option, a script shall be produced that โ€” when you feed it to ed as input, along with an appended w (write) command โ€” converts file1 into file2. Only the a (append), c (change), d (delete), i (insert), and s (substitute) commands of ed shall be used in this script. Text lines, except those consisting of the single character <period> (.), shall be output as they appear in the file.


๐Ÿฆ‹ Diff -f Output Format ๐Ÿฆ‹

With the -f option, an alternative format of script shall be produced. It's similar to what -e gives you, but with these differences, bestie:

  1. It's expressed in reverse sequence; the output of -e orders changes from the end of the file to the beginning; the -f goes from beginning to end.
  2. The command form <lines> <command-letter> used by -e is reversed. For example, 10c with -e would be c10 with -f.
  3. The form used for ranges of line numbers is <space>-separated, rather than <comma>-separated.

๐Ÿ’… Diff -c or -C Output Format ๐Ÿ’…

With the -c or -C option, the output format shall consist of affected lines along with surrounding lines of context. The affected lines shall show which ones need to be deleted or changed in file1, and those added from file2. With the -c option, three lines of context, if available, shall be written before and after the affected lines. With the -C option, the user can specify how many lines of context get written. The exact format follows.

The name and last modification time of each file shall be output in the following format:

"*** %s %s\n", file1, <file1 timestamp>
"--- %s %s\n", file2, <file2 timestamp>

Each file field shall be the pathname of the corresponding file being compared. The pathname written for standard input is unspecified ๐Ÿ™ˆ.

In the POSIX locale, each timestamp field shall be equivalent to the output from the following command:

date "+%a %b %e %T %Y"

without the trailing <newline>, executed at the time of last modification of the corresponding file (or the current time, if the file is standard input).

Then, the following output formats shall be applied for every set of changes.

First, a line shall be written in the following format:

"***************\n"

Next, the range of lines in file1 shall be written in the following format if the range contains two or more lines:

"*** %d,%d ****\n", <beginning line number>, <ending line number>

and the following format otherwise:

"*** %d ****\n", <ending line number>

The ending line number of an empty range shall be the number of the preceding line, or 0 if the range is at the start of the file.

Next, the affected lines along with lines of context (unaffected lines) shall be written. Unaffected lines shall be written in the following format:

"  %s", <unaffected_line>

Deleted lines shall be written as:

"- %s", <deleted_line>

Changed lines shall be written as:

"! %s", <changed_line>

Next, the range of lines in file2 shall be written in the following format if the range contains two or more lines:

"--- %d,%d ----\n", <beginning line number>, <ending line number>

and the following format otherwise:

"--- %d ----\n", <ending line number>

Then, lines of context and changed lines shall be written as described in the previous formats. Lines added from file2 shall be written in the following format:

"+ %s", <added_line>

๐ŸŒŸ Diff -u or -U Output Format ๐ŸŒŸ

The -u or -U options behave like the -c or -C options, except that the context lines are not repeated; instead, the context, deleted, and added lines are all shown together, interleaved. Periodt. The exact format follows.

The name and last modification time of each file shall be output in the following format:

"--- %s\t%s%s %s\n", file1, <file1 timestamp>, <file1 frac>, <file1 zone>
"+++ %s\t%s%s %s\n", file2, <file2 timestamp>, <file2 frac>, <file2 zone>

Each file field shall be the pathname of the corresponding file being compared, or the single character - if standard input is being compared. However, if the pathname contains a <tab> or a <newline>, or if it does not consist entirely of characters taken from the portable character set, the behavior is implementation-defined.

Each timestamp field shall be equivalent to the output from the following command:

date '+%Y-%m-%d %H:%M:%S'

without the trailing <newline>, executed at the time of last modification of the corresponding file (or the current time, if the file is standard input).

Each frac field shall be either empty, or a decimal point followed by at least one decimal digit, indicating the fractional-seconds part (if any) of the file timestamp. The number of fractional digits shall be at least the number needed to represent the file's timestamp without loss of information.

Each zone field shall be of the form "shhmm", where "shh" is a signed two-digit decimal number in the range -24 through +25, and "mm" is an unsigned two-digit decimal number in the range 00 through 59. It represents the timezone of the timestamp as the number of hours (hh) and minutes (mm) east (+) or west (-) of UTC for the timestamp. If the hours and minutes are both zero, the sign shall be '+'. However, if the timezone is not an integral number of minutes away from UTC, the zone field is implementation-defined.

Then, the following output formats shall be applied for every set of changes.

First, the range of lines in each file shall be written in the following format:

"@@ -%s +%s @@", <file1 range>, <file2 range>

Each range field shall be of the form:

"%1d", <beginning line number>

or:

"%1d,1", <beginning line number>

if the range contains exactly one line, and:

"%1d,%1d", <beginning line number>, <number of lines>

otherwise. If a range is empty, its beginning line number shall be the number of the line just before the range, or 0 if the empty range starts the file.

Next, the affected lines along with lines of context shall be written. Each non-empty unaffected line shall be written in the following format:

" %s", <unaffected_line>

where the contents of the unaffected line shall be taken from file1. It's implementation-defined whether an empty unaffected line is written as an empty line or a line containing a single <space> character. This line also represents the same line of file2, even though file2's line may contain different contents due to the -b. Deleted lines shall be written as:

"-%s", <deleted_line>

Added lines shall be written as:

"+%s", <added_line>

The order of lines written shall be the same as that of the corresponding file. A deleted line shall never be written immediately after an added line. Not ever. ๐Ÿ’€

If -U n is specified, the output shall contain no more than 2n consecutive unaffected lines; and if the output contains an affected line and this line is adjacent to up to n consecutive unaffected lines in the corresponding file, the output shall contain these unaffected lines. -u shall act like -U3.


๐Ÿซถ STDERR ๐Ÿซถ

The standard error shall be used only for diagnostic messages. That's it, that's the whole vibe.


๐ŸŽ€ OUTPUT FILES ๐ŸŽ€

None.


๐ŸŒˆ EXTENDED DESCRIPTION ๐ŸŒˆ

None.


โญ EXIT STATUS โญ

The following exit values shall be returned:

  • 0 โ€” No differences were found. Bestie, the files are twinsies ๐Ÿ’–
  • 1 โ€” Differences were found.
  • >1 โ€” An error occurred. ๐Ÿ˜ญ

๐Ÿ’€ CONSEQUENCES OF ERRORS ๐Ÿ’€

Default.

The following sections are informative.

๐ŸŒธ APPLICATION USAGE

okay so real talk bestie: if the lines at the end of a file get changed AND other lines get added, the diff output might show that whole situation as a delete-and-add, or as a change, or as a change-and-add. Nobody's expecting diff to be a mind reader here ๐Ÿ’€ โ€” it is not expected to know which one actually happened, and users should not care about the difference in output as long as it clearly shows the differences between the files. That's the vibe. It shows the diffs, you move on, periodt. ๐Ÿ’…


โœจ EXAMPLES โœจ

Let's get visual, bestie. Picture this: dir1 is a directory containing a directory named x, dir2 is a directory containing a directory named x, both dir1/x and dir2/x contain files named date.out, and dir2/x also contains a file named y.

So you run:

diff -r dir1 dir2

and it could produce output similar to:

Common subdirectories: dir1/x and dir2/x
Only in dir2/x: y
diff -r dir1/x/date.out dir2/x/date.out
1c1
< Mon Jul  2 13:12:16 PDT 1990
---
> Tue Jun 19 21:41:39 PDT 1990

look at that. immaculate. the whole tree compared, one character of nothing wasted. ๐ŸŒˆ


๐Ÿง  RATIONALE

Alright, storytime about why the standard looks like it does ๐Ÿ“–โœจ

The -h option was omitted because it was insufficiently specified and does not add to applications portability. bye bestie, not sorry. ๐Ÿ™ˆ

Historical implementations employ algorithms that do not always produce a minimum list of differences; the current language about making every effort is the best this volume of POSIX.1โ€2017 can do, as there is no metric that could be employed to judge the quality of implementations against any and all file contents. The statement "This list should be minimal" clearly implies that implementations are not expected to provide the following output when comparing two 100-line files that differ in only one character on a single line:

1,100c1,100
all 100 lines from file1 preceded with "< "
---
all 100 lines from file2 preceded with "> "

like no cap, that would be so extra. ๐Ÿซ 

The "Only in" messages required when the -r option is specified are not used by most historical implementations if the -e option is also specified. It is required here because it provides useful information that must be provided to update a target directory hierarchy to match a source hierarchy. The "Common subdirectories" messages are written by System V and 4.3 BSD when the -r option is specified. They are allowed here but are not required because they are reporting on something that is the same, not reporting a difference, and are not needed to update a target hierarchy. ๐Ÿ’…

The -c option, which writes output in a format using lines of context, has been included. The format is useful for a variety of reasons, among them being much improved readability and the ability to understand difference changes when the target file has line numbers that differ from another similar, but slightly different, copy. The patch utility is most valuable when working with difference listings using a context format. The BSD version of -c takes an optional argument specifying the amount of context. Rather than overloading -c and breaking the Utility Syntax Guidelines for diff, the standard developers decided to add a separate option for specifying a context diff with a specified amount of context (-C). Also, the format for context diffs was extended slightly in 4.3 BSD to allow multiple changes that are within context lines from each other to be merged together. The output format contains an additional four <asterisk> characters after the range of affected lines in the first filename. This was to provide a flag for old programs (like old versions of patch) that only understand the old context format. The version of context described here does not require that multiple changes within context lines be merged, but it does not prohibit it either. The extension is upwards-compatible, so any vendors that wish to retain the old version of diff can do so by adding the extra four <asterisk> characters (that is, utilities that currently use diff and understand the new merged format will also understand the old unmerged format, but not vice versa). ๐ŸŽ€

The -u and -U options of GNU diff have been included. Their output format, designed by Wayne Davison, takes up less space than -c and -C format, and in many cases is easier to read. The format's timestamps do not vary by locale, so LC_TIME does not affect it. The format's line numbers are rendered with the %1d format, not %d, because the file format notation rules would allow extra <blank> characters to appear around the numbers. โœจ

The substitute command was added as an additional format for the -e option. This was added to provide implementations with a way to fix the classic "dot alone on a line" bug present in many versions of diff. Since many implementations have fixed this bug, the standard developers decided not to standardize broken behavior, but rather to provide the necessary tool for fixing the bug. One way to fix this bug is to output two periods whenever a lone period is needed, then terminate the append command with a period, and then use the substitute command to convert the two periods into one period. ๐Ÿ”ฅ

The BSD-derived -r option was added to provide a mechanism for using diff to compare two file system trees. This behavior is useful, is standard practice on all BSD-derived systems, and is not easily reproducible with the find utility.

The requirement that diff not compare files in some circumstances, even though they have the same name, is based on the actual output of historical implementations. The specified behavior precludes the problems arising from running into FIFOs and other files that would cause diff to hang waiting for input with no indication to the user that diff was hung. An earlier version of this standard specified the output format more precisely, but in practice this requirement was widely ignored and the benefit of standardization seemed small, so it is now unspecified. In most common usage, diff -r should indicate differences in the file hierarchies, not the difference of contents of devices pointed to by the hierarchies.

Many early implementations of diff require seekable files. Since the System Interfaces volume of POSIX.1โ€2017 supports named pipes, the standard developers decided that such a restriction was unreasonable. Note also that the allowed filename - almost always refers to a pipe. ๐Ÿฆ‹

No directory search order is specified for diff. The historical ordering is, in fact, not optimal, in that it prints out all of the differences at the current level, including the statements about all common subdirectories before recursing into those subdirectories.

The message:

"diff %s %s %s\n", <diff_options>, <filename1>, <filename2>

does not vary by locale because it is the representation of a command, not an English sentence. ๐Ÿ’ฏ


๐Ÿ”ฎ FUTURE DIRECTIONS

None.

bestie the future is empty. nothing coming. no notes. ๐Ÿ‘‘

๐Ÿ‘€ SEE ALSO

  • cmp, comm, ed, find
  • The Base Definitions volume of POSIX.1โ€2017, Chapter 8, Environment Variables, Section 12.2, Utility Syntax Guidelines

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's a wrap on DIFF(1POSIX) bestie ๐Ÿ’– this one's from IEEE/The Open Group, dated 2017 โ€” go forth and compare your files, you absolute icon ๐ŸŽ€โœจ

๐Ÿ“ฆ where this page comes from