Jump to content

cat (Unix)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 193.24.32.53 (talk) at 09:25, 12 August 2011 (→‎Other operating systems). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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

References

  1. ^ The default behavior for redirection is to clobber the file to its immediate right.
  2. ^ More accurately stated ">" will truncate the file.
  3. ^ http://partmaps.org/era/unix/award.html
  4. ^ moggy is chiefly British word for "(mongrel) cat", hence demoggification literally means "removal of (non-special) cats".
  5. ^ FFmpeg FAQ: 3.15 How can I join video files?

Manual pages

Other