cat (Unix)
This article includes a list of references, related reading, or external links, but its sources remain unclear because it lacks inline citations. (February 2008) |
The cat command is a standard Unix program used to concatenate and display files. The name is from catenate, a synonym of concatenate.
Specification
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.
Extensions
Both the BSD versions of cat (as per the OpenBSD manpage) and the GNU coreutils version of cat specify the following options:
- -b (GNU only: --number-nonblank), number non-blank output lines
- -n (GNU only: --number), number all output lines
- -s (GNU only: --squeeze-blank), squeeze multiple adjacent blank lines
- -v (GNU only: --show-nonprinting), displays nonprinting characters as if they were visible, except for tabs and the end of line character
- -t on BSD, -T on GNU, implies -v but also display tabs as ^I
- -e on BSD, -E on GNU, implies -v but also display end-of-line characters as $
Unix culture
Jargon File definition
The Jargon File version 4.4.7 lists this as the definition of cat: Template:Quotation1
Useless use of cat
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]
Binary use
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]
Other operating systems
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
See also
- List of Unix utilities
split
, a command that splits a file into pieces which cat can then rejoin.- zcat
- paste (Unix)
References
- ^ The default behavior for redirection is to clobber the file to its immediate right.
- ^ More accurately stated ">" will truncate the file.
- ^ http://partmaps.org/era/unix/award.html
- ^ moggy is chiefly British word for "(mongrel) cat", hence demoggification literally means "removal of (non-special) cats".
- ^ FFmpeg FAQ: 3.15 How can I join video files?
External links
- The Single UNIX Specification, Version 4 from The Open Group : concatenate and print files – Shell and Utilities Reference,
- UNIX Style, or cat -v Considered Harmful - A paper by Rob Pike on proper Unix command design using cat as an example.
Manual pages
- cat(1) original manual page in the First Edition of Unix.
- GNU Coreutils reference : concatenate and write files –
- OpenBSD General Commands Manual : concatenate and print files –
- FreeBSD General Commands Manual –
- : catenate files – Plan 9 from Bell Labs manual