logoalt Hacker News

PSA about abuse of cat(1) command. Don't abuse cats

23 pointsby scooterboopertoday at 12:03 AM35 commentsview on HN

Comments

jasongitoday at 3:09 AM

The beauty of cat is that streams are the universal interface.

Program A might accept a file as the last positional arg. Program B might accept it as a named arg, where the name/flag could be anything from --input or -f or --file etc.

But a program will read from STDIN, which all good unix programs do, then piping cat into it works every time. I can write the cat foo.txt part before I even know what command I'm piping it into.

show 2 replies
internet2000today at 2:15 AM

> Piping a single file through cat spawns an entire process whose only job is to copy bytes to a program that already knew how to read them.

Chrome probably spawned two processes when I cmd+clicked this into a new tab. It really doesn't matter.

MPSimmonstoday at 3:00 AM

Unless you're executing these commands in a loop over a large number of items, or the item itself is gargantuan, it's almost always harmless.

Personally, when I'm exploring, I build a command line iteratively. Cat the file to see the content, pipe to grep to get the lines I want, sed/awk/cut/etc to finagle from there.

florentoday at 2:25 AM

if this wanton abuse of cat(1) doesn't stop, we're on track to run out of PIDs by 2031! Just because Unix makes it cheap and easy to fork doesn't mean you have to!

(who gives even a single shit, my god)

show 2 replies
peteetoday at 12:41 AM

> Since 1995, occasional awards for UUOC have been given out, usually by Perl luminary Randal L. Schwartz

http://catb.org/jargon/html/U/UUOC.html

Admittedly its taken me a long time to remember that the file is the last argument to grep, when so many other commands its the first. I'd guess common abuse is due to being easier to type cat x | than to dig up the man page

show 3 replies
copperxtoday at 3:01 AM

I'll make a note of it in my AGENTS.md file.

lifthrasiirtoday at 2:20 AM

Don't do this:

  cat file | wc -l            => wc -l < file
  cat file | head -n 5        => head -n 5 file
  cat file | awk '{print $1}' => awk '{print $1}' file
  cat file | sort             => sort file
Do this instead:

  cat file | wc -l            => <file wc -l
  cat file | head -n 5        => <file head -n 5
  cat file | awk '{print $1}' => <file awk '{print $1}'
  cat file | sort             => <file sort
The front-cat abuse is all about the order. The effective solution needs to keep the relative order of arguments.
show 1 reply
antonvstoday at 2:06 AM

Presumably written by someone without much interactive shell experience.

When you're building a pipeline, putting cat first can often be quite convenient. Essentially, it's more composable: it defines the input to the pipeline without committing to a specific tool. For example, you can up-arrow in the shell and change the part after the pipe without having to skip back past the filename.

In fact if you don't start with cat, it's possible you're more of a script kiddie than a software developer.

dminvstoday at 2:17 AM

"Don't be a catgrepper"

- various HostGator employees, c. 2011

jmclnxtoday at 1:57 AM

In this day an age this is still making rounds ? So this is the memory usage of cat on my system:

     VSZ   RSS    SZ CMD
    3252  1608   813 /bin/cat
To me there are far more things to worry about than cat. How about your multi-gig browser for one ?

Now for firefox:

        VSZ    RSS     SZ CMD
    3472212 395968 868053 /usr/lib64/firefox/firefox
Maybe people should be looking at that ? I will not even get into modern Linux Desktops :)
Shadowmisttoday at 1:47 AM

I’m going to keep doing it but wouldn’t mind it if my shell auto replaced it for me.

pyrolisticaltoday at 2:10 AM

I like putting the stdin before the command

< file grep abc

spriortoday at 2:21 AM

I raised eyebrows recently when I was working with someone and we needed to create a file and instead of starting an editor I did: cat > filename ... Ctrl-D

show 1 reply
BugsJustFindMetoday at 1:49 AM

[flagged]

show 1 reply