3 Linux commands you've overlooked for too long (but can memorize in seconds)

Linux ships with hundreds of small utilities, and most people stick to a handful.Turns out, there are tons of forgotten commands that can be either slightly fun or extremely useful.Three of them, tac, shuf, and timeout, solve common problems in a single line and take seconds to learn.

And they might just earn a spot in your repertoire of commands.tac — Read files in reverse Flip any file so the newest lines come first Close The name gives it away.tac is cat spelled backward, and as you might guess, it prints a file's lines in reverse order, starting with the last line and ending with the first.

It ships with GNU coreutils, so practically every mainstream Linux distribution already has it installed.You might be asking yourself: why the hell would I use that? The best use case is reading logs.Most log files append new entries at the bottom, which means the information you care about sits at the end of a file that may be thousands of lines long.

Running tac /var/log/syslog | less puts the most recent entries at the top, so you can start reading immediately instead of scrolling.It also pairs well with other tools.Pipe it into grep with the -m flag, as in tac access.log | grep -m 5 "404", and you get the five most recent matches without combing through the whole file.

Because tac reads from standard input as well as from files, it slots into any pipeline and can reverse the output of another command, such as seq 1 5 | tac.The -s flag changes the separator tac uses to split its input, which helps with data that isn't divided by newlines, while -r treats that separator as a regular expression.That makes it possible to reverse the order of records in simple structured text.

As normal, a few limits apply.tac reverses lines, not characters, so it is not a substitute for rev, which flips the text within each line.It also needs to read the whole input before it can print the first line, so it is a poor fit for endless streams.

If you only need the last handful of lines, tail is quicker.On macOS and BSD systems, tac usually isn't installed, though tail -r offers similar behavior.shuf — Randomize your inputs Pick, mix, and sample lines without writing scripts shuf generates random permutations, which is a technical way of saying it shuffles things.

Hand it a file and it prints the lines in a random order, as in shuf names.txt.Like tac, it belongs to GNU coreutils and reads from standard input, so it works in pipelines as easily as on files.Where it gets useful is sampling.

The -n option limits how many lines come out, so shuf -n 1 quotes.txt picks a single random quote, and shuf -n 10 dataset.csv pulls a ten-line sample from a large file.Anyone who works with data can use that to spot-check a file without opening it in a spreadsheet.The -e option treats command-line arguments as the input, which means you can shuffle a handful of words directly: shuf -e pizza sushi tacos -n 1 settles a dinner debate in one line.

The -i option generates a numeric range instead, so shuf -i 1-100 -n 5 draws five unique numbers between 1 and 100.Adding -r allows repeats, which turns it into a simple dice roller.It has practical scripting uses too.

A shell script can choose a random wallpaper from a folder by piping ls into shuf -n 1, play a playlist in random order, or generate varied test data.When you need to shuffle a file in place, the -o option names the output file.Unlike a plain redirect, it handles the case where input and output are the same file safely.

But as with everything in this list, there's a problem.By default, shuf draws on the system's random source, which is fine for everyday tasks but not a tool for cryptographic purposes such as generating passwords or keys.For repeatable results, --random-source lets you supply a file of bytes so the same shuffle can be reproduced.

timeout — Put a clock on commands Stop runaway commands before they hang your terminal timeout runs a command and ends it if it takes longer than a limit you set.The syntax is simple: the duration comes first, then the command, as in timeout 10 ping example.com.Durations default to seconds, but suffixes work too: m for minutes, h for hours, and d for days.

Why does that matter? As I'm sure you know, some commands hang.A network request waits on an unresponsive server, a script stalls on a locked resource, or a backup job runs far longer than expected.In an interactive terminal, you can press Ctrl+C.

In a cron job or an automated script, nobody is there to do it, and a stuck process can pile up or block everything queued behind it.Wrapping the risky command in timeout keeps the script moving.By default, timeout sends the TERM signal when time runs out, which asks the process to shut down politely.

Some programs ignore it, so the -k option adds a second deadline after which timeout sends KILL, which cannot be ignored.For example, timeout -k 5 30 ./long-task.sh gives the script thirty seconds, then five more to exit cleanly before it is forced to stop.The -s option lets you choose a different signal altogether.

The exit status is just as useful as the cutoff.When a command is stopped for exceeding its limit, timeout exits with status 124.Scripts can check for that value to tell a timeout apart from an ordinary failure, then retry, log a warning, or move on.

Adding --preserve-status makes timeout return the command's own exit status instead.One thing to watch for: by default, timeout runs the command in a background process group, so interactive programs that expect keyboard input may behave oddly.The --foreground option addresses that, and anything that reads from the terminal is worth testing before you rely on it.

Three tiny commands that save time every day Tac, shuf, and timeout each do one narrow job well.Reading logs backward, sampling random lines, and capping runaway processes come up constantly, and all three commands are already installed on most Linux systems, so there is nothing to set up before using them.

Read More
Related Posts