cat
Concatenate and display file content
TLDR
Print the contents of a file to stdout
Concatenate several files into an output file
Append several files to an output file
Copy the contents of a file into an output file without buffering
Write stdin to a file
SYNOPSIS
cat [OPTION]... [FILE]...
Reads files sequentially, writing them to standard output. If FILE is not specified, or if FILE is '-', cat reads from standard input.
PARAMETERS
-b, --number-nonblank
Number non-blank output lines, starting at 1.
-E, --show-ends
Display '$' at the end of each line.
-n, --number
Number all output lines, starting at 1.
-s, --squeeze-blank
Squeeze multiple adjacent blank lines into a single blank line.
-T, --show-tabs
Display TAB characters as '^I'.
-v, --show-nonprinting
Use '^' and 'M-' notation for non-printing characters, except for LFD and TAB.
-A, --show-all
Equivalent to -vET. Shows tabs, ends, and non-printing characters.
-u, --nobuf
Unbuffered output. (Often ignored by GNU `cat` as output is typically unbuffered by default or handled by the kernel).
--help
Display a help message and exit.
--version
Output version information and exit.
DESCRIPTION
cat, short for concatenate, is one of the most fundamental and frequently used commands in Unix-like operating systems. Its primary purpose is to read files sequentially and write their contents to the standard output. If no files are specified, or if a hyphen (-) is given as a filename, cat reads from standard input.
The command is versatile: it can display the content of a single file, combine multiple files into one (concatenation), or create new files from user input. While simple, cat is a powerful tool when combined with shell redirection operators. For instance, `cat file1 file2 > combined_file` will merge the contents of `file1` and `file2` into `combined_file`. It's often used for quick inspection of text files or as part of a larger pipeline to process data.
However, it's important to note that cat simply dumps content without pagination, which means for very large files, the output might scroll off the screen quickly. Its simplicity and effectiveness make it a cornerstone of shell scripting and daily command-line operations.
CAVEATS
- Binary Files: Using cat on binary files can result in garbled output on your terminal, potentially corrupting display settings, and may even cause beeps or other undesirable side effects.
- Large Files: cat simply dumps the entire file content to standard output without any paging. For very large files, this means the content will scroll past quickly, making it difficult to read or inspect interactively. Use pagers like less or more for large files.
- "Useless Use of Cat" (UUOC): A common anti-pattern where cat is used unnecessarily. For example, `cat file | grep pattern` is less efficient than `grep pattern file`. While harmless, it adds an extra process.
STANDARD INPUT INTERACTION
When cat is invoked without any FILE arguments (e.g., just `cat`), or if `-` is specified as a filename, it reads directly from standard input (stdin). This means it will echo whatever you type into the terminal until you signal the end of the input (usually by pressing Ctrl+D on a new line). This interactive mode is often used for quickly creating small text files or testing command pipelines.
FILE CREATION AND APPENDING WITH REDIRECTION
One of the most common and powerful uses of cat is in conjunction with shell redirection operators. You can create a new file by directing cat's output: `cat > new_file.txt`. You can append to an existing file using `cat >> existing_file.txt`. In both cases, cat reads from standard input until EOF (Ctrl+D), writing whatever you type to the specified file. This makes cat a simple text editor for quick, non-interactive tasks.
HISTORY
The cat command has been a staple of Unix-like operating systems since their early days. It originated in AT&T Bell Labs as part of the initial Unix development in the early 1970s. Its name, concatenate, directly reflects its primary function of joining files together. As a fundamental utility, its core functionality has remained remarkably consistent over decades, a testament to its simple yet effective design.
While its basic use is straightforward, cat gained notoriety in the Unix community for the concept of "Useless Use of Cat" (UUOC). This refers to scenarios where cat is piped into another command that could directly read from a file, such as `cat file | wc -l` instead of `wc -l file`. Despite this playful criticism, cat remains an indispensable tool for displaying file contents, interacting with standard input/output, and combining text streams in shell scripts. Its enduring presence underscores its foundational role in the Unix philosophy of small, specialized tools that do one thing well.