Viewing and processing text

Command cat

Output or merge files

Linux · Bash examples · commands run on the selected server

When to use cat

Use cat to display small text files or concatenate streams. For large logs, paging and filtering tools are easier to navigate and avoid flooding the terminal with irrelevant data.

Syntax

cat [OPTIONS] [FILE...]

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

-n
Number all lines.
-b
Number non-empty lines.
-s
Compress consecutive empty lines.

Usage examples

Show text file

cat ./README.md

The file contents are sent to standard output without changing the source.

Merge files

cat ./part-1.txt ./part-2.txt > ./combined.txt

Both parts are concatenated in order; redirection creates or replaces the combined output file.

View file with line numbers

cat -n ./app.conf

Line numbers are added to the displayed configuration, leaving the file unchanged.

Notes and limitations

  • The > redirection truncates its destination before reading starts. Never use the same path for input and output.
  • Use less or tail for large logs. Binary data can disrupt the terminal display.

Common questions and troubleshooting

Why did my output file become empty?

Redirecting into a file that is also an input can truncate it before cat reads it. Use a separate output path and replace the original only after checking.

Should I display unknown binary files with cat?

Binary control bytes can disrupt terminal output. Identify unknown files first and use a suitable viewer or byte inspection tool.

Further help

Options vary by version and distribution. Check the installed documentation with this command: man cat.