Ubuntu Server 18.04 – Learning some useful command-line tricks

How to Configure Network Static IP Address on Ubuntu 19.10

Productivity hacks utilizing the shell are one of my favorite things in this world, right up there with music, video games, and Diet Pepsi. There’s nothing like the feeling you get when you discover a useful feature that saves you time or increases your efficiency. There are many things I’ve discovered along the way that I wish I had known earlier on. One of my goals while writing this tutorial is to teach you as many things as I can that took me longer to learn than it should have. In this section, in no particular order, I’ll go over a few tricks that increased my work-flow.

First, entering !! (two exclamation marks) in your Terminal will repeat the command you last used. By itself, this may not seem like much. After all, you can press the up arrow key once and press Enter to recall the previous command and execute it. But, when paired with sudo, !! becomes more interesting. Imagine for a moment that you entered a command that needs root privileges, but you forgot to use sudo. We’ve all made this mistake. In fact, as of the time I’m writing this chapter, I’ve been using Linux for 16 years and I still forget to use sudo from time to time. When we forget sudo, we can type the command all over again. Or, we can just do this:

sudo !!

And just like that, you prefixed the previously used command with sudo without having to retype that command.

Going a bit further into command history, we can also press Ctrl  R on the shell to initiate a search. After pressing these keys, we can start typing a command, and we’ll get a preview of a command that matches what we’re typing, which will be narrowed down further as we type more characters of it. This is one of those things that is hard for me to describe, and screenshots certainly don’t help here, so go ahead and just give it a shot. For example, press Ctrl + R and then start typing sudo apt. The last time you used that command should appear, and you can press Ctrl + R again, and again, and again to see additional examples of commands that you’ve typed in the past that contain those characters. When you get efficient with this, it’s actually faster than the history command, but it takes a bit to get used to this.

Another fun trick is editing a command you’ve previously typed in a text editor. I know this sounds strange, but hear me out. Assume you pressed the up arrow, you have a very long command, and you just want to edit part of it without having to execute the entire thing, perhaps a command like this:

sudo apt update && sudo apt install apache2

Let’s assume you want to install nginx instead of apache2, but the rest of the command is right. If you hold Ctrl and then press X followed by E, which will open the command in a text editor. There, you can change the command. Once you’re done making your changes, the command will execute once you save the file. Admittedly, this is usually only useful when you have a very long command and you need to just change part of it.

Did you notice the two & symbols in the previous command? This is another useful trick, you can actually chain commands together. In the previous example command, we’re telling the shell to execute sudo apt update. Next, we’re telling the shell to then execute sudo apt install apache2. The double ampersand is known as the logical AND operator, where the second command is run if the first was successful. If the first command was successful, the second command will execute right after. Another method to chain commands is this:

sudo apt update; sudo apt install apache2

The difference with the semicolon is that we’re telling the shell to execute the second command regardless of whether the first command was successful. You may then be wondering, what constitutes success on the shell? An obvious answer to this question might be “it’s successful if there are no error messages.” While that’s true, the shell utilizes exit codes to programmatically attribute success or failure. You can see the exit code of a command by typing this:

echo $?

An exit code of 0 means success, anything else is some sort of error. Different programs will attribute different codes to different types of failures, but 0 is always a success. With this command, what we’re actually doing is printing the content of a variable. $? is actually a variable, which in this case only exists to hold an exit code. The echo command itself can be used to print text to the shell, but it’s often used to print the contents of a variable (which we’ll get into in more detail later).

Now, it’s time for my favorite time-consuming trick of them all, command aliases. The concept of an alias is simple: it allows you to create a command that is just another name for another command. This allows you to simplify commands down to just one word or a few letters. Consider this command, for example:

alias install="sudo apt install"

When you enter previous command, you will receive no actual output. But, what happens is now you have a new command available, install. This command isn’t normally available; you just created it with this command.

You can verify that the alias was created successfully by simply running the alias command, which will show you a list of aliases present in the shell. If you create a new alias, you should see it in the output. You’ll also see additional aliases in the output that you did not create. This is because Ubuntu sets up some by default. In fact, even the ls command is an alias!

With this new alias created, any time you execute install on the command-line, you’re instead executing sudo apt install. Now, installing packages becomes simpler:

install tmux

Just like that, you installed tmux. You didn’t have to type sudo apt install tmux, you simplified the first three words in the command into install. In fact, you can simplify it even further:

alias i="sudo apt install"

Now, you can install a package with this:

i tmux

With aliases, you can get very creative. Here are some of my personal favorites:

  • View the top ten CPU consuming processes:
alias cpu10='ps auxf | sort -nr -k 3 | head -10'
  • View the top ten RAM consuming processes:
alias mem10='ps auxf | sort -nr -k 4 | head -10'
  • View all mounted filesystems, and present the information in a clean tabbed layout:
alias lsmount='mount |column -t'
  • Clear the screen by simply typing c:
alias c=clear

What other aliases can you come up with? Think of a command you may use on a regular basis and simplify it.

There’s one issue though, and that is the fact that when you exit your Terminal window, your aliases are wiped out. How do you retain them? That leads me into my next productivity trick, editing your .bashrc file. This file is present in your home directory and is read every time you start a new Terminal session. You can add all of your alias commands there, just add them somewhere in the file (for example, at the end). You will need to include the entire command, beginning with alias and ending with the commands in quotes. If you wanted to steal my example aliases, you would enter the following lines somewhere in your .bashrc file:

alias i='sudo apt install'
alias cpu10='ps auxf | sort -nr -k 3 | head -10'
alias mem10='ps auxf | sort -nr -k 4 | head -10'
alias lsmount='mount |column -t'

There are, of course, additional time saving tricks that we could talk about here, but then again Bash is so complex we can write an entire tutorial about it (and many people have). As we go along in this chapter, I’ll give you even more tips. Here’s a bonus trick:

cd -

That simple command changes your working directory back to the previous directory you were in.

You’re welcome!

Comments are closed.