The cat command is one of the first commands every Linux user learns, and for good reason. Despite its simplicity, cat hides a surprising amount of flexibility, and mastering it will make your daily work on the command line faster and more confident. In practice, short for concatenate, it reads files and writes their contents to standard output, making it the simplest way to look inside a text file, combine several files into one, or create a new file straight from the terminal. This guide walks you through how to use the cat command in Linux, from the basic syntax to practical options, real-world examples, and the mistakes beginners usually make.
What Is the cat Command in Linux?
cat is a core utility that ships with virtually every Unix-like system. Still, its job is straightforward: it takes one or more files, reads them sequentially, and prints the result to the standard output (usually your terminal). If you give it no file arguments, it reads from standard input, which means it waits for you to type something That alone is useful..
The command is part of the GNU Core Utilities, so the version you find on most Linux distributions supports a rich set of options. On macOS and BSD systems the options are slightly fewer, but the core behavior is identical Worth keeping that in mind..
Basic Syntax
cat [OPTIONS] [FILE...]
- OPTIONS modify how the content is displayed.
- FILE... is a list of one or more files to read. Multiple files are processed in the order you list them.
Viewing the Contents of a File
The most common use of cat is simply reading a file:
cat notes.txt
The entire file is dumped to your terminal at once. This works beautifully for short files such as configuration snippets, scripts, or a quick README. For long files, the output scrolls past faster than you can read it, which is where tools like less become more practical.
You can also read several files in one go:
cat file1.txt file2.txt
The contents appear back to back, in the exact order you specified.
Creating Files with cat
Because cat reads from standard input when no file is given, you can use it to create a file directly from the terminal. The classic approach uses a here-document or input redirection:
cat > shopping.txt
After pressing Enter, type your text. Everything you typed is now saved in shopping.txt. When you are finished, press Ctrl+D to signal end-of-file. Be careful: the single > operator overwrites the file if it already exists, so double-check the filename before you start typing.
If you want to create a file with a fixed block of text in a single command, a here-document is cleaner:
cat > config.txt << EOF
server=localhost
port=8080
EOF
This is handy inside shell scripts where you need to generate configuration files automatically Less friction, more output..
Appending Text to an Existing File
To add content without destroying what is already there, use the double arrow operator >>:
cat >> notes.txt
Type your additional lines and press Ctrl+D. The new text is appended to the end of the file. You can also append the contents of one file to another:
cat extra.txt >> notes.txt
This is a quick way to merge logs or accumulate output over time.
Concatenating Multiple Files
The name concatenate points to the command's original purpose: joining files. To combine several text files into one:
cat part1.txt part2.txt part3.txt > complete.txt
The shell creates complete.txt and fills it with the contents of the three input files in order. This technique is widely used to merge split log files, assemble CSV exports, or stitch together chapters of a document.
You can also concatenate and append in one step:
cat part4.txt >> complete.txt
Useful cat Options You Should Know
The real power of cat shows up in its options. Here are the ones worth memorizing Not complicated — just consistent..
Numbering Lines
cat -n file.txtnumbers every line, including blank ones.cat -b file.txtnumbers only non-blank lines, which is usually what you want when reviewing code or configuration.
Showing Line Endings and Special Characters
cat -E file.txtdisplays a$at the end of each line, revealing trailing spaces and confirming where lines actually break.cat -T file.txtshows tab characters as^I, which helps when debugging indentation in scripts or Makefiles.cat -A file.txtis the combination option: it shows tabs, line endings, and non-printing characters all at once. This is invaluable when a file "looks fine" but a program refuses to parse it because of hidden Windows line endings or stray control characters.
Squeezing Blank Lines
cat -s file.txtcollapses multiple consecutive blank lines into a single blank line, making messy output far easier to read.
Combining Options
Options can be stacked. For example:
cat -ns file.txt
This numbers every line and squeezes repeated blank lines at the same time.
Using cat with Pipes
cat becomes far more useful when combined with other commands through the pipe operator |. A few practical patterns:
- Search inside a file:
cat log.txt | grep "ERROR"prints only the matching lines. (In practice,grep "ERROR" log.txtis more efficient, but the pipe form is a great learning example.) - Count lines:
cat file.txt | wc -ltells you how many lines the file contains. - Sort content:
cat names.txt | sortoutputs the file in alphabetical order. - Send content to another command:
cat script.sh | bashexecutes a script without needing execute permissions.
Piping is where cat earns its reputation as a building block of the Unix philosophy: small tools that do one thing well and cooperate through text streams Small thing, real impact. Nothing fancy..
Common Mistakes and Best Practices
Even a simple command has pitfalls. Keep these in mind:
- Do not use
catto read huge files. The output floods your terminal. Useless file.txtfor paging, orheadandtailto inspect the beginning or end. - Watch out for
>versus>>. A single arrow overwrites; a double arrow appends. Accidentally overwriting a configuration file is a classic beginner error. - Avoid "useless use of cat." Writing
cat file.txt | grep patternworks, butgrep pattern file.txtis shorter and faster. Reservecatfor cases where you genuinely need to concatenate or transform a stream. - Check binary files carefully. Running
caton an image or compiled binary dumps garbage into your terminal and can even corrupt your prompt. Usefilefirst if you are unsure what a file contains. - Use
cat -Awhen debugging. Hidden characters cause more mysterious failures than almost anything else.
Practical Examples to Try
Here is a quick set of exercises that cover the most useful patterns:
cat /etc/hostname— read a short system file.cat -n script.sh— review a script with line numbers.cat -A config.ini— expose hidden tabs and line endings.cat header.txt body.txt footer.txt > report.txt— build a report from parts.cat >> todo.txt— add tasks interactively, then press Ctrl+D.cat access.log | grep "404" | wc -l— count 404 errors in a log.
Run each one on a test file until the behavior feels natural. Muscle memory is what turns command-line knowledge into real productivity.
Frequently Asked Questions
What does cat stand for? It is short for *conc
What does cat stand for? It is short for concatenate, reflecting its original purpose: joining multiple files end-to-end and sending the result to standard output And that's really what it comes down to. That's the whole idea..
Can cat edit files?
No. cat is read-only (or write-only when redirecting). To modify a file in place, use sed, awk, or a proper editor like vim or nano.
Why does cat file1 file2 > file1 destroy data?
The shell opens file1 for writing before cat starts reading it, truncating it to zero bytes. cat then reads an empty file. Never read and write the same file in a single redirection; use a temporary file or sponge from moreutils.
How do I show line endings without -A?
cat -e shows $ at line ends (LF), while cat -t shows ^I for tabs. -A is simply the combination of -v, -e, and -t.
Is there a Windows equivalent?
type in cmd.exe and Get-Content (or cat alias) in PowerShell serve similar roles, though they lack the concatenation and flag flexibility of the Unix cat Still holds up..
Conclusion
cat looks trivial at first glance—just “print a file”—but its design embodies the Unix philosophy: a sharp, single-purpose tool that composes effortlessly with others. That said, mastering its flags (-n, -A, -s), its redirection quirks (>, >>, <<), and its role in pipelines transforms it from a novelty into a daily driver for everything from quick config checks to scripted report generation. The next time you reach for a heavy editor or a one-off Python script to glue text together, pause and ask: Can cat do this? More often than not, the answer is yes.