10.1 cat: Concatenating and Printing Files
Let’s talk about cat. The name is a stupid, confusing abbreviation for “concatenate,” which is a fancy way of saying “stick things together.” Its primary job is to read files and output their contents sequentially. But let’s be honest: 99% of the time, you and I use it to just quickly dump the contents of a single file to the terminal. We’re not concatenating a thing. We’re just peeking. It’s the digital equivalent of shouting “Hey, what’s in this box?!” and dumping it on the floor.
It’s brilliantly simple and, because of that, dangerously easy to misuse. I’ve seen more people accidentally cat a massive multi-gigabyte log file and freeze their terminal session than I’ve had hot dinners. Consider this your official warning: cat is for small files. If you need to look at a big one, use less or tail. You’ve been warned. Now, let’s get into it.
How It Works (The Basic Incantation)
You point cat at one or more files, and it reads them from first to last, writing every single byte it finds directly to standard output (which is usually your terminal).
$ cat hello.txt
Hello, world! This is the content of my file.
It has two lines.
That’s it. No secret handshake. It doesn’t care about file extensions or content type. If it’s a text file, you get text. If it’s a binary, you’ll get a horrifying symphony of beeps and nonsense that might mess up your terminal until you run reset. It’s a blunt instrument.
Actually Concatenating Stuff
Remember the name? Here’s where it earns it. You can list multiple files, and cat will read them in order and output them as one continuous stream. This is its real superpower, often used with redirection to create a new file.
$ cat part1.txt part2.txt > complete_story.txt
You can also append files to an existing one. This is a classic way to add a standard header or footer to a file.
$ cat header.txt my_data.txt footer.txt > final_report.txt
Why This Is Both Useful and Terrible
The power of cat is that it doesn’t try to be smart. It’s the simplest possible reader. This makes it perfect for piping (|) into other programs that are smart. You’ll often see it used to feed file content into a text processing tool like grep, sed, or awk.
$ cat server.log | grep "ERROR"
Here’s the secret that will make you look like a pro: this is almost always unnecessary. Those tools can take a filename as an argument directly. This is called a “useless use of cat” (UUOC), and while it’s not a capital offense, it’s an extra process for no reason. Just do this instead:
$ grep "ERROR" server.log
The exception is when you’re combining multiple files before piping, which is a perfectly valid and useful pattern.
$ cat log1.txt log2.txt | grep "ERROR" > all_errors.txt
Common Pitfalls and How to Avoid Them
The Giant File Cat-astrophe: I said it already, but it’s worth repeating.
cat /var/log/syslogmight be fine.cat /var/log/syslog.1might be fine.cat /var/log/syslog*.gzwill probably ruin your afternoon.catdoesn’t preview; it just goes. For large files, useless filenameto page through ortail -n 20 filenameto just see the end.Non-Printable Characters:
catwill faithfully output every byte, including non-printable and control characters. This can make output look weird or break your terminal. If you suspect a file has these, usecat -v(or better yet,less) to make them visible. The-voption tellscatto show non-printing characters visibly (e.g.,^Cfor a control-C).No Line Numbers: Unlike
nlorless -N,catdoesn’t show line numbers. If you need to reference a specific line, you’re out of luck. Pipe it into something that does:cat myfile.txt | nlor just useless -N myfile.txt.
The One Actually Useful Option
While cat is famously minimalist, it does have a couple of flags. Only one is worth remembering:
-nor--number: Number all output lines. This is the quick-and-dirty way to add line numbers before you pipe something elsewhere.
$ cat -n important_list.txt
1 Apples
2 Oranges
3 Backup the server
4 Bananas
So there you have it. cat: the digital equivalent of a spoon. It’s not a scalpel, it’s not a power drill. It’s a simple tool for simple jobs. Use it to quickly look at small configs, to glue files together, or to feed a stream of data to a more sophisticated tool. Just don’t blame me when you try to use it on a file bigger than your ego.