Guides And Explainers

cat command in Ubuntu: a definitive guide

The cat command in Ubuntu reads, concatenates, and outputs files and standard input. It is commonly used to display file contents, join files into one stream, number lines, and...

Mara Ellison
cat command in Ubuntu: a definitive guide

What the cat command does in Ubuntu

The cat command in Ubuntu reads, concatenates, and outputs files and standard input. It is commonly used to display file contents, join files into one stream, number lines, and write literal text into new files. The name derives from concatenation, and it is a small, composable utility aligned with Unix philosophy. Because cat processes input and output as streams, it works with pipes, redirections, and scripts, making it durable across workflows.

Common use cases for cat in Ubuntu

You can use cat to preview plain-text logs, inject small configurations, or stitch together reports without launching an editor. It works well for concatenating multiple files (for example, combining CSVs), creating quick test data, and verifying build artifacts. Scripts often use cat to chain commands or preprend boilerplate text. In contrast, avoid cat for very large files, since dumping huge content to the terminal can overwhelm your session.

Piping and redirection basics

Because cat writes to standard output, you can pipe it to less, grep, wc, or sort. You can redirect stdout to a new file with >, append with >>, or feed input into another program. Combining these patterns lets you filter, transform, and route text without intermediate files. Shell expansions and quoting rules apply, so careful quoting protects against unwanted word splitting and globbing.

Basic syntax and flags

Typical patterns include cat followed by one or more filenames, or here-documents for inline text. Flags alter output behavior, for example numbering all lines or visible tabs. Below is a compact reference table for commonly used options in Ubuntu.

Quick flags reference

FlagEffectNotes
-nNumber all output linesUseful for reviews and scripts
-bNumber nonempty lines onlySkips blank lines in numbering
-sSqueeze multiple blank linesReduces excessive empty space
-vShow nonprinting charactersHelpful for debugging whitespace
-eImplies -v and shows line endings as $Quick visibility of line endings
-tImplies -v and shows tabs as ^IReveals indentation problems

Behavior notes

By default, cat copies input to output verbatim, preserving bytes as they appear. Some flags change presentation without altering the underlying content, so downstream tools see the original data. Buffering may differ between interactive terminals and scripts, which can affect perceived performance in pipelines.

Practical examples you can use today

These common patterns demonstrate how cat fits into daily workflows in Ubuntu, from quick inspection to script-friendly constructions.

View a file with line numbers

To inspect a configuration while preserving readability:

cat -n /etc/nginx/nginx.conf

Use -b if you only care about lines that contain directives or values.

Combine multiple files

Concatenate logs for a summary without editing each individually:

cat /var/log/app/access-*.log | less

Create a simple text file inline

Write a short note directly into a new document:

cat > notes.txt << EOF
Meeting agenda
- Topics
- Decisions
EOF

Expose special characters in a file

Debug trailing whitespace or line-ending issues:

cat -vet requirements.txt

This combines -v, -e, and -t to make tabs, dollar endings, and other markers visible.

Append output to an existing file

Grow a report safely without overwriting earlier data:

cat newdata.txt >> report.txt

When not to use cat in Ubuntu

cat is fast and simple, yet it has limits. Avoid it for very large files, because dumping gigabytes to the terminal can freeze your session or scroll past useful context. Prefer streaming viewers like less, head, or tail when you need incremental inspection. For complex edits or structured data, dedicated tools such as sed, awk, jq, or editors are safer and more expressive.

Guidelines to choose the right tool

  • Preview small configs: cat -n file
  • Combine many logs: cat file*.log | less
  • Create quick text: cat > file << EOF
  • Examine hidden characters: cat -vet file
  • Large files: use less, head, or tail instead

Common pitfalls and troubleshooting

Misuse can lead to confusion, data loss, or unexpected output. Knowing these patterns helps you avoid mistakes and recover quickly.

Overwriting the wrong file

Redirecting with > without intending to replace can erase data silently. Always double-check paths and operators before pressing Enter. Consider using set -u in scripts to catch undefined variables.

Accidental binary output

Running cat on a binary file sends garbage to your terminal, which may reset your shell session. If this happens, type reset and press Enter to restore a clean prompt.

Ignoring exit codes

cat returns non-zero when it cannot read a file. In scripts, you can set -e or explicitly check $?, or use head/tail for partial reads when permissions or missing files are possible.

Advanced patterns and integration

In larger pipelines, cat works as a transparent pass-through or a simple combiner. It pairs naturally with grep, awk, sed, and compression tools, and its stream-based design keeps workflows memory-efficient in Ubuntu.

Using cat in scripts and automation

Scripts can rely on cat for consistent behavior across environments, but you should quote variables and avoid unnecessary cat when a tool can read files directly (for example, grep pattern file). Here is a minimal safe pattern:

if [ -f "$file" ]; then
  cat "$file" | process.sh
fi

Combining with compression and remote sources

You can decompress on the fly and concatenate with cat, or stream content over SSH. These patterns extend cat’s reach without changing its core behavior:

zcat logs.gz | cat -n
ssh host cat /var/log/messages | grep ERROR

Best practices and alternatives

Write predictable pipelines, avoid unnecessary cat (often called UUOC—Useless Use of Cat), and prefer purpose-built tools when they simplify logic. Use cat where its strengths—concatenation, quick display, and stream friendliness—align with your task.

When to prefer other tools

  • Quick partial views: head, tail
  • Line-based edits: sed, awk
  • Structured data: jq (JSON), csvkit (CSV)
  • Large files: less, bat, or pagers
  • Binary inspection: xxd, file

Summary and key takeaways

The cat command in Ubuntu is a foundational utility for reading, concatenating, and streaming text. Use it for small files, quick combinations, and pipeline glue. Apply the right flags for visibility and numbering, and avoid it for huge binaries or complex transformations. When you understand its limits, cat remains a reliable, script-friendly tool in everyday workflows.

Related Reading

More pages in this topic cluster.

How to Create a Text File in Command Prompt: A Verified Guide

To create a text file in Command Prompt on Windows, you can use command-line tools such as echo , type nul > filename.txt , or notepad filename.txt . On Linux and macOS terminal...

Read next
How to Go Back to the Previous Directory in Command Line

In command-line workflows, moving between directories is routine. The need to go back to the previous directory often arises when inspecting files, running scripts, or chaining...

Read next
How to Create a New File in the Terminal: A Cross-Platform Guide

Creating a new file terminal workflow is foundational for efficient, scriptable work across macOS, Linux, and Windows. Files created in the terminal integrate cleanly with autom...

Read next