Cat (Unix)
Encyclopedia
The cat command is a standard Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 program used to concatenate
Concatenation
In computer programming, string concatenation is the operation of joining two character strings end-to-end. For example, the strings "snow" and "ball" may be concatenated to give "snowball"...

 and display files. The name is from catenate, a synonym of concatenate.

Specification

The Single Unix Specification
Single UNIX Specification
The Single UNIX Specification is the collective name of a family of standards for computer operating systems to qualify for the name "Unix"...

 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
OpenBSD
OpenBSD is a Unix-like computer operating system descended from Berkeley Software Distribution , a Unix derivative developed at the University of California, Berkeley. It was forked from NetBSD by project leader Theo de Raadt in late 1995...

 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 $

Jargon File definition

The Jargon File
Jargon File
The Jargon File is a glossary of computer programmer slang. The original Jargon File was a collection of terms from technical cultures such as the MIT AI Lab, the Stanford AI Lab and others of the old ARPANET AI/LISP/PDP-10 communities, including Bolt, Beranek and Newman, Carnegie Mellon...

 version 4.4.7 lists this as the definition of cat:

Useless use of cat

UUOC (from comp.unix.shell on Usenet
Usenet
Usenet is a worldwide distributed Internet discussion system. It developed from the general purpose UUCP architecture of the same name.Duke University graduate students Tom Truscott and Jim Ellis conceived the idea in 1979 and it was established in 1980...

) 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




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 redirection symbol ">" instead of "<" (often adjacent on keyboards) may permanently delete 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
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 programmer Randal L. Schwartz
Randal L. Schwartz
Randal L. Schwartz , also known as merlyn, is an American author, system administrator and programming consultant.-Career:...

. There is a web page devoted to this and other similar awards. In British
British English
British English, or English , is the broad term used to distinguish the forms of the English language used in the United Kingdom from forms used elsewhere...

 hackerdom
Hacker culture
A hacker is a member of the computer programmer subculture originated in the 1960s in the United States academia, in particular around the Massachusetts Institute of Technology 's Tech Model Railroad Club and MIT Artificial Intelligence Laboratory...

 the activity of fixing instances of UUOC is sometimes called demoggification.

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 format
Container format
A container or wrapper format is a meta-file format whose specification describes how different data elements and metadata coexist in a computer file....

s 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
DV
DV is a format for the digital recording and playing back of digital video. The DV codec was launched in 1995 with joint efforts of leading producers of video camcorders....

 (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
FFmpeg
FFmpeg is a free software project that produces libraries and programs for handling multimedia data. The most notable parts of FFmpeg are libavcodec, an audio/video codec library used by several other projects, libavformat, an audio/video container mux and demux library, and the ffmpeg command line...

.

Other operating systems

The equivalent command in the VMS
OpenVMS
OpenVMS , previously known as VAX-11/VMS, VAX/VMS or VMS, is a computer server operating system that runs on VAX, Alpha and Itanium-based families of computers. Contrary to what its name suggests, OpenVMS is not open source software; however, the source listings are available for purchase...

, CP/M
CP/M
CP/M was a mass-market operating system created for Intel 8080/85 based microcomputers by Gary Kildall of Digital Research, Inc...

, DOS
DOS
DOS, short for "Disk Operating System", is an acronym for several closely related operating systems that dominated the IBM PC compatible market between 1981 and 1995, or until about 2000 if one includes the partially DOS-based Microsoft Windows versions 95, 98, and Millennium Edition.Related...

, OS/2
OS/2
OS/2 is a computer operating system, initially created by Microsoft and IBM, then later developed by IBM exclusively. The name stands for "Operating System/2," because it was introduced as part of the same generation change release as IBM's "Personal System/2 " line of second-generation personal...

, and Microsoft Windows
Microsoft Windows
Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...

 operating system command shells is type
Type (command)
In computing, type is a command in various VMS. AmigaDOS, CP/M, DOS, OS/2 and Microsoft Windows command line interpreters such as COMMAND.COM, cmd.exe, 4DOS/4NT and Windows PowerShell. It is used to display the contents of specified files...

.

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
    Split (Unix)
    split is a Unix utility most commonly used to split a file into two or more smaller files.-Usage:The command-syntax is: split [OPTION] [INPUT [PREFIX]]...

    , a command that splits a file into pieces which cat can then rejoin.
  • zcat
  • paste (Unix)
    Paste (Unix)
    paste is a Unix command line utility which is used to join files horizontally by outputting lines consisting of the sequentially corresponding lines of each file specified, separated by tabs, to the standard output...


External links


Manual pages

  • cat(1) original manual page in the First Edition of Unix
    Unix
    Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

    .
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK