ahh

~$ which cat

top 57 comments

sorted by: hot top controversial new old
[–] 85 points 3 months ago* (22 children)

cat is misunderstood a lot. The purpose of cat is to combine multiple files together (it's literally short for "concatenate"), like cat *.log to combine all log files together.

If you're just using it for a single file, then you should just redirect the file to stdin. These two command lines behave similarly:

cat foo.txt | some-command
some-command < foo.txt

For head, tail, grep and some other commands, you can just pass in the file name directly as an argument (e.g. grep whatever foo.txt).

  • source
  • hideshow 22 child comments
  • [–] 70 points 3 months ago (4 children)
  • [–] [S] 17 points 3 months ago (3 children)

    awww what's a cat without a head and a tail?

  • source
  • parent
  • hideshow 3 child comments
  • [–] [S] 16 points 3 months ago (4 children)

    yeah sure but the command is literally written for the humor.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 19 points 3 months ago (3 children)

    I know, but I just wanted to mention this since a lot of new Linux users end up using cat this way :)

  • source
  • parent
  • hideshow 3 child comments
  • [–] 10 points 3 months ago (2 children)

    I still like doing cat file.txt | grep thing because I often re-run it to find something else like cat file.txt | grep thing2 - and if I do that with grep, the pattern sits awkwardly in the middle instead of at the end

  • source
  • parent
  • hideshow 2 child comments
  • [–] 11 points 3 months ago (6 children)

    Fair, but in general I find it way more useful to use cat for piping.

    In most cases I'll do something like

    cat file | program
    cat file | program | grep
    cat file | program | grep | cut
    

    This gets more complicated if the file is at the end of the command.

  • source
  • parent
  • hideshow 6 child comments
  • [–] 9 points 3 months ago (4 children)

    It wouldn't be at the end, since you'd still be piping into program

    program <file | grep | cut
    
  • source
  • parent
  • hideshow 4 child comments
  • [–] 26 points 3 months ago (2 children)

    Another all-time favorite:

    $ man woman  
    No manual entry for woman  
    
  • source
  • hideshow 2 child comments
  • load more comments (1 reply)
    [–] 21 points 3 months ago (1 child)

    tac is a cat but faced the other way

  • source
  • hideshow 1 child comment
  • [–] 14 points 3 months ago (2 children)

    I've been using a slightly customized bash-cat for years now. Still makes me smile when I see it

  • source
  • hideshow 2 child comments
  • [–] 13 points 3 months ago (3 children)
  • [–] 4 points 3 months ago (2 children)

    I've always wondered what a good use case for tac would be...

  • source
  • parent
  • hideshow 2 child comments
  • [–] 4 points 3 months ago (1 child)

    Reverse chronological order, mostly

    tac log.txt |head # would give you newest first

    no good for sort because sort already supports -r

    no need it for tail on large files because tail already skips to the end.

    I do a lot of impromptu bash to sus crap out of logs.

    when you're balls deep in

    cat log|grep -v foo| rev|cut -f 1-3 -d \ | |rev

    Sometimes tac comes in clutch.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 11 points 3 months ago (1 child)
  • [–] 10 points 3 months ago (2 children)

    Bat is my favorite command to date.

  • source
  • hideshow 2 child comments
  • [–] 10 points 3 months ago

    I was today's years old to finally notice the cat-head-tail thing...

    But it also just recently dawned upon me why pacman has those weird "c" as progress indicators...

    I'm not a smart man...

  • source
  • [–] 10 points 3 months ago (4 children)

    I always thought it's

    head | cat | tail
    

    It, of course, waits for an input, which brings up the one and only question...

    Have you fed your cat?

  • source
  • hideshow 4 child comments
  • [+] 1 point 3 months ago (2 children)
  • [–] 2 points 3 months ago

    it won't... do anything

    it'll wait for you to type ten lines (the default for head without arguments), buffer them (bc tail must buffer its input), then spit them back at you, and exit

    • head takes the first N lines of its input (if -n <number> is not specified, the default is 10 lines, and default reading from stdin, which in this case is your terminal) and prints them to its stdout (which thanks to the pipe, goes directly to cat's stdin), then signals end-of-file.
    • cat accepts any number of filenames and conCATenates them (hence the name), writing their contents one after the other to stdout. with only one file as argument (the most common case in practice) it will simply write that file's contents to stdout. with no arguments (as in this case) it will simply copy everything from its stdin (in this case head's stdout) to its stdout (in this case tail's stdin). the cat command could be completely omitted from this invocation (leaving simply head | tail) and it would work exactly the same.
    • tail reads its entire input (like head, it defaults to reading from stdin unless a filename is specified) and buffers it, then, when the end of the file is reached, it prints the last N lines is read (again defaulting to 10 unless -n <number> is specified) to stdout (in this case your terminal).

    put all together, head will wait for you to type a line, then will pass it on to cat, which will then immediately pass it on to tail, which will add it to a buffer and wait for another line. rinse and repeat until you have typed 10 lines, and then head will exit and the pipe between it and cat will close. cat, noticing the closed pipe, will exit in turn, causing the pipe between it and tail to close. tail, seeing an end of file signal on its input pipe, will cough up the last 10 lines in its buffer and then exit. since head only sent it 10 lines, this is the entirety of its input. so it will spit back everything you typed.

    If you press Ctrl+D to send head an end-of-file signal before you have typed 10 lines, head will exit immediately, causing cat to exit, causing tail to output everything it had in its buffer so far.

  • source
  • parent
  • [–] 7 points 3 months ago* (5 children)
  • [–] 11 points 3 months ago (4 children)
  • [–] 5 points 3 months ago (3 children)
  • [–] 7 points 3 months ago (3 children)

    For the past 30 years or so, my brain always tells me to type "last" instead of "tail". This usually results in some kind of "Permission denied" message that confuses me for a few seconds.

  • source
  • hideshow 3 child comments
  • [–] [S] 1 point 3 months ago (2 children)

    there's a last command? just knowing that.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 4 points 3 months ago (1 child)

    Yes, but it shows information about recent/current logins and requires access a file called wtmp (or something like that, off the top of my head), and that requires privileged access.

    My usual use case where I mix it up is looking at a log file and and I just want to see the last few entries. So my brain 🧠 tells my fingers to type "last logfile", and when I get the permission message it tells my mouth to go, "Doh!".

  • source
  • parent
  • hideshow 1 child comment
  • [–] 5 points 3 months ago (4 children)
  • [–] 1 point 3 months ago (3 children)

    Be careful now, 2000/2010 Linux terminology can be taken as an insult in 2026. /s

    I’m tired of this stupid coddling bullshit with the influx of new Linux users. If you can’t read don’t use Linux. Simple. 😂

  • source
  • parent
  • hideshow 3 child comments