tac (Unix)
   HOME

TheInfoList



OR:

cat is a standard
Unix utility 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, and o ...
that reads files sequentially, writing them to
standard output In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin ...
. 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
Unix 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, an ...
, e.g.,
Version 1 Version 1 is an Irish company specializing in international management consulting, software asset management, software development, cloud computing, and outsourcing company. On July 13, 2022, Partners Group completed the acquisition of a m ...
, and replaced pr, a
PDP-7 The PDP-7 was a minicomputer produced by Digital Equipment Corporation as part of the PDP series. Introduced in 1964, shipped since 1965, it was the first to use their Flip-Chip technology. With a cost of , it was cheap but powerful by the s ...
and
Multics Multics ("Multiplexed Information and Computing Service") is an influential early time-sharing operating system based on the concept of a single-level memory.Dennis M. Ritchie, "The Evolution of the Unix Time-sharing System", Communications of ...
utility for copying a single file to the screen. It was written by
Ken Thompson Kenneth Lane Thompson (born February 4, 1943) is an American pioneer of computer science. Thompson worked at Bell Labs for most of his career where he designed and implemented the original Unix operating system. He also invented the B programmi ...
and Dennis Ritchie. The version of cat bundled in
GNU GNU () is an extensive collection of free software (383 packages as of January 2022), which can be used as an operating system or can be used in parts with other operating systems. The use of the completed GNU tools led to the family of operat ...
coreutils The GNU Core Utilities or coreutils is a package of GNU software containing implementations for many of the basic tools, such as cat, ls, and rm, which are used on Unix-like operating systems. In September 2002, the ''GNU coreutils'' were cr ...
was written by Torbjorn Granlund and
Richard Stallman Richard Matthew Stallman (; born March 16, 1953), also known by his initials, rms, is an American free software movement activist and programmer. He campaigns for software to be distributed in such a manner that its users have the freedom to ...
. The ReactOS version was written by David Welch, Semyon Novikov, and Hermès Bélusca. Over time, alternative utilities such as tac and bat also became available, bringing different new features.


Usage

The Single Unix Specification defines the operation of cat 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 Coreutils
"GNU Coreutils manual"
''
GNU GNU () is an extensive collection of free software (383 packages as of January 2022), which can be used as an operating system or can be used in parts with other operating systems. The use of the completed GNU tools led to the family of operat ...
'', Retrieved on 1 Mars 2017.
If one of the input filenames is specified as a single hyphen (''-''), then cat 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_names


Options

Example of some cat 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
ASCII ASCII ( ), abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Because ...
. cat does not provide a way to concatenate Unicode text files that have a
Byte Order Mark The byte order mark (BOM) is a particular usage of the special Unicode character, , whose appearance as a magic number at the start of a text stream can signal several things to a program reading the text: * The byte order, or endianness, of t ...
or files using different text encodings from each other. For many structured binary data sets, the resulting combined file may not be valid; for example, if a file has a unique header or footer, the result will spuriously duplicate these. However, for some multimedia
digital container format A container format (informally, sometimes called a wrapper) or metafile is a file format that allows multiple data streams to be embedded into a single file, usually along with metadata for identifying and further detailing those streams. Nota ...
s, the resulting file is valid, and so 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 Program stream (PS or MPEG-PS) is a container format for multiplexing digital audio, video and more. The PS format is specified in MPEG-1 Part 1 (ISO/IEC 11172-1) and MPEG-2 Part 1, Systems (ISO/IEC standard 13818-1/ITU-T H.222.0). The MPEG-2 ...
(MPEG-1 and MPEG-2) and DV (Digital Video) formats, which are fundamentally simple streams of packets.


Examples


Unix culture


Jargon file definition

The Jargon File version 4.4.7 lists this as the definition of cat:


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.shell
"Early award example of UUOC (1994)"
'' comp.unix.shell via Google Groups'', Retrieved on 1 Mars 2017.
In computing, the word "abuse", in the second sense of the definition, is used to disparage the excessive or unnecessary use of a language construct; thus, abuse of cat 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 In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin ...
instead, in either of the following forms (the first is more traditional):
 command arg1 arg2 argn < filename
 

Beyond other benefits, the input redirection forms allow ''command'' to perform  random access on the file, whereas the cat examples do not. This is because the redirection form opens the file as the stdin file descriptor which ''command'' can fully access, while the cat form simply provides the data as a stream of bytes.

Another common case where cat 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 examples
 cat file ,  grep pattern
 cat file ,  less
can instead be written as
 grep pattern file
 less file
A common interactive use of cat 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. A cat 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 In software engineering and computer science, clobbering a file, processor register or a region of computer memory is the process of overwriting its contents completely, whether intentionally or unintentionally, or to indicate that such an acti ...
, and one way to avoid this is to use cat with pipes. Compare:
 command < in ,  command2 > out
  out
with: cat in , command , command2 > out


See also

* paste *
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, entertai ...
, a command that splits a file into pieces which cat can then rejoin. * zcat *
less Less or LESS may refer to: fewer than,: not as much. Computing * less (Unix), a Unix utility program * Less (stylesheet language), a dynamic stylesheet language * Large-Scale Scrum (LeSS), a product development framework that extends Scrum Othe ...


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 of
Unix 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, an ...
. * * * * * {{Core Utilities commands Unix text processing utilities
Cat 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