Linux Tips: The Power Of Aliasing

We all know how tedious using the command line interface can be sometimes, especially when doing particularly complex and repetitive tasks. I have recently come to love the alias command, which allows you to collect one or many commands into one. Here is a simple example of how to use the command:

alias bin=’cd /usr/local/bin’

In this example, I have a aliased a cd command to that specific directory as “bin”. Now, whenever that word is entered into the command line, you will go to that directory. Simple enough, right? Well, you can actually alias any combination of commands you would like. Here is a more complex example:

alias count=’echo $(ls -l | grep ^- | wc -l) files; echo $(ls -l | grep ^d | wc -l) directories’

With this command, when count is entered into the command line, the number of files and directories in the current directory will be displayed. All you have to do to add more commands to the alias is add a colon at the end of the each command (except for the last one). Another cool thing to add is that once you alias a command, you can use its alias anywhere, even in another alias. For example:

alias rm_all=”yes ‘yes’ | rm *”
alias rm_media=’cd /root/mystuff/pictures;rm_all;cd ..; cd music;rm_all;cd; echo done’

Here, I aliased a command to delete all of the files in the current directory as “rm_all”. I then used that in the string of commands that I aliased as rm_media, which moves into two different directories and removes everything in them.

It is important to note that if you enter these alias commands in the command line as is, you will only be able to use that name for the current session. This is ok sometimes, but if you would like a certain command to always be aliased, then you should copy it into your /root/.bashrc file and then restart your session. As you can see, this can be a very powerful and helpful tool that every linux user should take full advantage of.