The linux cat command is one of those tools you use every single day without thinking about it. Then one day you realize you only know 20% of what it does. Here’s the other 80%.
What Is the Linux cat Command?
cat stands for concatenate. Its original purpose was to join files together, but it’s become the go-to command for quickly viewing file contents. It reads one or more files and writes them to standard output, which you can view in the terminal or redirect elsewhere.
Where cat Fits in Daily Linux Work
You’ll reach for cat when you need to quickly read a config file, check a log snippet, or verify a file was written correctly. For longer files, less or more are better. cat is for short files where you want the whole thing at once without paging.
cat Command Syntax
cat [OPTIONS] [FILENAME...]Without a filename, cat reads from standard input until you press Ctrl+D. With one or more filenames, it reads and prints them in order.
How to Use the cat Command
Display File Contents
The most common use case:
cat /etc/hostnamecat /var/log/syslogFor multiple files, cat displays them sequentially in the order provided.
Concatenate Multiple Files
This is what cat was actually designed for. Combine files into one:
cat file1.txt file2.txt > combined.txtThe > redirects the combined output to a new file. Without it, the combined content prints to the terminal.
Add Line Numbers with -n
The -n flag numbers every output line, which is useful when referencing specific lines in config files or scripts:
cat -n /etc/nginx/nginx.confThe -b flag numbers only non-blank lines if you want to skip counting empty ones.
Suppress Blank Lines with -s
The -s flag squeezes multiple consecutive blank lines into a single blank line. Useful with verbose config files or log outputs that have excessive whitespace:
cat -s verbose_config.txtShow Non-Printing Characters with -A
The -A flag shows tabs as ^I and line endings as $ characters. This is invaluable when debugging Windows-formatted files uploaded to a Linux server, where hidden carriage returns (^M) break scripts:
cat -A script.shIf you see ^M at the end of every line, the file has Windows line endings. Fix it with dos2unix.
Create and Append to Files with cat
Create a New File
Use cat with redirection and a heredoc to create a file interactively:
cat > newfile.txtType your content, then press Ctrl+D to save and exit. The file gets created with whatever you typed.
Append to an Existing File
Use >> instead of > to append without overwriting:
cat >> existing.txtType new content, Ctrl+D to finish. This adds to the end of the file.
cat with Pipes and Redirects
cat works well as the start of a pipeline when you want to feed a file into another command:
cat access.log | grep "404" | wc -lThat said, many commands accept filenames directly (grep “404” access.log), making the cat unnecessary. It’s a style choice for readability versus efficiency.
When NOT to Use cat: Common Misuse
The ‘useless use of cat’ is a real thing in Linux scripting circles. Piping cat into commands that can read files directly adds a pointless process fork:
- cat file.txt | grep pattern → use grep pattern file.txt instead
- cat file.txt | head -20 → use head -20 file.txt instead
- cat file.txt | wc -l → use wc -l file.txt instead
These aren’t wrong, but they’re unnecessary overhead. On large files or in tight loops, it adds up.
FAQ: Linux cat Command
cat reads one or more files and writes their contents to standard output. You can view files in the terminal, concatenate multiple files, or redirect output to new files.
Use cat -n filename. This numbers every line in the output. Use -b instead to number only non-blank lines.
Run: cat file1.txt file2.txt > combined.txt. This merges both files into combined.txt in the order specified.
cat dumps the entire file at once. less and more paginate the output so you can scroll through it. Use cat for short files, less for long ones.