I once spent 45 minutes staring at a shell script that refused to run. No syntax errors. No missing dependencies. Just silent failure. When I finally ran cat -A on the config file, there they were: ^M$ characters at the end of every line. Someone had edited it on Windows, and those invisible carriage returns broke everything. That moment taught me more about the cat command in Linux than any tutorial ever did.
The cat command is one of those tools you learn on day one and keep using for years. But most people only scratch the surface. In this guide, I’ll walk you through everything cat can do, from reading files to creating them, plus a few tricks that separate casual users from seasoned admins.
What Is the cat Command (And Why Every Admin Uses It)
The name cat is short for concatenate. It was built into the original Unix system by Ken Thompson and Dennis Ritchie. You can read the full cat command history on Wikipedia if you’re curious about its roots.
Despite its name, most people use cat to display file contents. That’s perfectly fine. But it was actually designed to join multiple files together. Understanding this helps you unlock its full potential.
Basic Syntax
The syntax is straightforward:
Get a VPS from as low as $11/year! WOW!
cat [OPTIONS] [FILE...]
You can check every available flag in the official Linux man page for cat. But don’t worry. I’ll cover the ones that actually matter below.
Three Core Jobs cat Does
- Read files: Display the contents of one or more files in your terminal
- Concatenate files: Merge multiple files into a single output or new file
- Create files: Write new files directly from the command line without opening an editor
Most beginners only know about the first one. By the end of this article, you’ll be comfortable with all three.
Viewing File Contents with cat
This is where everyone starts. You have a file. You want to see what’s inside. Cat dumps it to your terminal. Simple.
Display a Single File
cat /etc/hostname
That’s it. The file contents appear in your terminal. Classic sysadmin move: cat /etc/passwd to quickly check user accounts on a system.
View Multiple Files at Once
cat file1.txt file2.txt file3.txt
Cat prints each file in sequence. No separators, no labels. The output flows together as one continuous stream. This is concatenation in action.
Show Line Numbers with -n
cat -n /etc/ssh/sshd_config
The -n flag adds line numbers to every line. I use this constantly when debugging config files. When someone says “check line 47,” you need those numbers visible.
Squeeze Blank Lines with -s
cat -s logfile.txt
Some files have multiple blank lines in a row. The -s flag collapses them down to a single blank line. It cleans up the output without modifying the original file.
Quick Tip
Avoid using cat on large log files. It dumps the entire file to your terminal at once, which can freeze your session. Use the tail command or less instead for large files.
Concatenating and Merging Files
This is what cat was actually built for. Joining files together. If you understand output redirection in Linux, this section will feel natural.
Merge Files Into One with >
cat part1.txt part2.txt part3.txt > complete.txt
This reads all three files in order and writes the combined output into complete.txt. I’ve used this to merge split log files, combine config snippets, and reassemble files that were split with the split command.
Append to a File with >>
cat new-entries.txt >> master-log.txt
The double arrow >> appends to the end of the file instead of overwriting it.
Warning
Using > overwrites the target file completely. Using >> appends. Mixing these up is a mistake that hurts once and teaches forever. I learned this the hard way during my first sysadmin job when I accidentally overwrote a production log. Always double-check your arrows.
Creating Files with cat
You don’t always need nano, vim, or a full text editor to create a file. Cat handles quick file creation right from the terminal.
Quick File Creation from the Terminal
cat > notes.txt
This is line one.
This is line two.
Press CTRL+D when done.
After typing cat > notes.txt, everything you type goes into the file. Press CTRL+D on a new line to save and exit. Want to add more lines later? Use cat >> notes.txt to append.
Using Heredoc with cat for Multi-Line Files
This is where cat gets seriously powerful, especially if you’re into writing Bash scripts.
cat < config.yaml
database:
host: localhost
port: 5432
name: myapp_production
EOF
The heredoc syntax lets you create multi-line files inline. Everything between << EOF and EOF goes into the file. You’ll see this pattern everywhere in Dockerfiles, CI/CD pipelines, and Ansible playbooks.
There’s also the <<- variant that strips leading tabs. This keeps your scripts clean when the heredoc is nested inside a function or loop:
if [ "$ENV" = "production" ]; then
cat < /etc/app.conf
server_name=prod.example.com
debug=false
EOF
fi
Useful cat Flags You Should Know
Beyond -n and -s, cat has several flags that come in handy for debugging and troubleshooting.
Show Non-Printing Characters (-A, -v, -e, -t)
This is the sysadmin power move I mentioned at the start. The -A flag is equivalent to -vET combined. It reveals invisible characters that silently break your configs.
cat -A config.conf
Here’s what each sub-flag does:
- -v: Shows non-printing characters. Great for debugging encoding issues
- -e: Displays
$at the end of each line. Instantly reveals Windows-style CRLF endings as^M$ - -t: Shows TAB characters as
^I. Useful when tabs vs. spaces matters (YAML files, Makefiles) - -A: All three combined. This is the one I reach for first when something “looks right but doesn’t work”
If you’ve ever dealt with Linux file permissions issues or mysterious config failures, cat -A should be in your toolkit.
Number Only Non-Blank Lines (-b)
cat -b script.sh
Similar to -n, but it skips blank lines when numbering. This gives you cleaner output when working with files that have lots of spacing.
The “Useless Use of Cat” Anti-Pattern
Here’s something most beginner tutorials won’t tell you. There’s a named anti-pattern in the Linux community called the “Useless Use of Cat,” or UUOC. It’s been around since 1995, originally coined by Randal L. Schwartz.
What Is UUOC and Why It Matters
UUOC happens when you pipe cat into another command that can read files directly. According to The Jargon File entry on Useless Use of Cat, it’s a construct that “provides only a function of convenience to the user” while spawning an unnecessary process.
“Useless use of cat is a common Unix jargon for command line constructs that provide only a function of convenience to the user… pipelines that include a call to cat that is unnecessary for solving the problem.”
How to Replace Unnecessary cat Pipes
Here are the most common offenders:
cat file | grep 'pattern'→ Just usegrep 'pattern' file(see our grep command in Linux guide)cat file | sort→ Just usesort file(learn more about the sort command)cat file | wc -l→ Just usewc -l filecat file | awk '{print $1}'→ Just useawk '{print $1}' file(see our awk command guide)
Each unnecessary cat spawns an extra process and adds overhead. On a single command, the difference is negligible. But in scripts that process thousands of files, it adds up.
That said, UUOC isn’t always wrong. Sometimes cat file | command is more readable than command < file. Readability counts too. I personally use cat in pipelines when I’m building a chain of commands interactively and want the data flow to read left to right. Just know the tradeoff.
When Not to Use cat (And What to Use Instead)
Cat is great, but it’s not the right tool for every job. Here’s when to reach for something else:
- Large files: Use
lessinstead. It paginates the output so your terminal doesn’t choke. Cat dumps everything at once - Last N lines only: Use the tail command. Running
tail -n 20 logfile.txtis far more efficient than cat-ing the whole thing - First N lines only: Use the head command. Same idea:
head -n 20 file.txt - Searching within a file: Use grep directly rather than piping from cat
- In-place editing: Use sed for in-place file editing. Cat reads files but never modifies them
- Binary files: Use
xxdorhexdump. Running cat on a binary produces garbled output and can mess up your terminal - Comparing files: Use the diff command to see differences between two files
Knowing when not to use a tool is just as important as knowing how to use it. That’s a lesson my early sysadmin days drilled into me.
Modern Alternative: bat
If you spend a lot of time reading files in the terminal, you owe it to yourself to try bat. It’s a cat replacement written in Rust that adds features I didn’t know I needed until I tried them.
What bat Adds Over cat
- Syntax highlighting: Automatic color coding for 100+ programming languages
- Line numbers: Shown by default without needing a flag
- Git integration: Shows modified lines in the gutter with diff markers
- Auto-paging: Pipes long output through
lessautomatically
With over 47,000 stars on bat on GitHub, it’s one of the most popular modern CLI tools in the Linux community. I started using it about two years ago, and now my muscle memory defaults to bat for reading files.
Installing bat on Your System
# Debian/Ubuntu
sudo apt install bat
# Arch Linux
sudo pacman -S bat
# Fedora
sudo dnf install bat
On some Debian-based systems, the binary is installed as batcat due to a naming conflict. You can alias it:
alias bat='batcat'
Some people go further and alias cat to bat. Fair warning: this can break scripts that expect standard cat output. If you do it, use alias cat='bat --paging=never --plain' to keep behavior closer to the original.
If you use a terminal multiplexer like tmux, bat’s syntax highlighting really shines when you’re reading code across split panes.
cat Command Quick Reference
Here’s a table you can bookmark for quick lookups:
| Command / Flag | What It Does |
|---|---|
cat file.txt |
Display file contents |
cat -n file.txt |
Show with line numbers |
cat -b file.txt |
Number non-blank lines only |
cat -s file.txt |
Squeeze repeated blank lines |
cat -A file.txt |
Show all non-printing characters |
cat -v file.txt |
Show non-printing characters |
cat -e file.txt |
Show $ at end of lines |
cat -t file.txt |
Show tabs as ^I |
cat f1 f2 > out |
Merge files into one |
cat f1 >> f2 |
Append f1 to end of f2 |
cat > new.txt |
Create file from terminal input |
cat << EOF > file |
Create file using heredoc |
Start Using cat Like an Admin
The cat command is deceptively simple. On the surface, it just prints files. But once you understand concatenation, heredocs, the -A debugging trick, and when to reach for alternatives, you’re working at a different level.
My recommendation? Open a terminal right now and try cat -A on one of your config files. You might be surprised at what invisible characters are hiding in there. And if you want to take your piping skills further, pair cat with xargs for powerful batch operations.
If you found this useful, check out our guides on the grep command and tail command to keep building your command line toolkit. The terminal is where Linux really opens up, and every command you master makes the next one easier to learn.




