What Does the Cat Command Do in Linux or Unix?

The cat command reads one or more files and prints their contents to your terminal. Its name is short for “concatenate,” because its core job is joining files together, but most people use it simply to display what’s inside a file. It’s one of the most fundamental commands in Linux and macOS terminals, originally written by Ken Thompson and Dennis Ritchie for Unix in 1971.

Displaying a File’s Contents

The simplest and most common use of cat is viewing a file:

cat myfile.txt

This prints the entire file to your screen from top to bottom, then returns you to the command prompt. Unlike interactive tools like less or more, cat doesn’t let you scroll or search. It dumps everything at once. That makes it ideal for short files (config files, logs, quick notes) but impractical for anything longer than your terminal window, since the beginning scrolls out of view.

Combining Multiple Files

This is what cat was actually designed for. You can merge several files into one by listing them in order and redirecting the output:

cat file1.txt file2.txt file3.txt > combined.txt

The > operator creates a new file (or overwrites an existing one) with the combined contents. If you want to add to an existing file instead of replacing it, use >>:

cat newdata.txt >> existing.txt

There’s an important gotcha here. If you try to concatenate files back into one of the source files, like cat file1.txt file2.txt > file1.txt, the shell empties file1.txt before cat even starts reading it. You’ll lose its contents. Always write to a different filename when combining files.

Useful Flags

A handful of options modify how cat displays output:

  • -n adds line numbers to every line of output, numbered sequentially from 1. Helpful when you need to reference specific lines.
  • -b adds line numbers only to non-blank lines, skipping empty ones. Use this with -n when blank lines would clutter the numbering.
  • -s squeezes repeated blank lines down to a single blank line, cleaning up files with excessive spacing.
  • -v makes invisible characters visible. Control characters show up as ^ followed by a letter (like ^M for carriage returns), which is useful for debugging files that came from Windows or contain hidden formatting.
  • -T displays tab characters as ^I so you can distinguish tabs from spaces.

You can combine flags. Running cat -ns myfile.txt would number every line while also collapsing consecutive blank lines.

Creating Files With Heredocs

You can use cat with a “here document” to create multi-line files directly from your terminal or inside a script, without opening a text editor:

cat > newfile.txt <<EOF
This is line one.
This is line two.
This is the last line.
EOF

Everything between <<EOF and the closing EOF gets written into newfile.txt. The delimiter word (here “EOF”) can be anything you choose, as long as the opening and closing markers match. This pattern shows up constantly in shell scripts that need to generate configuration files or multi-line output.

How Cat Handles Large and Binary Files

cat does not load an entire file into memory. It reads in fixed-size chunks, typically 128 kilobytes at a time, and writes each chunk to output before reading the next. This means it uses a constant amount of memory regardless of file size. You can safely run cat on a 50 GB file without worrying about memory, though your terminal will be busy for a while printing all that text.

cat works on binary files too, but the results aren’t pretty. It prints the raw data, including non-printable characters that can scramble your terminal display. If your terminal starts behaving strangely after accidentally catting a binary file, typing reset usually fixes it. For inspecting binary files, tools like hexdump or xxd give you a readable hex representation instead.

When to Use Something Else

For long files, less is almost always a better choice. It lets you scroll up and down, search with /, and quit with q without flooding your terminal history. The more command does something similar but only scrolls forward.

If you only need the beginning or end of a file, head and tail print the first or last 10 lines by default. The tail -f command is especially useful for log files: it prints the last few lines and then keeps watching, displaying new lines as they’re added in real time.

There’s also a well-known anti-pattern called “Useless Use of Cat,” where people pipe cat into another command unnecessarily. For example, cat file.txt | grep "error" works, but grep "error" file.txt does the same thing without spawning an extra process. Most commands that read from a pipe can also accept a filename directly. Using cat in a pipeline only makes sense when you’re genuinely feeding multiple files into a command or when the receiving command can’t take a filename argument.