Linux cat Command: Display and Concatenate File Contents

The cat command writes file contents to standard output and concatenates multiple files in the order given. It is ideal for quickly reading short text files or joining data streams, while less is usually a better choice for browsing long files.

This guide explains the basic syntax, key options, practical examples, related commands, common cautions, and frequently asked questions for cat.

The name comes from concatenate. With no file operand, cat reads standard input. With multiple files, it writes each file in sequence without inserting an extra separator. It does not modify its inputs, but a shell redirection can create, truncate, or overwrite the destination file before cat begins reading.

Basic Syntax

cat [OPTION]... [FILE]...

Options and edge cases can vary by implementation and version. Check cat --help and man cat on the target system before relying on implementation-specific behavior.

Basic cat Usage

Display a file

Use cat for a short file whose complete contents fit comfortably in the terminal.

cat /etc/os-release

Concatenate multiple files

The inputs are written in operand order. No blank line or delimiter is added automatically.

cat part-01.txt part-02.txt

Save combined output

Redirect standard output to a new destination. Never use the same path as both an input and a truncating output.

cat chapter-1.txt chapter-2.txt > book.txt

Checking Standard Output Results

cat outputs the contents of the input file exactly in order. The following is an example of creating two sample lines and then checking them.

printf 'one\ntwo\n' > sample.txt
cat sample.txt

Example output:

one
two

cat does not stop a long file by page, so the results can pass by the screen quickly. To browse the content, use less.

Key Options

Option Purpose
-n Number every output line.
-b Number nonblank lines and override -n.
-s Squeeze repeated blank lines into one.
-E Show a $ marker at each line ending.
-T Display tab characters as ^I.
-A Show tabs, line endings, and other nonprinting characters.

Practical Examples

Review a file with line numbers

Line numbers help when an error message points to a specific line in a configuration file.

cat -n app.conf

Reveal hidden characters

Use this when trailing carriage returns, tabs, or unexpected control characters may be causing a parsing problem.

cat -A script.sh

Read standard input into a file

Press Ctrl+D to send end-of-file. For substantial editing, use an editor instead.

cat > memo.txt

cat and Related Commands

Command Primary role Best fit
cat Display or concatenate streams Short files and pipelines
less Interactive paging and search Long files
tac Reverse record order Reading the latest lines first
paste Merge corresponding lines Combining files as columns

Important Cautions

Use less for large files

Printing a large file floods the terminal and consumes scrollback without providing convenient navigation.

Do not redirect a file onto itself

In cat file > file, the shell can truncate the destination before cat opens it for reading.

Avoid sending binary data to a terminal

Control bytes can disrupt the display. Inspect binary data with file, hexdump, or xxd instead.

FAQ

Does cat modify the input file?

No. Its normal operation is read-only, although shell redirection can modify the output path.

Does cat add separators between files?

No. It writes the exact byte streams in sequence.

How do I show line numbers?

Use -n for all lines or -b for nonblank lines.

Should I use cat for a very long log?

You can, but less is more practical when you need navigation and search.

Related Resources