Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Viewing and Searching Files

Linux cat command

Reference for the Linux cat command: print and join files, number lines with -n, reveal hidden characters with -A, squeeze blank lines and write with here-documents.

cat (short for concatenate) reads files in the order you list them and writes their contents, joined together, to standard output. With one file it displays it; with several it glues them into one stream; with no file, or with -, it reads standard input.

Combined with redirection, cat is the usual way to join files (cat a b > c) and, with a here-document, to create small files from a script. For long files use a pager such as less, because cat does not stop at the end of a screen.

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

Try it

Bash Join files and number lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Show hidden characters and create files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
cat [-AbeEnstTuv] [FILE]...
Option Description
-n, --number Number all output lines.
-b, --number-nonblank Number only non-blank lines (overrides -n).
-s, --squeeze-blank Replace several adjacent blank lines by one.
-E, --show-ends Show $ at the end of each line.
-T, --show-tabs Show tabs as ^I.
-v, --show-nonprinting Show control and non-ASCII characters in ^ and M- notation (except tab and newline).
-A, --show-all Same as -vET.
-e / -t Same as -vE / -vT.
-u Ignored (for compatibility).

Returns: 0 on success; 1 if any file could not be read (the others are still printed).

Usage notes

  • Order matters. cat a b prints a first. Wildcards expand in sorted order, so cat part-*.txt joins parts in name order.
  • Never redirect onto an input file: cat a b > a truncates a before it is read and loses data. Write to a new file, then move it.
  • A file without a final newline is joined directly to the next file's first line; the output of cat does not add a newline. Check with cat -A.
  • cat file | command is usually unnecessary. Most commands take file names or < redirection (grep x file, sort < file). Avoiding the extra process is cleaner, though harmless for small data. This is the "useless use of cat".
  • Hidden characters explain many puzzles: Windows line endings show as ^M$ with -A, and a trailing space shows before the $. Byte-level detail is in od.
  • Binary files printed to the terminal can scramble it. Use cat -v, or strings or file first. If the terminal is garbled, type reset.
  • Here-documents with a quoted delimiter (<<'EOF') stop variable expansion, as in the example where $HOME stays literal. Without quotes the shell expands it. See Here-documents.
  • Speed: cat is fast, as it simply copies blocks; -n, -s and the show options process line by line and are a bit slower.
  • Compare: tac reverses line order, nl numbers lines with more control, head and tail print parts.