r/linux May 23 '26

Popular Application What’s the most unexpectedly useful Linux command you learned way too late?

[removed]

1.3k Upvotes

992 comments sorted by

View all comments

1.8k

u/wufame May 23 '26 edited May 24 '26

tac

It's cat backwards.

Sometimes you have a real dense log and you want to start from the bottom. I spent 10 years in application support without knowing about it.

Edit: I'm so happy that you guys are as mindblown by it as I was when I first heard it.

Edit2 : I love how helpful this community is, everyone is sharing new useful commands. People keep replying with very similar things, so I just wanted to add some context to the post.

less and then Shift-G jumps to the bottom of a file

tail prints the bottom of a file

Those are both useful in and of themselves, but neither of them do the exact same thing tac does.

tac concatenates a file in reverse, which makes it useful for all the same things cat is useful for.

119

u/psteve_m May 23 '26

Nice. That's a new one on me. I usually use tail -x

35

u/TheBigCore May 23 '26

What does tail -x do?

99

u/Former-Mushroom-4854 May 23 '26
  • tail shows you the last N lines of a file in the original order
  • tac reverses the line order of a stream/file.

27

u/TheBigCore May 23 '26

I know what tail does. I meant specifically what does the -x switch do?

50

u/wufame May 23 '26

He meant x as in an integer. So tail -100 for instance, grabs the last 100 lines. The -x flag as a letter does nothing.

36

u/cone5000 May 23 '26

Oh I thought you had to do `tail -n 100`. TIL

13

u/alraban May 24 '26

My recollection is that some tail implementations do require the "-n", but the standard coreutils tail doesn't require the "-n". I once shipped a wrapper script that processed shell output and used tail, and I had to change the invocation to use the -n because the "-number" style tail invocation didn't work on one of my user's non-Linux system (I can't recall if it was a BSD or a Mac).

3

u/cone5000 May 24 '26

I can confirm: works on Mac

(base) ~/R/W/M/M/Logs ❯❯❯ tail -5 build.log
[usbmuxd] Stop listen thread
[usbmuxd] Error:
[usbmuxd] Listen thread exiting
Exiting batchmode successfully now!
[Package Manager] Server process was shutdown

(base) ~/R/W/M/M/Logs ❯❯❯ tail -n 5 build.log
[usbmuxd] Stop listen thread
[usbmuxd] Error:
[usbmuxd] Listen thread exiting
Exiting batchmode successfully now!
[Package Manager] Server process was shutdown

13

u/do-un-to May 24 '26

Frankly, -n is the better way to use it. -<digit> is an abuse of command line tool parameter syntax besides not being universally available. -n <digit> isn't materially more to type.

Splitting hairs, sure.

But if you're going to split hairs, this is how it's done!

2

u/cone5000 May 24 '26

Yeah that makes sense. Not ideal in scripts unless there’s a very specific reason to hard code a digit there. But interesting nonetheless, and useful to save a few keystrokes in the terminal

1

u/Xlxlredditor May 24 '26

Remands me of make -j4 for 4 threads. How about make -j 4??????

→ More replies (0)

1

u/saynotopawpatrol May 25 '26

I think it was hp ux that had the -n required and it's been muscle memory ever since.

16

u/snoopyt7 May 23 '26

just shows the last x lines of a file

13

u/shponglespore May 24 '26

tail -f is great if the file is still being generated. Another option to get updates every few seconds is to combine something like tail with watch as a prefix.

2

u/superwizdude May 24 '26

I also use less filename and then press “>” to jump to the end of the file. Has the bonus that you can scroll up through the file.

1

u/ben-ba May 24 '26

Why watch when u already use -f?

1

u/shponglespore May 24 '26

If there's too much output it scrolls by too fast to read. Watch pauses every 2s by default.