In Linux, a great deal can be accomplished via the Terminal, a powerful, text-based interface to a great number of programs and tools. Beyond simple things, such as package management (via apt-get, dpkg and other utilities), it can interface with ftp programs, be used to launch your other programs such as word processors and web browsers, and convert your music, documents and ebooks to different formats.
Of course, some of the commands used (such as the command to add a drop shadow to an image as described in this article), are quite long, and probably not very easy to remember. In the aforementioned article we showed how to add a command to a user’s .bashrc file so it was easy to call up. In this article we’ll show something similar: we’ll create aliases for common, everyday commands that allow you to call them up with just a few keyboard strokes.
There are numerous commands you may find yourself using all the time, and not all of them are short. With .bashrc aliases, we can “shrink” those commands down to only a few characters. An alias, in effect, tells your computer to create a new command (preferably short and memorable), which the computer will then turn into the longer one. Here’s how it works.
First, open up your Terminal.
Next, type nano .bashrc as shown below:
This will open up the .bashrc file in the Terminal, using the nano text editor, where we will place our aliases. We could replace ‘nano’ in the previous command with ‘gedit’ if we wanted to use a GUI text editor.
Once the file is open, scroll to the bottom.
Now we can start creating aliases. Let’s take the commands mentioned in order that we use them. For updating software, we’ll use ‘update’ as our alias. There is no program on our computer that uses ‘update’ as its command, so this is safe. In the .bashrc file, we would type the following:
alias update=’sudo apt-get update’
Once we’re finished (and have restarted the Terminal so that our updated .bashrc file is used, we can type ‘update’ in the Terminal (without the quotes) and the computer acts as if we typed ‘sudo apt-get update’ instead.
Moving on, we can continue to create aliases, such as:
alias upgrade=’sudo apt-get upgrade’
alias distup=’sudo apt-get dist-upgrade’
alias aptupin=’sudo apt-get update && sudo apt-get install’
With the above added to our previous aliases, we now have four, all of which are very useful for speeding up the software update/upgrade process.
That last command is a favorite, as it first updates the software lists, then allows us to install software. So, instead of typing the following to update our software lists and install firefox (for example):
sudo apt-get update && sudo apt-get install firefox
We would simply type:
It’s that easy!
The one big thing to be concerned with when creating aliases is the possibility of a conflict. Let’s say we were trying to create an alias for a long ffmpeg string that we use to convert mp3 to a different format. It would be tempting to use ‘convert’ as our alias, but if you have Imagemagick installed, ‘convert’ is already a command used, so creating an alias using the same command would cause problems. To avoid this, a good idea is to attempt to use your desired alias before adding it to your .bashrc file. If nothing happens, the alias is available.
Finally, newer versions of the Bash environment allow you to store your aliases in a separate file (as opposed to the bottom of the .bashrc file. This file is called .bash_aliases, and using it is as simple as making sure a couple lines already in your .bashrc file are not commented out. If you see this:
Change it to this:
Now close your .bashrc file and open the .bash_aliases file with this command:
Once again, using Gedit or another GUI text editor is just fine. If the file doesn’t already exist, opening it in this fashion will create it. Once this file is open, simply type your aliases into it and close, saving along the way. Once your Terminal is restarted, your aliases will be found if they are in either location (the .bashrc file or the .bash_aliases file).
Using aliases is a great way to speed up common functions as well as making more difficult commands simpler and easier to use. It only takes a bit of time to set up, but once it’s done, the advantages in speed and efficiency should be obvious.