The cat command concatenates files and writes their contents to standard output.
It is commonly used to quickly inspect files, combine multiple files, and feed content into pipelines.
Overview
Use cat when you need to:
- Display file contents directly in the terminal
- Concatenate several files into one output stream
- Create small files from terminal input
- Number lines or visualize non-printing characters
- Bridge content between files and pipelines
Syntax
cat [options] [file...]
Common forms:
# Display one file
cat notes.txt
# Concatenate multiple files in order
cat part1.txt part2.txt part3.txt
# Read from standard input explicitly
cat -
# Write output to a new file via redirection
cat source.txt > copy.txt
If no files are provided, cat reads from standard input.
Common Options
| Option | Purpose | Example |
|---|---|---|
-n |
Number all output lines | cat -n file.txt |
-b |
Number non-empty output lines | cat -b file.txt |
-s |
Squeeze repeated blank lines | cat -s file.txt |
-E |
Show $ at end of each line |
cat -E file.txt |
-T |
Show tab characters as ^I |
cat -T file.txt |
-v |
Show non-printing chars (except tab/newline) | cat -v file.txt |
-A |
Equivalent to -vET (show all) |
cat -A file.txt |
Behavior Notes
- Input files are processed left to right.
catdoes not modify source files.- Binary files can produce unreadable terminal output; redirect when needed.
- Large outputs may scroll quickly, so pair with
lesswhen reviewing.
Examples
# Display a configuration file
cat config.yaml
# Concatenate log segments into one file
cat app.log.1 app.log.2 app.log.3 > combined.log
# Number all lines
cat -n script.sh
# Number non-blank lines only
cat -b document.txt
# Visualize tabs and line endings
cat -A data.tsv
# Collapse repeated blank lines before viewing
cat -s notes.txt
# Create a file from terminal input (Ctrl+D to finish)
cat > quick-note.txt
# Append terminal input to existing file (Ctrl+D to finish)
cat >> quick-note.txt
# Combine with grep in a pipeline (usually grep can read files directly)
cat app.log | grep -i error
Here-Document Alternative
For multi-line file creation in scripts, a here-document is often clearer:
cat > app.env <<'EOF'
APP_NAME=sample
APP_ENV=dev
LOG_LEVEL=info
EOF
Safe Usage Guidelines
- Use
lessfor long output:cat file | less. - Be careful with redirection targets to avoid overwriting files accidentally.
- Use
-Awhen debugging whitespace-sensitive files. - Prefer direct command input over useless pipes when possible (
grep pattern fileinstead ofcat file | grep pattern).
Performance and Practical Tips
- For very large files,
less,head, andtailare often more efficient for interactive inspection. - Use
catto concatenate files; usepaste/awkwhen column-aware merging is required. - Avoid printing binary files directly to interactive terminals.
Troubleshooting
cat: file: No such file or directory
Cause: Wrong path or filename.
Fix: Verify path and current directory.
pwd
ls -la
cat ./relative/path/file.txt
Terminal appears garbled after viewing a file
Cause: Binary or control characters were written to the terminal.
Fixes:
- Use
cat -vorhexdump -Cfor inspection - Reset terminal state with
reset
Unexpected overwrite when creating files with cat > file
Cause: > truncates destination before writing.
Fix: Use >> to append, or verify target path before running.
Notes
cat is simple and ubiquitous, but it is best paired with other tools for filtering,
searching, and paging.
For command details and implementation-specific behavior, run:
man cat
cat --help