LinuxCommandLibrary

cat

Concatenate and display file content

TLDR

Print the contents of a file to stdout

$ cat [path/to/file]
copy

Concatenate several files into an output file
$ cat [path/to/file1 path/to/file2 ...] > [path/to/output_file]
copy

Append several files to an output file
$ cat [path/to/file1 path/to/file2 ...] >> [path/to/output_file]
copy

Copy the contents of a file into an output file without buffering
$ cat -u [/dev/tty12] > [/dev/tty13]
copy

Write stdin to a file
$ cat - > [path/to/file]
copy

SYNOPSIS

cat [OPTION]... [FILE]...

PARAMETERS

-A, --show-all
    equivalent to -vET

-b, --number-nonblank
    number nonempty output lines, overrides -n

-e
    equivalent to -vE

-E, --show-ends
    show $ at end of each line

-n, --number
    number all output lines

-s, --squeeze-blank
    suppress repeated empty output lines

-t
    equivalent to -vT

-T, --show-tabs
    display TAB characters as ^I

-u, --unbuffered
    (ignored)

-v, --show-nonprinting
    use ^ and M- notation except LFD and TAB

--help
    display this help and exit

--version
    output version information and exit

DESCRIPTION

The cat command (concatenate) is a core Unix utility for reading files sequentially and writing their contents to standard output. It excels at quick file inspection, merging multiple files, and feeding data into pipelines.

Basic usage: cat file.txt prints a file's content. For concatenation: cat file1.txt file2.txt > combined.txt merges files. It processes files in order, reading from stdin if no files are given or if - is specified.

Options enhance visibility: -n numbers lines, -A shows all non-printing characters, tabs as ^I, and line ends as $. Suppress blank lines with -s. Ideal for scripts and pipes, e.g., cat data.txt | sort | uniq.

Though simple, cat is ubiquitous in shell scripting for input/output manipulation. Avoid for very large files without redirection or piping to pagers like less, as it dumps everything at once. Supports text and binary files but may garble terminals with binaries.

CAVEATS

Binary files may corrupt terminal display; use hexdump or xxd. No built-in paging for large files—pipe to less. Processes entire files sequentially without seeking.

COMMON EXAMPLES

cat file.txt: View file.
cat *.log > all_logs.txt: Concatenate.
cat -n script.sh: Number lines.
cat -A config: Reveal hidden chars.

HISTORY

Originated in 1971 for first Edition Unix by Ken Thompson at Bell Labs. Core POSIX utility, evolved in GNU coreutils with enhanced options.

SEE ALSO

tac(1), more(1), less(1), head(1), tail(1)

Copied to clipboard