Linux Cat

2 months ago by SHΛRK to c/programmer_humor

ahh

~$ which cat
dan 85 points 2 months ago

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).

path: 0 25099327, hotness: undefined, score: 85, children: 31
pedz 75 points 2 months ago path: 0 25099327 25099748, hotness: undefined, score: 75, children: 4
shark_byte 18 points 2 months ago

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

path: 0 25099327 25099748 25099791, hotness: undefined, score: 18, children: 3
ranzispa 11 points 2 months ago

I guess depending on your interpretation of without it could either be "cat" or "a".

path: 0 25099327 25099748 25099791 25103664, hotness: undefined, score: 11, children: 0
MonkderVierte 9 points 2 months ago

A kernel?

path: 0 25099327 25099748 25099791 25099817, hotness: undefined, score: 9, children: 1
shark_byte 7 points 2 months ago

uhmm no

path: 0 25099327 25099748 25099791 25099817 25099831, hotness: undefined, score: 7, children: 0
lokalhorst 17 points 2 months ago

If I just want to look at the file, is there a better way than cat foo.txt? I guess I can't just do < foo.txt

path: 0 25099327 25099948, hotness: undefined, score: 17, children: 7
mote 22 points 2 months ago

So close! you have to do cat < foo.txt :-) (/s)

path: 0 25099327 25099948 25099999, hotness: undefined, score: 22, children: 0
dan 13 points 2 months ago

Admittedly I still use cat for this.

This also works too, but it's more verbose:

echo "$(<foo.txt)"

It's mentioned in the Bash man pages:

The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

EDIT: I was just informed that simply <foo.txt works too.

path: 0 25099327 25099948 25104087, hotness: undefined, score: 13, children: 3
not_IO 6 points 2 months ago path: 0 25099327 25099948 25104087 25110048, hotness: undefined, score: 6, children: 2
dan 6 points 2 months ago

OK, TIL you can just do <foo.txt. I didn't think that worked. Thanks!

path: 0 25099327 25099948 25104087 25110048 25110967, hotness: undefined, score: 6, children: 1
TwilightKiddy 4 points 2 months ago

You can use less. There are also specialized ones for that purpose like bat.

path: 0 25099327 25099948 25108489, hotness: undefined, score: 4, children: 0
MonkeMischief 1 point 2 months ago

If it's really long, I use less , because it breaks it up into navigable pages instead of filling your entire buffer. :)

Never knew about just using < though! Cool!

path: 0 25099327 25099948 25132394, hotness: undefined, score: 1, children: 0
qqq 15 points 2 months ago

You'll see cat FILE | all the time because it's just a natural start to a pipeline. You cat FILE to see the content unfiltered before filtering it with further commands.

path: 0 25099327 25102155, hotness: undefined, score: 15, children: 1
BasedMaquisEddington 17 points 2 months ago

it’s just a natural start to a pipeline

path: 0 25099327 25102155 25104869, hotness: undefined, score: 17, children: 0
shark_byte 15 points 2 months ago

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

path: 0 25099327 25099394, hotness: undefined, score: 15, children: 5
dan 18 points 2 months ago

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

path: 0 25099327 25099394 25099862, hotness: undefined, score: 18, children: 4
Grail 4 points 2 months ago

Is there any downside?

path: 0 25099327 25099394 25099862 25105031, hotness: undefined, score: 4, children: 1
sqw 2 points 2 months ago
path: 0 25099327 25099394 25099862 25105031 25114201, hotness: undefined, score: 2, children: 0
shark_byte 4 points 2 months ago

ohhh but it's all humor that's the point of the post.

I mean as a new Linux user, I won't take anything from a humor group seriously.

path: 0 25099327 25099394 25099862 25099888, hotness: undefined, score: 4, children: 1
daychilde 2 points 2 months ago

I would.

I'd be careful of jokes, sure, but geeks often can't help have real discussions. Like in this very thread

path: 0 25099327 25099394 25099862 25099888 25112435, hotness: undefined, score: 2, children: 0
ranzispa 11 points 2 months ago

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.

path: 0 25099327 25103612, hotness: undefined, score: 11, children: 6
dan 8 points 2 months ago

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

