The cat (concatenate) command is one of the most frequently used utilities in Unix/Linux systems.
While it’s often seen simply showing file contents, it can do much more—including creating, combining, inspecting, and transforming text streams efficiently.
Below are 10 practical, progressively advanced examples, each with explanation, pitfalls, and alternatives where appropriate.
Table of Contents
1. Display the Contents of a File
cat /etc/hostname
Use it for quick viewing of small files.
Tip: For long files, prefer less or bat (if installed).
Pitfall: Don’t use cat file | less; just run less file.
2. Concatenate Multiple Files Into One Stream
cat intro.txt chapter1.txt chapter2.txt > book.txt
This merges files in the given order into a new file.
Variation (append to an existing file):
cat appendix.txt >> book.txt
Tip: Order matters—cat a b c differs from cat c b a.
3. Create a New File from Keyboard Input (STDIN Redirection)
cat > notes.txt
Line one
Line two
Press Ctrl + D to save
This writes everything you type until EOF (Ctrl + D).
See also: Mastering the Linux Command Line — Your Complete Free Training Guide
Pitfall: Overwrites existing files without warning.
Safer alternative: Use a here-document (see Example 9) or nano, vim.
4. Append Text to an Existing File
cat >> todo.txt
Refactor logging subsystem
Ctrl + D to finish
Appends instead of overwriting.
Tip: Combine with a timestamp:
echo "$(date -Iseconds) – Deployed staging build" | cat >> deploy.log
5. Number All Output Lines (n) or Non-Blank Lines (b)
cat -n script.sh
Output looks like:
1 #!/usr/bin/env bash
2 set -e
3 echo "Starting..."
For numbering only non-empty lines:
cat -b script.sh
Use this to reference line numbers in discussions or debugging.
6. Reveal Hidden Characters (T, E, A)
Tshows TABs as^IEshows end-of-line markers ($)A(All) combinesvET(useful for invisible/control characters)
cat -A Makefile
Great for diagnosing formatting issues (e.g., stray Windows ^M carriage returns).
Remove Windows CRLF:
cat -A file.txt | tr -d '\\\\r' > cleaned.txt
7. Quickly Copy or Duplicate a File
Simple copy:
cat original.jpg > copy.jpg
But for binary files, prefer cp—cat is fine but less explicit.
Stream transform example (lowercasing a text file):
cat README.md | tr 'A-Z' 'a-z' > readme_lower.md
Tip: Avoid “Useless Use of cat” when the next command supports a filename:
Better:
tr 'A-Z' 'a-z' < README.md > readme_lower.md
8. Merge, Filter, and Process Multiple Files in a Pipeline
Combining logs and extracting unique error codes:
cat app1.log app2.log | grep -E 'ERROR|FATAL' | cut -d' ' -f4 | sort | uniq -c | sort -rn
Another: Combine partial files (e.g., chunked exports):
cat part-*.csv > complete.csv
Tip: For massive concatenations, find -name 'part-*.csv' -print0 | xargs -0 cat > complete.csv avoids shell glob limits.
9. Use Here-Documents with cat to Create Template Files
cat > docker-compose.yml <<'EOF'
services:
web:
image: nginx:alpine
ports:
- "8080:80"
EOF
- Quoted (
<<'EOF') prevents variable expansion. - Unquoted (
<<EOF) allows$VARexpansion.
Example with expansion:
APP_PORT=5000
cat > config.env <<EOF
PORT=$APP_PORT
MODE=production
EOF
10. Combine Process Substitution (Advanced Bash) for On-the-Fly Merging
If using Bash or Zsh, you can merge outputs of commands without temporary files:
cat <(printf "HEADER\\\\n") <(grep -v '^#' settings.conf) > final.conf
Another example—compare two command outputs:
diff <(cat prod.env | sort) <(cat staging.env | sort)
This pattern is powerful for build scripts and CI tasks.
Bonus Micro-Patterns
| Task | Better Pattern |
|---|---|
| View gzip file | zcat file.gz |
| Safe view of binary | `hexdump -C file.bin |
| Fast large-file preview | head -n 100 huge.log (not `cat huge.log |
| Continuous growth | tail -f app.log instead of repeatedly cat app.log |
| Paginate | less file.txt instead of `cat file.txt |
Common Pitfalls & Best Practices
- Avoid cat file | command when command accepts a file directly (e.g.,
grep pattern file). - Don’t use
catfor massive binary copy tasks—cporrsyncis clearer. - Use
cat -Ato debug whitespace bugs in YAML, Makefiles, or Python. - Use
>>carefully—accidental log growth may balloon disk usage.
Summary
The cat command is more than “print a file.” It:
- Creates and appends content interactively.
- Merges and numbers lines.
- Reveals formatting issues.
- Powers pipelines and dynamic file assembly.
Mastering these patterns improves your shell fluency and helps build reliable, scriptable workflows.
Related: 10 Examples to use Linux Cat Command



