The cat command is a standard Unix program used to concatenate and display files. The name is from catenate, a synonym of concatenate.
Contents |
The Single Unix Specification specifies the behavior that the contents of each of the files given in sequence as arguments will be written to the standard output in the same sequence, and mandates one option, -u, where each byte is printed as it is read.
If the filename is specified as -, then cat will read from standard input at that point in the sequence. If no files are specified, cat will read from standard input entered.
Both the BSD versions of cat (as per the OpenBSD manpage) and the GNU coreutils version of cat specify the following options:
The Jargon File version 4.4.7 lists this as the definition of cat:
Among Unix fans, cat(1) is considered an excellent example of user-interface design, because it delivers the file contents without such verbosity as spacing or headers between the files, and because it does not require the files to consist of lines of text, but works with any sort of data. Among Unix critics, cat(1) is considered the canonical example of bad user-interface design, because of its woefully unobvious name. It is far more often used to blast a single file to standard output than to concatenate two or more files. The name cat for the former operation is just as unintuitive as, say, LISP's cdr. |
UUOC (from comp.unix.shell on Usenet) stands for "Useless Use of cat". As received wisdom on comp.unix.shell observes "The purpose of cat is to concatenate (or 'catenate') files. If it's only one file, concatenating it with nothing at all is a waste of time, and costs you a process." This is also referred to as "Cat abuse". Nevertheless one sees people doing
cat filename | cmd arg1 arg2 argn
instead of the (not quite) equivalent and cheaper
<filename cmd arg1 arg2 argn
or the classical (not quite) equivalent
cmd arg1 arg2 argn < filename
It is not quite equivalent as the last two examples allows cmd to seek in the file, whereas the first does not.
Without two named files cat is a useless use of cat, with the exception of viewing the contents of a single file on the command line. A UUOC campaign will eliminate the inefficiency from shell scripts by using redirection instead. However, cat with one named file is safer where human error is a concern—one wrong use of the default[1] redirection symbol ">" instead of "<" (often adjacent on keyboards) may permanently delete[2] the file you were just needing to read. Also cat will prevent the command from seeking in the file.
Since 1995, occasional awards for UUOC have been given out, usually by Perl programmer Randal L. Schwartz. There is a web page devoted to this and other similar awards.[3] In British hackerdom the activity of fixing instances of UUOC is sometimes called demoggification.[4]
Because cat simply joins files (without adding anything in between), it can be used equally well on binary files as on text files. For many binary formats, the result cannot be parsed (for example, if a file has a unique header or footer) and this use of cat is not especially useful, but for some multimedia container formats the resulting file is valid, and this provides a very simple way of concatenating files, particularly video streams. Significantly, the MPEG program stream (MPEG-1 and MPEG-2) and DV (Digital Video) formats can be so concatenated – such a stream is fundamentally a stream of packets, which can thus be joined.
Further, any other video format can be concatenated by transcoding to one of these privileged formats, concatenating via cat, and then transcoding back. This procedure is in fact endorsed by FFmpeg.[5]
The equivalent command in the VMS, CP/M, DOS, OS/2, and Microsoft Windows operating system command shells is type.
Note: To combine multiple files together in DOS/Windows you can use the "copy /b" command:
Example:
copy /b file1.txt + file2.txt file3.txt
This copies file1.txt and file2.txt in binary mode together in one file, file3.txt.
split
, a command that splits a file into pieces which cat can then rejoin.
|