program <file | grep | cut
path: 0 25099327 25103612 25104016, hotness: undefined, score: 8, children: 4
jxk 14 points 2 months ago

And <file program | grep | cut works too

path: 0 25099327 25103612 25104016 25104100, hotness: undefined, score: 14, children: 3
ranzispa 3 points 2 months ago

Oh, thanks! Didn't know that

path: 0 25099327 25103612 25104016 25104100 25104981, hotness: undefined, score: 3, children: 0
dan 1 point 2 months ago

Thanks - I didn't know this either!

path: 0 25099327 25103612 25104016 25104100 25107683, hotness: undefined, score: 1, children: 0
Digit 1 point 2 months ago

These <file things must be for different shells than the ones I use.

path: 0 25099327 25103612 25104016 25104100 25177484, hotness: undefined, score: 1, children: 0
a_non_monotonic_function 1 point 2 months ago

I don't know why anybody would disagree with this. It's a very clean way of putting it.

path: 0 25099327 25103612 25131710, hotness: undefined, score: 1, children: 0
eager_eagle 10 points 2 months ago

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

path: 0 25099327 25102897, hotness: undefined, score: 10, children: 2
tgt 9 points 2 months ago

< file.txt grep thing also works.

path: 0 25099327 25102897 25103316, hotness: undefined, score: 9, children: 1
sqw 2 points 2 months ago

is it more obtuse

path: 0 25099327 25102897 25103316 25114237, hotness: undefined, score: 2, children: 0
A_norny_mousse 29 points 2 months ago

Another all-time favorite:

$ man woman  
No manual entry for woman  
path: 0 25100560, hotness: undefined, score: 29, children: 2
TwilightKiddy 20 points 2 months ago
$ touch woman
touch: cannot touch 'woman': Permission denied
path: 0 25100560 25105781, hotness: undefined, score: 20, children: 0
Digit 1 point 2 months ago

Huh. ... I could have swore there was a woman. I even package searched, even in overlays... no woman.

digit 20260807 tyson gentoo /home/digit % whereis woman
woman:
digit 20260807 tyson gentoo /home/digit % which woman
which: no woman in (/bedrock/cross/pin/bin:/bedrock/bin:/usr/local/bin:/usr/local/sbin:/opt/bin:/opt/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/snap/bin:/home/digit/bin:/usr/games/bin:/usr/games:/bedrock/strata/devuan/usr/libexec/xscreensaver:/home/digit/.cargo/bin:/bedrock/cross/bin)
digit 20260807 tyson gentoo /home/digit % apropos woman
woman: nothing appropriate.

digit 20260807 tyson gentoo /home/digit % man cry
No manual entry for cry
digit 20260807 tyson gentoo /home/digit % whereis cry
cry:

No woman.

No cry.

path: 0 25100560 25177621, hotness: undefined, score: 1, children: 0
red_tomato 22 points 2 months ago

tac is a cat but faced the other way

path: 0 25099559, hotness: undefined, score: 22, children: 1
shark_byte 12 points 2 months ago

ohhh let me check it out lol

path: 0 25099559 25099599, hotness: undefined, score: 12, children: 0
exu 15 points 2 months ago

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

path: 0 25101481, hotness: undefined, score: 15, children: 2
shark_byte 5 points 2 months ago

yeah I just checked it out it's cute

path: 0 25101481 25101686, hotness: undefined, score: 5, children: 0
A_norny_mousse 2 points 2 months ago

last commit message: "Shorter Cat with added Tail." 👍

path: 0 25101481 25107549, hotness: undefined, score: 2, children: 0
anotherspinelessdem 12 points 2 months ago
man which
path: 0 25100621, hotness: undefined, score: 12, children: 1
darth_grunkus 14 points 2 months ago

path: 0 25100621 25104260, hotness: undefined, score: 14, children: 0
rumba 11 points 2 months ago

tac

path: 0 25108337, hotness: undefined, score: 11, children: 3
Sprocketfree 4 points 2 months ago

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

path: 0 25108337 25109396, hotness: undefined, score: 4, children: 2
rumba 4 points 2 months ago

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.

