cat
Overview
cat (concatenate) reads files sequentially and writes them to standard output. Use it to print small files, join files, and write heredocs. For large files, prefer a pager (less). For syntax-highlighted viewing, consider bat. For following growth, use tail -f / journalctl -f.
Classic Unix joke still holds: cat file | something is often better as something < file or something file.
Syntax
cat [options] [file ...]With no file (or -), reads standard input.
Common Options
| Option | Description |
|---|---|
-n |
Number all output lines |
-b |
Number non-blank lines |
-s |
Squeeze multiple blank lines |
-A |
Show all non-printing (-vET) |
-E |
Show $ at end of lines |
-T |
Show tabs as ^I |
-v |
Show non-printing characters |
-u |
Unbuffered (historical; often default) |
-- |
End of options |
Key Use Cases
- Print small config/text files
- Concatenate parts into one file
- Create files via heredoc
- Reveal invisible characters (
-A) - Quick copy via redirect
Examples with Explanations
Print files
cat file.txt
cat /etc/os-release
cat -n file.txt # number lines
cat -A file.txt # show tabs/line endingsConcatenate
cat part1 part2 part3 > whole
cat part1 part2 >> whole # append
cat header.json body.json > combined.jsonHeredocs
cat <<'EOF' > greeting.txt
hello
world
EOF
cat <<EOF >> /etc/hosts
# added by bootstrap
10.0.0.5 app.local
EOFQuoted 'EOF' disables expansion; unquoted EOF expands $vars and command substitutions.
Here-string and stdin
cat <<< 'single line'
cat - <<'EOF' | ssh host 'cat > /tmp/x'
content
EOFShow non-printing / DOS endings
cat -A dosfile.txt
# CRLF shows as ^M$Useless use of cat — avoid
# avoid
cat file | grep pattern
# prefer
grep pattern file
grep pattern < fileBinary caution
cat binary.dat # can mess up terminal
cat binary.dat | xxd | less # betterMultiple files with separators (manual)
for f in *.conf; do
echo "===== $f ====="
cat "$f"
done | lessNotes / Pitfalls
- Large files: use
less,tail, or streaming tools; don’t dump multi-GB logs withcat. cat file1 file2 > file1truncates file1 first — data loss. Write to a new name.- Terminal corruption from binary output:
resetortput reset. - Order matters for concatenation; globs are sorted by shell locale.
- BusyBox
catmay support fewer flags.
2026-relevant notes
- Prefer
batfor interactive reading; keepcatfor scripts and POSIX pipelines. systemd-catsends stdin to the journal — different tool for logging.- For cloud-init / config blobs, heredoc with
cat <<'EOF'remains standard in shell provisioning.
Additional Resources
man cat