Hacker News new | ask | show | jobs
by benatkin 1190 days ago
What's natural about cat on only one file?

> Briefly, here's the collected wisdom on using cat:

> The purpose of cat is to concatenate (or "catenate") files. If it's only one file, concatenating it with nothing at all is a waste of time, and costs you a process. The fact that the same thread ("but but but, I think it's cleaner / nicer / not that much of a waste / my privelege to waste processes!") springs up virtually every time the Award is posted is also Ancient Usenet Tradition.

https://porkmail.org/era/unix/award

4 comments

> What's natural about cat on only one file?

It's about building the query, really. Here is example:

    cat file.log
"Oh, it's too big, I only care about these messages"

    cat file.log |grep error_name
"don't care about old stuff, show me the new one"

    cat file.log |grep error_name | tail
"hmm, show me when it started, and let's ignore monitoring checks"

    cat file.log |grep error_name | grep -v 'user: mon'| head
"okay, but maybe log was rotated ? Let's see last week"

    zcat file.log.1 |grep error_name | grep -v 'user: mon'| head
"hmm, it's some old error, let's search some more logs"

    zcat file.log.{10..1} |grep error_name | grep -v 'user: mon'| head
(zcat will auto add .gz if not passed)

You can't for example do

    < file.log.{1..10} zcat
That's a lot of extra processes!

There's also zgrep: https://man.openbsd.org/OpenBSD-5.9/grep.1

And if you're in a situation where you care about the overhead of extra processes, you wouldn't be exploratively and iteratively constructing a query on the command-line - you'd have already done the exploration in order to understand exactly what query you needed, and then would be converting the query into a non-bash programming language for optimization.
> What's natural about cat on only one file?

What's natural about using tar to create .tar.gz files instead of using them to archive files to tape?

"cat is to catenate" sounds like an argument from people that need to get out and smell the flowers once in a while

From 'man cat'

> cat – concatenate and print files

So if I want to print a file to stdout, I'll use cat, thanks.

Of course it's reasonable to use cat on one file. Why should it work for all 𝑛≥2 files, but suddenly fail at 𝑛=1?

Despite the dismissive tone of the article you linked, if the overhead of one single process was remotely relevant, shell wouldn't be a good fit.

Yes, it should and does work for 1. That doesn't mean it's a good use of cat.
Catenating a file with the output stream is quite useful in many circumstances. Paged data isn't always wanted, and it's not generally known if filters are needed before the file is seen. Cat is the standard way to print to stdout.