Managing and Using Linux Command Aliases

Aliases are very useful when you have a long command which you need to shorten. Being able to manage aliases is a good skill to have when dealing with Linux, as there are several ways in which an alias can be set.

slide 1 of 4

Introduction to Aliases

Linux Aliases are much like aliases are in real-life, it is the exact same principle as using "Bart" to refer to someone whose actual name is "Bartholomew".

In this article I will be going through a few different ways to set your aliases and the best way to use them to make it faster for you to type your commands, think of them as a "shortcut" to your commands. There are a lot of ways to set them depending whether you would like them to permanent or temporary, and you may also want your aliases to be accessed by other users and groups.

slide 2 of 4

Session Aliases

Session aliases are exactly what they say on the tin, they are only used for that session and are good for any script which you may have to refer to a number of times. Setting these is quite simple, this is one I have on my linux system all you need to do is set an alias in that session using the following command:

alias clean='/home/daniel/scripts/cleanup.sh'

This means that until you exit or destroy the bash session, typing clean will now refer to "/home/daniel/scripts/cleanup.sh" clean is a lot easier to type and remember. However once you exit the session the alias will be destroyed; if you would like to destroy it before then use the unalias command.

unalias clean

Or to remove all of the aliases for that session:

unalias -a

In order to make the commands permanent you must edit the .bashrc file as i will show you now.

slide 3 of 4

Editing .bashrc

You can get to .bashrc in two separate ways, one is through the GUI and the other is through the terminal. Since you are wanting to set aliases which can only be used in the terminal i will assume you are quite comfortable with it. I have used gedit as my file editor but you may use whatever you are comfortable with, what you will need to type to get to the configuration file is the following:

gedit ~/.bashrc

Once you have loaded .bashrc into your editor, simply add your alias to the bottom of the file (it would actually be fine anywhere in the file but as a principle it is always best to add additions to a system file to the bottom) an example of an alias would be:

alias c='clear'

Once you have logged out and logged in again typing "c" into the terminal will use the 'clear' command, this is now a permanent alias and will be there no matter how many times you shutdown/restart/logout. It is also probably worth mentioning that aliases are CASE-Sensitive, C is not the same as c.

slide 4 of 4

Making Aliases Globally Available

The above method is okay if you only want one user to be able to use that alias but if you want all users to be able to use an alias you must use this method. There is a short warning which I must give you if you are using this it uses the "root" account, different distributions of Linux have different methods of getting into the root account.

If you are using Ubuntu or a derivative, use the following command to access the root account (you must be in an account permitted to use sudo)

sudo su

Then type in your account password and you now have root access, for all other distributions of Linux you must log into your root account with the password you gave it at the start, see your specific distribution help files if you do not know how to do that.

Once you have root access it is a similar process to the one above except you are now using a different file. I will be using gedit again for this command:

gedit /etc/bash.bashrc

Then add your alias to the bottom (very carefully since you are root user)

The final thing to do is restart the computer to make the file get reloaded into the system with the new aliases. You can do it right there from the terminal if you are root by using: