10 Examples to use Linux Cat Command

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.


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)

  • T shows TABs as ^I
  • E shows end-of-line markers ($)
  • A (All) combines vET (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 cpcat 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 $VAR expansion.

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

TaskBetter Pattern
View gzip filezcat file.gz
Safe view of binary`hexdump -C file.bin
Fast large-file previewhead -n 100 huge.log (not `cat huge.log
Continuous growthtail -f app.log instead of repeatedly cat app.log
Paginateless 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 cat for massive binary copy tasks—cp or rsync is clearer.
  • Use cat -A to 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

David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 275

Leave a Reply

Your email address will not be published. Required fields are marked *