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
Linux journalctl Command: Read systemd Logs

Linux journalctl Command: Read systemd Logs

Learn how to read systemd Logs with the Linux journalctl command, including practical examples, key options, and important precautions.

Linux renice Command: Change the Priority of a Running Process

Linux renice Command: Change the Priority of a Running Process

Learn how to change the Priority of a Running Process with the Linux renice command, including practical examples, key options, and important precautions.

Linux last Command: Review Login and Reboot History

Linux last Command: Review Login and Reboot History

Learn how to use the Linux last command to review user login sessions, system reboots, and shutdown history, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux whereis Command: Locate Binaries, Source, and Man Pages

Linux whereis Command: Locate Binaries, Source, and Man Pages

Learn how Linux whereis locates binaries, source files, and manual pages, limits each search type, lists effective paths, and differs from which and find.

Linux lsmod Command: List Loaded Kernel Modules

Linux lsmod Command: List Loaded Kernel Modules

Learn how to list Loaded Kernel Modules with the Linux lsmod command, including practical examples, key options, and important precautions.

Linux nice Command: Set a New Process's Scheduling Priority

Linux nice Command: Set a New Process's Scheduling Priority

Learn how to set a New Process's Scheduling Priority with the Linux nice command, including practical examples, key options, and important precautions.

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Learn how to inspect Disks, Partitions, and Mounts with the Linux lsblk command, including practical examples, key options, and important precautions.

Linux unalias Command: Remove Command Aliases

Linux unalias Command: Remove Command Aliases

Learn how to use the Linux unalias command to remove one or all aliases from the current shell session, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux systemctl Command: Manage systemd Services

Linux systemctl Command: Manage systemd Services

Learn how to manage systemd Services with the Linux systemctl command, including practical examples, key options, and important precautions.

Linux mktemp Command: Create Secure Temporary Files and Directories

Linux mktemp Command: Create Secure Temporary Files and Directories

Learn how to create unpredictable temporary files and directories with Linux mktemp, use templates and TMPDIR, clean up with trap, and avoid unsafe dry runs.