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.
cat [OPTION]... [FILE]...
Try it
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.
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
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 bprintsafirst. Wildcards expand in sorted order, socat part-*.txtjoins parts in name order. - Never redirect onto an input file:
cat a b > atruncatesabefore 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
catdoes not add a newline. Check withcat -A. cat file | commandis 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 inod. - Binary files printed to the terminal can scramble it. Use
cat -v, orstringsorfilefirst. If the terminal is garbled, typereset. - Here-documents with a quoted delimiter (
<<'EOF') stop variable expansion, as in the example where$HOMEstays literal. Without quotes the shell expands it. See Here-documents. - Speed:
catis fast, as it simply copies blocks;-n,-sand the show options process line by line and are a bit slower. - Compare:
tacreverses line order,nlnumbers lines with more control,headandtailprint parts.
Related
tac,less,head,tail,nl- Lessons: Viewing text, Here-documents