Cmp (Unix)

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

In computing, cmp is a command-line utility on Unix and Unix-like operating systems that compares two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same; if they differ, the byte and line number at which the first difference occurred is reported. The command is also available in the OS-9 shell. [1]

Contents

History

cmp 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. [2] It first appeared in Version 1 Unix. [3] The version of cmp bundled in GNU coreutils was written by Torbjorn Granlund and David MacKenzie. [4] The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities. [5] The cmp command has also been ported to the IBM i operating system. [6]

Switches

cmp may be qualified by the use of command-line switches. The switches supported by notable implementations of cmp are:

NameDescription 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").NoNoNoNoYesNo
-hDo not follow symbolic links.NoNoNoYesNoNo
-i SKIP,
--ignore-initial=SKIP
Skip the first SKIP bytes of input.NoNoNoNoYesNo
-i SKIP1:SKIP2,
--ignore-initial=SKIP1:SKIP2
Skip the first SKIP1 bytes of FILE1 and the first SKIP2 bytes of FILE2.NoNoNoNoYesNo
-l,
--verbose
Output the (decimal) byte numbers and (octal) values of all differing bytes, instead of the default standard output.

Also, output the EOF message if one file is shorter than the other.

YesYesYesYesYesYes
-LPrint the line number of the first differing byte.YesYesYesNoNoNo
-n LIMIT,
--bytes=LIMIT
Compare at most LIMIT bytes.NoNoNoNoYesNo
-s,
--quiet,
--silent
Output nothing; yield exit status only.YesYesYesYesYesYes
-tText mode where the files are opened in text mode and translated to the CCSID of the job before comparing byte for byte.NoNoNoNoNoYes
-v,
--version
Output version info.NoNoNoNoYesNo
-xLike -l but prints in hexadecimal and using zero as index for the first byte in the files.NoNoNoYesNoNo
-zFor regular files compare file sizes first, and fail the comparison if they are not equal.NoNoNoYesNoNo
--helpOutputs a help file.NoNoNoNoYesNo

Operands that are byte counts are normally decimal, 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]

Examples

Comparing

:020000040007F3 :10E00000840D80E501000000010000000100000017 :10E01000000000000000000070DE010062A726542E  with  :020000040007F3 :10E44400840D80E501000000010000000100000017 :10E01000000000000000000070DE010062A726542E  > cmp  0 00  0 00 differ: char 21, line 2  > cmp -l 0 00 # note: byte location is in decimal, contents is in octal  21  64  60  22  64  60  23  64  60 

Return values

See also

Related Research Articles

In computing, tar is a computer software utility for collecting many files into one archive file, often referred to as a tarball, for distribution or backup purposes. The name is derived from "tape archive", as it was originally developed to write data to sequential I/O devices with no file system of their own. The archive data sets created by tar contain various file system parameters, such as name, timestamps, ownership, file-access permissions, and directory organization. POSIX abandoned tar in favor of pax, yet tar sees continued widespread use.

ln (Unix) Unix file management utility

The ln command is a standard Unix command utility used to create a hard link or a symbolic link (symlink) to an existing file or directory. The use of a hard link allows multiple filenames to be associated with the same file since a hard link points to the inode of a given file, the data of which is stored on disk. On the other hand, symbolic links are special files that refer to other files by name.

In computing, touch is a command used to update the access date and/or modification date of a computer file or directory. It is included in Unix and Unix-like operating systems, TSC's FLEX, Digital Research/Novell DR DOS, the AROS shell, the Microware OS-9 shell, and ReactOS. The command is also available for FreeDOS and Microsoft Windows.

The archiver, also known simply as ar, is a Unix utility that maintains groups of files as a single archive file. Today, ar is generally used only to create and update static library files that the link editor or linker uses and for generating .deb packages for the Debian family; it can be used to create archives for any purpose, but has been largely replaced by tar for purposes other than static libraries. An implementation of ar is included as one of the GNU Binutils.

xargs is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command.

dd is a command-line utility for Unix, Plan 9, Inferno, and Unix-like operating systems and beyond, the primary purpose of which is to convert and copy files. On Unix, device drivers for hardware and special device files appear in the file system just like normal files; dd can also read and/or write from/to these files, provided that function is implemented in their respective driver. As a result, dd can be used for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data. The dd program can also perform conversions on the data as it is copied, including byte order swapping and conversion to and from the ASCII and EBCDIC text encodings.

tr (Unix) Unix text formatting utility

tr is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. It is an abbreviation of translate or transliterate, indicating its operation of replacing or removing specific characters in its input data set.

wc (Unix) Unix command utility

wc is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. The program reads either standard input or a list of computer files and generates one or more of the following statistics: newline count, word count, and byte count. If a list of files is provided, both individual file and total statistics follow.

In computing, cut is a command line utility on Unix and Unix-like operating systems which is used to extract sections from each line of input — usually from a file. It is currently part of the GNU coreutils package and the BSD Base System.

df (Unix) Standard Unix command

df is a standard Unix command used to display the amount of available disk space for file systems on which the invoking user has appropriate read access. df is typically implemented using the statfs or statvfs system calls.

du (Unix) Standard Unix program

du is a standard Unix program used to estimate file space usage—space used under a particular directory or files on a file system. A Windows commandline version of this program is part of Sysinternals suite by Mark Russinovich.

mv is a Unix command that moves one or more files or directories from one place to another. If both filenames are on the same filesystem, this results in a simple file rename; otherwise the file content is copied to the new location and the old file is removed. Using mv requires the user to have write permission for the directories the file will move between. This is because mv changes the content of both directories involved in the move. When using the mv command on files located on the same filesystem, the file's timestamp is not updated.

file (command) Standard Unix program

The file command is a standard program of Unix and Unix-like operating systems for recognizing the type of data contained in a computer file.

rm (Unix) Unix command utility

rm is a basic command on Unix and Unix-like operating systems used to remove objects such as computer files, directories and symbolic links from file systems and also special files such as device nodes, pipes and sockets, similar to the del command in MS-DOS, OS/2, and Microsoft Windows. The command is also available in the EFI shell.

In computing, tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters. The command is named after the T-splitter used in plumbing.

test is a command-line utility found in Unix, Plan 9, and Unix-like operating systems that evaluates conditional expressions. test was turned into a shell builtin command in 1981 with UNIX System III and at the same time made available under the alternate name [.

In computing, sleep is a command in Unix, Unix-like and other operating systems that suspends program execution for a specified time.

od is a command on various operating systems for displaying ("dumping") data in various human-readable output formats. The name is an acronym for "octal dump" since it defaults to printing in the octal data format.

The csplit command in Unix and Unix-like operating systems is a utility that is used to split a file into two or more smaller files determined by context lines.

cat (Unix) Unix command utility

cat is a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to (con)catenate files. It has been ported to a number of operating systems.

References

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