Cmp (Unix)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

Template:Short description Template:Lowercase title Template:Infobox software cmp is a shell command that compares two files of any type and reports differences. By default, it outputs nothing if the files match. If they differ, it reports the byte and line number of the first difference. The exit code can be used programmatically since it is 0 if the files match, 1 if the files differ or 2 if comparison fails (i.e. inaccessible or missing argument).

The command is available on Unix-like systems, OS-9, IBM i and Windows (via UnxUtils).<ref>Template:Cite book</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> The command first appeared in Version 1 Unix.<ref>Template:Man</ref> It is part of the X/Open Portability Guide since issue 2 of 1987. It was inherited into the first version of POSIX.1 and the Single Unix Specification.<ref>Template:Man</ref> The implementation in GNU coreutils was written by Torbjorn Granlund and David MacKenzie.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

Use

Command line options include:

Option Description Unix Plan 9 Inferno FreeBSD Linux Template:Nowrap
Template:Mono Print the differing bytes; display control bytes as a 'Template:Mono' followed by a letter of the alphabet and precede bytes that have the high bit set with 'Template:Mono' (which stands for "meta") Template:No Template:No Template:No Template:No Template:Yes Template:No
Template:Mono Do not follow symbolic links Template:No Template:No Template:No Template:Yes Template:No Template:No
Template:Mono Skip the first SKIP bytes of input Template:No Template:No Template:No Template:No Template:Yes Template:No
Template:Mono Skip the first SKIP1 bytes of FILE1 and the first SKIP2 bytes of FILE2 Template:No Template:No Template:No Template:No Template:Yes Template:No
Template:Mono Print the (decimal) byte numbers and (octal) values of all differing bytes, instead of the default info; also, print the EOF message if one file is shorter than the other Template:Yes Template:Yes Template:Yes Template:Yes Template:Yes Template:Yes
Template:Mono Print the line number of the first differing byte Template:Yes Template:Yes Template:Yes Template:No Template:No Template:No
Template:Mono Compare at most LIMIT bytes Template:No Template:No Template:No Template:No Template:Yes Template:No
Template:Mono Print nothing; yield exit status only Template:Yes Template:Yes Template:Yes Template:Yes Template:Yes Template:Yes
Template:Mono Text mode where the files are opened in text mode and translated to the CCSID of the job before comparing byte for byte Template:No Template:No Template:No Template:No Template:No Template:Yes
Template:Mono Print version info Template:No Template:No Template:No Template:No Template:Yes Template:No
Template:Mono Like -l but prints in hexadecimal and using zero as index for the first byte in the files Template:No Template:No Template:No Template:Yes Template:No Template:No
Template:Mono For regular files, compare file sizes first, and fail if they are not equal Template:No Template:No Template:No Template:Yes Template:No Template:No
Template:Mono Print command-line help info Template:No Template:No Template:No Template:No Template:Yes Template:No

Operands that are byte counts are decimal by default, but may be preceded by 'Template:Mono' for octal and 'Template:Mono' for hexadecimal.

A byte count can be followed by a suffix to specify a multiple of that count; in this case an omitted integer is understood to be 1. A bare size letter, or one followed by 'Template:Mono', specifies a multiple using powers of 1024. A size letter followed by 'Template:Mono' specifies powers of 1000 instead. For example, 'Template:Mono' and 'Template:Mono' are equivalent to 'Template:Mono', whereas 'Template:Mono' is equivalent to 'Template:Mono'. This notation is upward compatible with the SI prefixes<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> for decimal multiples and with the IEC 60027-2 prefixes for binary multiples.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

Example

File:Cmp-example-command-gimp.gif
Example use to report different bytes between text files

See also

References

Template:Reflist

Template:Sister project

Template:Unix commands Template:Plan 9 commands