Cmp (Unix)

File comparison command

cmp

Original author(s)| Dennis Ritchie
(AT&T Bell Laboratories)
Developer(s)| Various open-source and commercial developers
Initial release| November 3, 1971; 53 years ago (1971-11-03)
Written in| Plan 9: C
Operating system| Unix, Unix-like, Plan 9, Inferno, OS-9, IBM i
Type| Command
License| coreutils: GPLv3+
Plan 9: MIT License

`cmp` is a shell command that compares two files of any type and reports differences. By default, it is silent (outputs nothing) if the files are the same. If they differ, the byte and line number at which the first difference occurs is reported. The command is available on Unix-like systems, OS-9, IBM i and Windows (via UnxUtils).[1][2][3]

The command first appeared in Version 1 Unix.[4] 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.[5] The implementation in GNU coreutils was written by Torbjorn Granlund and David MacKenzie.[6]

## Use

[edit]

The exit codes indicates one of the following conditions:

* 0 – Files are identical
* 1 – Files differ
* 2 – Inaccessible or missing argument

Options include:

Option  | Description  | Unix | Plan 9 | Inferno | FreeBSD | Linux | IBM i

|
|
|
|
|
|
|

-b,
\--print-bytes | Print the differing bytes; display control bytes as a '^' followed by a letter of the alphabet and precede bytes that have the high bit set with 'M-' (which stands for "meta")  | No  | No  | No  | No  | Yes  | No
-h | Do not follow symbolic links  | No  | No  | No  | Yes  | No  | No
-i SKIP,
\--ignore-initial=SKIP | Skip the first SKIP bytes of input  | No  | No  | No  | No  | Yes  | No
-i SKIP1:SKIP2,
\--ignore-
initial=
SKIP1:SKIP2 | Skip the first SKIP1 bytes of FILE1 and the first SKIP2 bytes of FILE2  | No  | No  | No  | No  | Yes  | No
-l,
\--verbose | 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  | Yes  | Yes  | Yes  | Yes  | Yes  | Yes
-L | Print the line number of the first differing byte  | Yes  | Yes  | Yes  | No  | No  | No
-n LIMIT,
\--bytes=LIMIT | Compare at most LIMIT bytes  | No  | No  | No  | No  | Yes  | No
-s,
\--quiet,
\--silent | Print nothing; yield exit status only  | Yes  | Yes  | Yes  | Yes  | Yes  | Yes
-t | Text mode where the files are opened in text mode and translated to the CCSID of the job before comparing byte for byte  | No  | No  | No  | No  | No  | Yes
-v,
\--version | Print version info  | No  | No  | No  | No  | Yes  | No
-x | Like -l but prints in hexadecimal and using zero as index for the first byte in the files  | No  | No  | No  | Yes  | No  | No
-z | For regular files, compare file sizes first, and fail if they are not equal  | No  | No  | No  | Yes  | No  | No
\--help | Print command-line help info  | No  | No  | No  | No  | Yes  | No

Operands that are byte counts are decimal by default, but may be preceded by '0' for octal and '0x' 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 'iB', specifies a multiple using powers of 1024. A size letter followed by 'B' specifies powers of 1000 instead. For example, '-n 4M' and '-n 4MiB' are equivalent to '-n 4194304', whereas '-n 4MB' is equivalent to '-n 4000000'. This notation is upward compatible with the SI prefixes[7] for decimal multiples and with the IEC 60027-2 prefixes for binary multiples.[8]

## Example

[edit]

Example use to report different bytes between text files

## See also

[edit]

* Comparison of file comparison tools
* List of POSIX commands

## References

[edit]

1. ^ Paul S. Dayan (1992). The OS-9 Guru - 1 : The Facts. Galactic Industrial Limited. ISBN 0-9519228-0-7.
2. ^ IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM. Retrieved 2020-09-05.
3. ^ "Native Win32 ports of some GNU utilities". unxutils.sourceforge.net.
4. ^ `cmp(1)` – FreeBSD General Commands Manual
5. ^ `printf` – Shell and Utilities Reference, The Single UNIX Specification, Version 5 from The Open Group
6. ^ "cmp(1): compare two files byte by byte - Linux man page". linux.die.net.
7. ^ "Welcome - BIPM".
8. ^ "Definitions of the SI units: The binary prefixes". physics.nist.gov. Retrieved 21 April 2018.

## External links

[edit]

The Wikibook Guide to Unix has a page on the topic of: Commands

* `cmp`: compare two files – Shell and Utilities Reference, The Single UNIX Specification, Version 5 from The Open Group
* Comparing and Merging Files: Invoking cmp The section of the manual of GNU cmp in the diffutils free manual.

* v
* t
* e

Unix command-line interface programs and shell builtins

File system|

* cat
* chattr
* chmod
* chown
* chgrp
* cksum
* cmp
* cp
* dd
* du
* df
* file
* fuser
* ln
* ls
* mkdir
* mv
* pax
* pwd
* rm
* rmdir
* split
* tee
* touch
* type
* umask

Processes|

* at
* bg
* crontab
* fg
* kill
* nice
* ps
* time

User environment|

* env
* exit
* logname
* mesg
* talk
* tput
* uname
* who
* write

Text processing|

* awk
* basename
* comm
* csplit
* cut
* diff
* dirname
* ed
* ex
* fold
* head
* iconv
* join
* m4
* more
* nl
* paste
* patch
* printf
* read
* sed
* sort
* strings
* tail
* tr
* troff
* uniq
* vi
* wc
* xargs

Shell builtins|

* alias
* cd
* echo
* test
* unset
* wait

Searching|

* find
* grep

Documentation|

* man

Software development|

* ar
* ctags
* lex
* make
* nm
* strip
* yacc

Miscellaneous|

* bc
* cal
* expr
* lp
* od
* sleep
* true and false

* Categories
* Standard Unix programs
* Unix SUS2008 utilities
* List

* v
* t
* e

Plan 9 command-line interface programs and shell builtins

File system|

* chmod
* chgrp
* cmp
* cp
* dd
* du
* file
* gzip
* ls
* mkdir
* pwd
* rm
* split
* tee
* touch

Processes|

* kill
* ps

User environment|

* passwd
* who

Text processing|

* awk
* basename
* comm
* diff
* ed
* eqn
* join
* sed
* sort
* spell
* strings
* tail
* tr
* troff
* uniq
* wc

Shell builtins|

* echo
* test

Networking|

* ip/ipconfig
* ip/ping
* netstat

Searching|

* grep

Software development|

* ar
* hoc
* lex
* nm
* strip
* yacc

Miscellaneous|

* bc
* cal
* fortune
* sleep

*  Category

*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template