The Linux terminal may seem rather daunting when you’re accustomed to Windows' menu-based interface, but its utility is nothing to scoff at. Rather than forcing you to navigate through a barrage of options and menus just to fix an error, the CLI nature of the Linux terminal makes troubleshooting a breeze. Likewise, installing applications and packages from the terminal is a lot easier than using executables. Having used Linux for years, I’ve picked up a couple of ways to elevate my CLI experience, and here’s a compilation of my favorite terminal tricks.

AWK

Especially useful while dealing with logs and databases

Browsing log files is something you’ll often do once you get into the nitty-gritty of Linux computing, though there are plenty of fields you may not care about. Perhaps you’re investigating crashes caused by a specific package, but the log file’s never-ending records make this process a challenge.

That’s where the AWK utility comes in handy. Typically used with the awk ‘select-condition {operation}’ file-name syntax, AWK reads every record in a file, compares it with a selection condition, and performs an operation on datasets that match the criteria. Think of it as a way to run queries on your log files and core dumps without siphoning them to a database tool. I tend to leverage awk when examining my container logs, but you can also use it to filter and sort huge datasets directly from the terminal.

Re-run previous commands

As well as arguments

When you’re working in the Linux terminal, you’ll often need to run previously executed commands. Perhaps it’s just the apt update command after adding new repository links to your sources.list file. Or maybe it’s something as intricate as running the fio command to check your network share’s transfer rates after modifying your NAS settings.

Regardless, there are a few ways you can re-run older commands without typing them out. For instance, using !! will bring up the previously executed command, while running !$ will only show the argument associated with it. Then there’s the history keyword, which shows a numbered list containing the last 1000 commands you’ve executed in the terminal. What’s more, you can type ! followed by the number next to a specific command to instantly bring it up in the terminal.

Command chaining

Run multiple commands in one line

From update and upgrade to touch and nano (or vi/emacs), there are tons of Linux commands that are typically run together. That’s where chaining comes into play, as it lets you add a couple of commands to the same terminal line, so you can execute them at once. In fact, there are a couple of chaining operators in Linux.

For instance, && runs the second command only if the first one is free of errors (as in, its error code is 0 after execution), which is why the entire apt update && apt upgrade command fails if the update section encounters an error during execution. Meanwhile, using a semicolon (;) to separate commands ensures that all of them are executed regardless of whether the first one runs into errors. There’s also dual vertical bars (||), where the second command is run only if the first one fails.

Command piping

It’s even cooler than chaining

Although certain chaining operators rely on the execution status of a command to run the next set of instructions, piping creates an entire flow of input-output results between subsequent commands. In piping, each command uses the output from the previous instruction as input, and a single vertical bar (|) is used to create a piping sequence.

Heck, you can even combine a piping sequence with awk syntax and command chains to create complex workflows from a seemingly simple set of keywords.

Caret fixing

With a shout-out to the fc command

So far, every trick I’ve mentioned creates a long set of commands. While compressing multiple snippets of terminal instructions into the same line makes things more efficient, it’s easy to fat-finger the code. The caret operator (^) is pretty useful, as it can replace a typo with the correct keyword in the previous command before running it again. Its syntax is ^typo^correct-expression, but you’ll want to be extra cautious when typing the correction if you’re as prone to making typos as I am.

There’s also the fc command, which opens any previously executed command in an editor, so you can modify it. Once you’ve fixed the error and exited the editor (using Ctrl+X), it’ll immediately run the command, saving you the hassle of pasting it into the terminal and manually executing it.

Command line edit

I use this one all the time

As someone who geeks over anything and everything related to home labs, I use podman run and docker run commands all the time. However, certain templates need to be modified before they can be run. There are containers where I need to allocate local storage volumes, while others require custom user credentials, though editing them in the terminal UI is kind of a pain.

Luckily, pressing Ctrl+X followed by Ctrl+E switches to the default text editor – similar to the fc command I mentioned earlier. Likewise, exiting the editor causes the terminal to execute the code. It’s a neat trick I discovered a few months ago, and have been using it ever since to deploy new containers, though there are plenty of other scenarios where it can come in handy.

The wild west of Linux terminal hides many secrets

Still looking for more tips to up your terminal game? Pressing Ctrl+R will let you reverse search for commands you’ve previously executed, and the Linux terminal uses autocomplete to help you execute older commands instantly (though I prefer using history). If you’re prone to making the same typo repeatedly, you can use the alias keyword (syntax: alias typo=”correct-term”) to force the terminal to autocorrect your mistake.