path: 0 25108337 25109396 25109938, hotness: undefined, score: 4, children: 1
Sprocketfree 4 points 2 months ago

That's fair. I'd usually just use less and drop to the end. My use cases would always need the older log before a newer log though because that's the start of the problem. At least that's how I operate.

path: 0 25108337 25109396 25109938 25112076, hotness: undefined, score: 4, children: 0
Jankatarch 10 points 2 months ago

Bat is my favorite command to date.

path: 0 25112485, hotness: undefined, score: 10, children: 2
shark_byte 8 points 2 months ago
~$ which bat
~$ bat man
path: 0 25112485 25113510, hotness: undefined, score: 8, children: 1
Digit 1 point 2 months ago

man bat | bat

path: 0 25112485 25113510 25177780, hotness: undefined, score: 1, children: 0
Dyskolos 10 points 2 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...

path: 0 25117536, hotness: undefined, score: 10, children: 0
TwilightKiddy 9 points 2 months ago

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?

path: 0 25105822, hotness: undefined, score: 9, children: 4
shark_byte 6 points 2 months ago

of course look meow meow so loud

path: 0 25105822 25107565, hotness: undefined, score: 6, children: 0
Digit 1 point 2 months ago

I'm scared to run that.

Is that like some kind of fork bomb?

Or is it just a tubular cat?

path: 0 25105822 25177811, hotness: undefined, score: 1, children: 2
TwilightKiddy 3 points 2 months ago

Put suspicious cats into sandboxes until it's proven that they are crewmates.

path: 0 25105822 25177811 25178435, hotness: undefined, score: 3, children: 0
AVincentInSpace 2 points 2 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.

path: 0 25105822 25177811 25214591, hotness: undefined, score: 2, children: 0
Avicenna 7 points 2 months ago

whereis cat?

path: 0 25103849, hotness: undefined, score: 7, children: 5
LedgeDrop 11 points 2 months ago

which cat

path: 0 25103849 25103945, hotness: undefined, score: 11, children: 4
Avicenna 6 points 2 months ago

whatis cat?

path: 0 25103849 25103945 25104927, hotness: undefined, score: 6, children: 3
cypherpunks 9 points 2 months ago

man cat

path: 0 25103849 25103945 25104927 25105004, hotness: undefined, score: 9, children: 2
Avicenna 5 points 2 months ago

locate cat

path: 0 25103849 25103945 25104927 25105004 25107505, hotness: undefined, score: 5, children: 1
HamsterRage 7 points 2 months ago

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.

path: 0 25118101, hotness: undefined, score: 7, children: 3
shark_byte 1 point 2 months ago

there's a last command? just knowing that.

path: 0 25118101 25118958, hotness: undefined, score: 1, children: 2
HamsterRage 4 points 2 months ago

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!".

path: 0 25118101 25118958 25122618, hotness: undefined, score: 4, children: 1
shark_byte 2 points 2 months ago

yeah I looked it up

this will be handy

~$ last reboot
~$ last -s -7days
path: 0 25118101 25118958 25122618 25123282, hotness: undefined, score: 2, children: 0
reader 5 points 2 months ago

rtfm

path: 0 25103323, hotness: undefined, score: 5, children: 4
selarian 1 point 2 months ago

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. 😂

path: 0 25103323 25105182, hotness: undefined, score: 1, children: 3
piccolo 2 points 2 months ago

But wheres the manual??

path: 0 25103323 25105182 25110116, hotness: undefined, score: 2, children: 2
MonkeMischief 1 point 2 months ago

In order of approachability:

Man pages.

Also Arch Wiki.

Also excellent books by NoStarch Press.

... Or did I miss a joke? Hehe

path: 0 25103323 25105182 25110116 25132442, hotness: undefined, score: 1, children: 1
Digit 1 point 2 months ago

Missing the joke is always worthy as it's own joke.

path: 0 25103323 25105182 25110116 25132442 25180532, hotness: undefined, score: 1, children: 0
programmer_humor
programmer_humor

@programming.dev

login for more options
33631
2562
7551

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics
    • If the mod doesn't find it funny, you're banned. Ha-ha!... For real: do not use the community for "statements". There are other places for such content. Keep it chill and funny.

go to feed...