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.
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 (OpenBSD manual page and the GNU Core Utiltites version of cat):
- -b (GNU: --number-nonblank), number non-blank output lines
- -e implies -v but also display end-of-line characters as $ (GNU only: -E the same, but without implying -v)
- -n (GNU: --number), number all output lines
- -s (GNU: --squeeze-blank), squeeze multiple adjacent blank lines
- -t implies -v, but also display tabs as ^I (GNU: -T the same, but without implying -v)
- -u use unbuffered I/O for stdout. POSIX does not specify the behavior without this option.
- -v (GNU: --show-nonprinting), displays nonprinting characters, except for tabs and the end of line character
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.
|cat file1.txt||Display contents of file|
|cat file1.txt file2.txt||Concatenates 2 text files and will show them in the terminal|
|cat file1.txt file2.txt > newcombinedfile.txt||Concatenates 2 text files and writes them to a new file|
|cat >newfile.txt||Creates 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||Redirects standard output of a file into a new file|
|cat file1.txt >> file2.txt||Appends standard output of a file into a new file|
|cat file1.txt file2.txt file3.txt | sort > test4||Output of cat command is piped to sort and result will be redirected in a newly created file.|
|cat file1 file2 | less||file1 and file2 is redirected as input to another program, less|
Jargon file definition
The Jargon File version 4.4.7 lists this as the definition of cat:
- To spew an entire file to the screen or some other output sink without pause (syn. blast).
- By extension, to dump large amounts of data at an unprepared target or with no intention of browsing it carefully. Usage: considered silly. Rare outside Unix sites. See also dd, BLT.
Among Unix fans, cat(1) is considered an excellent example of user-interface design, because it delivers the file contents without such verbosity as spacing or headers between the files, and because it does not require the files to consist of lines of text, but works with any sort of data.
Among Unix critics, cat(1) is considered the canonical example of bad user-interface design, because of its woefully unobvious name. It is far more often used to blast a single file to standard output than to concatenate two or more files. The name cat for the former operation is just as unintuitive as, say, LISP's cdr.
UUOC (Useless Use Of Cat)
UUOC (from comp.unix.shell on Usenet) stands for "useless use of cat". comp.unix.shell observes: "The purpose of cat is to concatenate (or catenate) files. If it is 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". The activity of fixing instances of UUOC is sometimes called demoggification. Example of a common cat abuse is given in the award:
cat filename | command arg1 arg2 argn
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 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, and one way to avoid this is to use cat with pipes. Compare:
command < in | command2 > out <in command | command2 > out
cat in | command | command2 > out
- McIlroy, M. D. (1987). A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 (PDF) (Technical report). CSTR. Bell Labs. 139.
- GNU Coreutils. "GNU Coreutils manual", GNU, Retrieved on 1 Mars 2017.
- comp.unix.shell. "Early award example of UUOC (1994)", comp.unix.shell via Google Groups, Retrieved on 1 Mars 2017.
- Rose, John (2012). "Demoggification". YouTube. SoutEast LinuxFest.
- Nguyen, Dan. "Stanford Computational Journalism Lab". stanford.edu. Retrieved 2017-10-08.
- The Single UNIX Specification, Issue 7 from The Open Group : concatenate and print files – Commands & Utilities Reference,
- 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.
- GNU Coreutils reference : concatenate and write files –
- OpenBSD General Commands Manual : concatenate and print files –
- FreeBSD General Commands Manual –
- Plan 9 Programmer's Manual, Volume 1 –
- Useless Use Of Cat Award
- 13 examples of cat and tac usage from tecmint