cat | |
Author: | Ken Thompson, Dennis Ritchie |
Developer: | AT&T Bell Laboratories |
Operating System: | Unix, Unix-like, Plan 9, Inferno, ReactOS |
Platform: | Cross-platform |
Genre: | Command |
License: | coreutils GPLv3+ |
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").[1] [2] It has been ported to a number of operating systems.
The other primary purpose of cat
, aside from concatenation, is file printing allowing the computer user to view the contents of a file. Printing to files and the terminal are the most common uses of cat
.
cat
was part of the early versions of Unix, e.g., Version 1, and replaced pr
, a PDP-7 and Multics utility for copying a single file to the screen.[3] It was written by Ken Thompson and Dennis Ritchie.The version of cat
bundled in GNU coreutils was written by Torbjorn Granlund and Richard Stallman. The ReactOS version was written by David Welch, Semyon Novikov, and Hermès Bélusca.[4]
Over time, alternative utilities such as tac
and bat
also became available, bringing different new features.[5] [6]
The cat
utility serves a dual purpose: concatenating and printing.With a single argument, it is often used to print a file to the user's terminal emulator (or historically to a computer terminal or teletype). With more than one argument, it concatenates several files. The combined result is by default also printed to the terminal, but often users redirect the result into yet another file.[7] Hence printing a single file to the terminal is a special use-case of this concatenation program. Yet, this is its most common use.[8]
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.[9]
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 [options] [file_names]
Example of some cat
options:[10]
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. cat
does not provide a way to concatenate Unicode text files that have a Byte Order Mark 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 formats, 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 (MPEG-1 and MPEG-2) and DV (Digital Video) formats, which are fundamentally simple streams of packets.
Command | Explanation |
---|---|
cat file1.txt | Display contents of file |
cat file1.txt file2.txt | Concatenate two text files and display the result in the terminal |
cat file1.txt file2.txt > newcombinedfile.txt | Concatenate two text files and write them to a new file |
cat >newfile.txt | Create a file called newfile.txt. Type the desired input and press CTRL+D to finish. The text will be in file newfile.txt. |
cat -n file1.txt file2.txt > newnumberedfile.txt | Some implementations of cat, with option -n, can also number lines |
cat file1.txt > file2.txt | Copy the contents of file1.txt into file2.txt |
cat file1.txt >> file2.txt | Append the contents of file1.txt to file2.txt |
cat file1.txt file2.txt file3.txt | sort > test4 | Concatenate the files, sort the complete set of lines, and write the output to a newly created file |
cat file1.txt file2.txt | less | Run the program "less" with the concatenation of file1 and file2 as its input |
cat file1.txt | grep example | Highlight instances the word "example" in file1.txt |
command | cat | Cancel "command" special behavior (e.g. paging) when it writes directly to TTY (cf. UUOC below) |
The Jargon File version 4.4.7 lists this as the definition 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.[11] In computing, the word "abuse",[12] in the second sense of the definition, is used to disparage the excessive or unnecessary use of a language construct; thus, abuse of is sometimes called "cat abuse". Example of a common 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 < filename
<filename command arg1 arg2 argn
Beyond other benefits, the input redirection forms allow command to perform random access on the file, whereas the examples do not. This is because the redirection form opens the file as the stdin file descriptor which command can fully access, while the form simply provides the data as a stream of bytes.
Another common case where 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 for a single file is to output the content of a file to standard output. However, if the output is piped or redirected, is unnecessary.
A written with UUOC might still be preferred for readability reasons, as reading a piped stream left-to-right might be easier to conceptualize.[13] 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 use with pipes. Compare:
command < in | command2 > out
<in command | command2 > out
with: cat in | command | command2 > out