May 24, 2008

How-To: Create Your Own Aliases

Aliases are custom commands which can be used to make the work with the shell easier and faster. For example, if your current working directory is /usr/bin and you want to quickly go to /var/cache/apt/archives you can do a 'cd /var/cache/apt/archives'. If you want to save time, you may want to type only a command such as 'cache' or 'debs. Or you may want to update your Debian system in one command, so instead of typing 'su -c "apt-get update && apt-get upgrade"' you only type 'upgrade' or something of your choice. This is where aliases come to help.

In this How-To I will explain two ways of creating aliases.

Method 1
The first method is to add aliases directly into your ~/.bashrc file. The format should be:

alias name='command'

For example you can add something like:

alias ll='ls -l'
alias deb='cd /var/cache/apt/archives'
alias dld='cd ~/downloads'

alias upgrade='su -c "apt-get update && apt-get upgrade"'

Open a console and try your aliases. For example if you type 'deb', the current working directory will be changed to /var/cache/apt/archives.

Method 2
The second method lets you make a separate aliases file, so you won't have to put them in ~/.bashrc, but to a file of your choice. First, edit your ~/.bashrc file and add or uncomment the following lines:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases

Save it and close the file. After that, all you have to do is create a ~/.bash_aliases file and add your aliases there, with the same format specified at the first method.

1 comment:

krishnakant said...

I follow all steps but when i create aliases for Desktop
alias Desktop='cd /home/sns/Desktop'
when i run the command
It not working, give the message like tish:
-su: Desktop: command not found