Sunday 21 September 2008

Tip of the Day: Create Aliases

Aliases are user-defined commands which allow a person to execute several commands by only typing one. For example, to update and upgrade your Ubuntu system you would usually issue this:

sudo apt-get update
sudo apt-get upgrade

Or:

sudo apt-get update && sudo apt-get upgrade

The second command, sudo apt-get upgrade, is executed only if the first one was successful. In order to make this even shorter, you can create an alias and use it instead of the entire command you want executed. One of the ways to do it is add your aliases in the ~/.bashrc file, where ~ is your home directory. Just edit it with a text editor and add something like this at the end:

alias upgrade='sudo apt-get update && sudo apt-get upgrade'

Save the file, then open a terminal and try it by typing upgrade. Notice that if you did this in a terminal you'll have to logout first (use exit or CTRL+D) and open it again, so the .bashrc file is read again. Alternatively, you can only type:

source ~/.bashrc

Several other examples of aliases:

alias lsh='ls -lh'
alias killfx='kill -9 $(pidof firefox)'
alias gotocd='cd /media/cdrom0'

The first one will list the files in a directory using the long method and displaying sizes in human readable units. The second one will kill Firefox (useful when it stops responding and you have many windows opened). And the third one will change the current working directory to /media/cdrom0.

Updated: Sep 21, 2008

2 comments:

Anonymous said...

I use `killall firefox` instead of your proposed killfx. This works with any program.

Craciun Dan said...

Yeah, I guess killall should do the job too.