One of the great things about Linux based systems is that you can achieve quite a bit from the command line more quickly than you can from the GUI, so long as you know which command to use. You can even combine groups of commands into a bash script, which you can then run with the following command:
More about that in a minute. We’re here to talk about aliases.
Let’s look at a common command you might run in the terminal: getting a directory listing using the
ls command. Type
ls in your home directory and you’re likely to get something similar to the following output:
That’s helpful, but I usually run this command with a few options to show invisible files, to show files sizes, and to show those sizes in a form that makes sense to humans. So instead of
ls I type in
ls -alh and get this output:
That’s only the top part of the listing. There are a lot of invisible files in your home directory.
It’s not a lot of work to type
ls -alh all the time, but we can make it even less work by creating an alias. See that “.bashrc” file in the above listing? That’s where we’re going to add our aliases. It’s as simple as adding a line to the end of that file that looks like this:
So in the case of our example above, I’m going to create a new alias “kls” to do the work of that command by adding this line:
alias kls="ls -alh"
Now when I’m in my terminal, I can just type
kls to get the same directory listing:
The truth is, I didn’t really save that many keystrokes with that alias. However, using an alias is perfect for longer commands. For example, logging into your webserver on the shell usually involves a command like this:
We can shorten that by adding an alias, such as
alias user="ssh email@example.com"
Now, whenever we want to log in to our webserver on the shell, we can just type in
user instead of the actual command. This will save you time. Since my host is Opalstack, I set this command to the alias “opal”. Typing
opal in my shell gets me to the same point as typing the longer command:
Running Scripts in an Alias
Remember I said earlier that you can create a bash script to run one or more commands at a time? Since I backup to the cloud whenever I change the contents of one the folders in my home directory, I created a bash script called
backup.sh in the root of each one that backs up that directory. So to backup my Documents folder, I would run the following series of commands in the shell:
cd ~/Documents ./backup.sh
But I can add an alias that will run that script from wherever I am by using the
bash command and the
alias kdoc="bash $HOME/Documents/backup.sh
Instead of returning to my Documents folder and running that script, I can just enter
kdoc in the terminal, regardless of where I am.
Activating Your Aliases
After you edit your
.bashrc file, your changes won’t take effect until you reload the
.bashrc file. You can do that in one of two ways. You can simply close your terminal window and relaunch it.
Of, if you’d rather not touch the mouse, just run the following command:
A Note About Alias Names
It goes without saying that you shouldn’t use an existing command for an alias. To test out an alias name, simply type it into the terminal. If it’s not an existing command, you should get something like the following output:
The nice thing about Ubuntu is that if it thinks you’re close to something, it will tell you:
To make them easier for me to remember, I try to use alias names that are meaningful to me. So, for my version of the
ls command, I use
kls as the alias, since my first name starts with k.
To log into my web host, I use
opal because my web host is OpalStack.
Try It Out!
If you have a few long commands that you run all the time, please go ahead and try this out. Of course, be sure to back up your
.bashrc file first.
If you have questions or suggestions, please leave a comment below.