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.

What Is the cat Command?

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

More in This Category
How to Check IP Addresses on Linux

How to Check IP Addresses on Linux

Learn how to check private and public IP addresses on Linux, identify the active interface and route, and distinguish IPv4, IPv6, and externally visible addresses.

Linux join Command: Combine Files by a Common Field

Linux join Command: Combine Files by a Common Field

Learn how to combine Files by a Common Field with the Linux join command, including practical examples, key options, and important precautions.

Linux mount Command: Inspect and Mount Filesystems

Linux mount Command: Inspect and Mount Filesystems

Learn how to inspect and Mount Filesystems with the Linux mount command, including practical examples, key options, and important precautions.

Linux free Command: Check Memory and Swap Usage

Linux free Command: Check Memory and Swap Usage

Learn how to use the Linux free command to inspect available memory, used memory, cache, and swap usage, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux cmp Command: Compare Files Byte by Byte

Linux cmp Command: Compare Files Byte by Byte

Learn how Linux cmp compares two files byte by byte, reports the first mismatch, runs silently in scripts, limits ranges, and differs from diff and checksums.

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Understand what PID and PPID mean, inspect parent-child process relationships with ps and pgrep, and safely terminate a process you started yourself.

Linux grep Command: Search Text and Filter Command Output

Linux grep Command: Search Text and Filter Command Output

Learn how to use the Linux grep command to search files for text or regular expressions and filter command output, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux gunzip Command: Decompress gzip Files

Linux gunzip Command: Decompress gzip Files

Learn how to decompress gzip Files with the Linux gunzip command, including practical examples, key options, and important precautions.

Linux diff Command: Compare Text Files and Directories

Linux diff Command: Compare Text Files and Directories

Learn how Linux diff compares text files and directories, produces unified patches, ignores selected whitespace changes, and reports differences through exit status.

Linux file Command: Identify File Types and Formats

Linux file Command: Identify File Types and Formats

Learn how to use the Linux file command to identify file formats from their contents instead of filename extensions, with essential options, practical examples, output interpretation, and common troubleshooting tips.