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 (from Latin ''catenare'', "to chain"). It has been ported to a number of operating systems.
History
cat
was part of the early versions of pr
, a PDP-7 and cat
bundled in tac
and bat
also became available, bringing different new features.
Usage
The Single Unix Specification defines the operation ofcat
to read files in the sequence given in its arguments, writing their contents to the standard output in the same sequence. The specification mandates the support of one option flag, ''u'' for unbuffered output, meaning that each byte is written after it has been read. Some operating systems, like the ones using GNU Core Utilities, do this by default and ignore the flag.GNU Coreutilscat
reads from standard input at that point in the sequence. If no files are specified, cat
reads from standard input only.
The command- syntax is:
cat ptions ile_namesOptions
Example of somecat
options:
* (GNU: ), number non-blank output lines
* implies but also display end-of-line characters as (GNU only: the same, but without implying )
* (GNU: ), number all output lines
* (GNU: ), squeeze multiple adjacent blank lines
* implies , but also display tabs as (GNU: the same, but without implying )
* use unbuffered I/O for stdout. POSIX does not specify the behavior without this option.
* (GNU: ), displays nonprinting characters, except for tabs and the end of line character
Use cases
cat
can be used to pipe a file to a program that expects plain text or binary data on its input stream. cat
does not destroy non-text bytes when concatenating and outputting. As such, its two main use cases are text files and certain format-compatible types of binary files.
Concatenation of text is limited to text files using the same legacy encoding, such as cat
does not provide a way to concatenate Unicode text files that have a cat
provides an effective means of appending files. Video streams can be a significant example of files that cat
can concatenate without issue, e.g. the MPEG program stream (MPEG-1 and MPEG-2) and DV (Digital Video) formats, which are fundamentally simple streams of packets.
Examples
Unix culture
Jargon file definition
Thecat
:
Useless use of cat
''Useless use of cat'' (''UUOC'') is common Unix jargon for command line constructs that only provide a function of convenience to the user.comp.unix.shellcat
is sometimes called "cat abuse". Example of a common cat
abuse is given in the award:
cat filename , command arg1 arg2 argn
This can be rewritten using redirection of stdin instead, in either of the following forms (the first is more traditional):
command arg1 arg2 argn < filenameBeyond other benefits, the input redirection forms allow ''command'' to perform random access Random access (more precisely and more generally called direct access) is the ability to access an arbitrary element of a sequence in equal time or any datum from a population of addressable elements roughly as easily and efficiently as any othe ...on the file, whereas thecat
examples do not. This is because the redirection form opens the file as the stdin file descriptor which ''command'' can fully access, while thecat
form simply provides the data as a stream of bytes. Another common case wherecat
is unnecessary is where a command defaults to operating on stdin, but will read from a file, if the filename is given as an argument. This is the case for many common commands; the following examplescat file , grep pattern cat file , lesscan instead be written asgrep pattern file less fileA common interactive use ofcat
for a single file is to output the content of a file to standard output. However, if the output is piped or redirected,cat
is unnecessary. Acat
written with UUOC might still be preferred for readability reasons, as reading a piped stream left-to-right might be easier to conceptualize. Also, one wrong use of the redirection symbol ">" instead of "<" (often adjacent on keyboards) may permanently delete the content of a file, in other words clobbering, and one way to avoid this is to usecat
with pipes. Compare:command < in , command2 > outwith: cat in , command , command2 > outout
See also
*paste Paste is a term for any very thick viscous fluid. It may refer to: Science and technology * Adhesive or paste ** Wallpaper paste ** Wheatpaste, A liquid adhesive made from vegetable starch and water * Paste (rheology), a substance that behaves a ...*split Split(s) or The Split may refer to: Places * Split, Croatia, the largest coastal city in Croatia * Split Island, Canada, an island in the Hudson Bay * Split Island, Falkland Islands * Split Island, Fiji, better known as Hạfliua Arts, entertain ..., a command that splits a file into pieces which cat can then rejoin. *zcat gzip is a file format and a software application used for Data compression, file compression and decompression. The program was created by Jean-loup Gailly and Mark Adler as a free software replacement for the compress program used in early Uni ...* less
References
External links
*
UNIX Style, or cat -v Considered Harmful
- A paper by Rob Pike on proper Unix command design using cat as an example.
cat(1) original manual page
in the First Edition ofUnix Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a .... * * * * * {{Core Utilities commands Unix text processing utilitiesCat The cat (''Felis catus'') is a domestic species of small carnivorous mammal. It is the only domesticated species in the family Felidae and is commonly referred to as the domestic cat or house cat to distinguish it from the wild members of ...Unix SUS2008 utilities IBM i Qshell commands