CMP(1P)
okay bestie, come sit with me π this is the POSIX Programmer's Manual page for cmp, section 1P, and I'm gonna walk you through every single line of it, no skips, no shortcuts, no cap.
β¨ PROLOG β¨
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for the details of how Linux actually behaves, fr), or the interface may not be implemented on Linux at all. π
π¦ NAME π¦
cmp β compare two files. That's it. That's the whole vibe check. π
πΈ SYNOPSIS πΈ
cmp [-l|-s] file1 file2
β DESCRIPTION β
The cmp utility shall compare two files. Periodt.
If the files are the same, cmp writes no output. Not a peep. π€«
Under default options, if they differ, it shall write to standard output the byte and line number at which the first difference occurred.
Bytes and lines shall be numbered beginning with 1 (not 0, bestie β we're not doing that here).
π OPTIONS π
cmp shall conform to the Base Definitions volume of POSIX.1β2017, Section 12.2, Utility Syntax Guidelines.
The following options shall be supported:
-lβ (Lowercase ell, don't get it twisted π) Write the byte number (decimal) and the differing bytes (octal) for each difference.-sβ Write nothing to standard output or standard error when the files differ; indicate differing files through exit status only. It is unspecified whether a diagnostic message is written to standard error when an error is encountered; if a message is not written, the error is indicated through exit status only.
π OPERANDS π
The following operands shall be supported:
file1β A pathname of the first file to be compared. Iffile1is'-', the standard input shall be used.file2β A pathname of the second file to be compared. Iffile2is'-', the standard input shall be used.
π¨ If bothfile1andfile2refer to standard input, or refer to the same FIFO special, block special, or character special file, the results are undefined. Like, undefined undefined. Don't do it. π
π«Ά STDIN π«Ά
The standard input shall be used only if the file1 or file2 operand refers to standard input. See the INPUT FILES section below, babe. β¬οΈ
π INPUT FILES π
The input files can be any file type. Any. Go wild. β¨
π ENVIRONMENT VARIABLES π
The following environment variables shall affect the execution of cmp:
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).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.NLSPATHβ Determine the location of message catalogs for the processing ofLC_MESSAGES.
π§ ASYNCHRONOUS EVENTS π§
Default. Nothing fancy here, moving on. π
π₯ STDOUT π₯
In the POSIX locale, results of the comparison shall be written to standard output.
When no options are used, the format shall be:
"%s %s differ: char %d, line %d\n", file1, file2,
<byte number>, <line number>
When the -l option is used, the format shall be:
"%d %o %o\n", <byte number>, <differing byte>,
<differing byte>
...for each byte that differs. The first <differing byte> number is from file1 while the second is from file2.
In both cases, <byte number> shall be relative to the beginning of the file, beginning with 1.
π No output shall be written to standard output when the -s option is used. Silence is golden, bestie.
π STDERR π
The standard error shall be used only for diagnostic messages.
If the -l option is used and file1 and file2 differ in length, or if the -s option is not used and file1 and file2 are identical for the entire length of the shorter file, in the POSIX locale the following diagnostic message shall be written:
"cmp: EOF on %s%s\n", <name of shorter file>, <additional info>
The <additional info> field shall either be null or a string that starts with a <blank> and contains no <newline> characters. Some implementations report on the number of lines in this case. π
If the -s option is used and an error occurs, it is unspecified whether a diagnostic message is written to standard error.
π OUTPUT FILES π
None. We produce no files. We're just here to compare. π«Ά
β¨ EXTENDED DESCRIPTION β¨
None. Nothing extra, she's a simple girl. π
π EXIT STATUS π
The following exit values shall be returned:
0β The files are identical.1β The files are different; this includes the case where one file is identical to the first part of the other.>1β An error occurred.
π CONSEQUENCES OF ERRORS π
Default.
The following sections are informative.
πΈ APPLICATION USAGE πΈ
Okay so even though input files to cmp can be any type, the results might not be what you'd expect on character special device files, or on file types not described by the System Interfaces volume of POSIX.1β2017. Since this volume of POSIX.1β2017 does not specify the block size used when doing input, comparisons of character special files need not compare all of the data in those files. π¬
For files which are not text files, line numbers simply reflect the presence of a <newline>, without any implication that the file is organized into lines.
Since the behavior of -s differs between implementations as to whether error messages are written, the only way to ensure consistent behavior of cmp when -s is used is to redirect standard error to /dev/null. That's the move. π―
If error messages are wanted, instead of using -s you should redirect standard output to /dev/null, and anything written to standard error should be discarded if the exit status is 1. For example:
silent_cmp() {
# compare files with no output except error messages
message=$(cmp "$@" 2>&1 >/dev/null)
status=$?
case $status in
(0|1) ;;
(*) printf '%s\n' "$message" ;;
esac
return $status
}
π EXAMPLES π
None. π
π RATIONALE π
The global language in Section 1.4, Utility Description Defaults indicates that using two mutually-exclusive options together produces unspecified results. Some System V implementations consider the option usage:
cmp -l -s ...
to be an error. They also treat:
cmp -s -l ...
as if no options were specified. Both of these behaviors are considered bugs, but are allowed. π
The word char in the standard output format comes from historical usage, even though it is actually a byte number. When cmp is supported in other locales, implementations are encouraged to use the word byte or its equivalent in another language. Users should not interpret this difference to indicate that the functionality of the utility changed between locales. Same girl, different word. π¦
Some implementations report on the number of lines in the identical-but-shorter file case. This is allowed by the inclusion of the <additional info> fields in the output format. The restriction on having a leading <blank> and no <newline> characters is to make parsing for the filename easier. It is recognized that some filenames containing white-space characters make parsing difficult anyway, but the restriction does aid programs used on systems where the names are predominantly well behaved.
β FUTURE DIRECTIONS β
Future versions of this standard may require that diagnostic messages are written to standard error when the -s option is specified. So like, watch this space. π
π SEE ALSO π
commdiff- The Base Definitions volume of POSIX.1β2017, Chapter 8, Environment Variables, Section 12.2, Utility Syntax Guidelines
π«Ά COPYRIGHT π«Ά
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 the whole page, bestie, we ate and left no crumbs π
β this was brought to you by IEEE/The Open Group, 2017, page CMP(1P). mwah πβ¨
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms