Customizing the Terminal: Create Useful Aliases

Written by BinnyVA on March 28, 2009 – 12:35 am -

Terminal

This is part two of the ‘Customizing the Terminal’ series. Part one is ‘Customizing the Terminal: The Prompt‘. In this part, we’ll see how to create aliases to make working in the console easier.

How To Create an Alias

You can create a temporary alias using this command…

alias new_name='old command'

This will stop working when you exit the terminal. If you want to make the alias permanent, put the same command in your ~/.bashrc file.

There is another way to create an alias – create a executable file and place it in a folder in your path. This is not technically an alias – but it works the same way. I use this for alias that tend to change often. Its easier to find a file in a folder and edit it. YMMV.

My Aliases

This is a incomplete list of the aliases I use. Feel free to copy them to your .bashrc file.

Quick Directory Jumps

Create an alias to jump to folders you have to visit often. This is my list…

alias www='cd /var/www/html'
alias e='cd /mnt/x'

Relative Jumps

The above jumps are absolute jumps – relative jumps are possible too…

alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'

Some prefer this syntax…

alias ..='cd ..'
alias ..2='cd ../..'
alias ..3='cd ../../..'
alias ..4='cd ../../../..'

Often Used Commands

If you use some commands a lot, create smaller alternative for it…

alias x='exit'
alias q='exit'
alias rmdir='rm –rf'

Many of my own scripts are also alias’ed this way…

alias bk='perl "/home/binnyva/Scripts/Perl/Maintenance/Rsync Backup/RsyncBackup.pl"'
alias rbk='perl "/home/binnyva/Scripts/Perl/Maintenance/Rsync Backup/RsyncRemoteBackup.pl"'
alias nbk='perl "/home/binnyva/Scripts/Perl/Maintenance/Rsync Backup/RsyncNetworkBackup.pl"'
alias bdb='perl "/home/binnyva/Scripts/Perl/Maintenance/Database Backup/Dbbackup.pl"'

Complex Commands

Create a short version of long and complex command using alias…

alias gitstat='git status | perl -pe "exit if(/Untracked files\:/)"'
alias ra='ruby script/server'
alias wikipedia='cd /mnt/x/Data/Wikipedia/mywiki; firefox "http://localhost:8001/"; python manage.py runserver 8001; '
alias sup='svn update'

and more.

Command Changes

When I came from Windows to Linux, I was used to the dos commands – but not to the linux’s mv,cp commands. So I used to have aliases for those(I don’t have these now)

alias move='mv'
alias copy='cp'
alias ren='mv'
alias del='rm'

If you go from Red Hat/Fedora to Debian/Ubuntu(or vise versa), you can set up a few alias to make the change easier…

alias yum='apt-get'

You can get a lot of ideas for more aliases by looking at others .bashrc files.

Now tell me you aliases…


del.icio.us | Digg it | reddit | StumbleUpon

Tags: , , , ,
Posted in Command Line, Configuration | 15 Comments »

15 Comments to “Customizing the Terminal: Create Useful Aliases”

  1. Noushad says:

    Hi Binny,

    I didn’t have a ~/.bashrc file on my home folder. I created on and added alias. alias coda=’open -a /applications/Coda.app’
    It’s not working. I am using mac os x 10.5. it works temporarily in terminal.

    What could be wrong?

  2. BinnyVA says:

    I’m sorry – but I am not sure how it should work in the Mac system. Try posting the question in some Mac forum.

  3. Noushad says:

    Thanks anyway.. Binny.

    Looking forward for more interesting tips.. Subscribing you :)

  4. Noushad says:

    OX 10.5 Terminal doesn’t read ~/.bashrc file natively. We need to place this line “source ~./bashrc” in ~/.bash_profile file and it works. Hope this info would be helpful to someone.

  5. Another interesting thing to use in the dot bashrc file is funtions. Example (all the .. alias can be done with just one function):
    http://blog.amit-agarwal.co.in/2009/04/06/quick-one-liner-bash-function-to-navigate-parent-directories-quickly/

  6. [...] I’m a devoted command line user. It’s faster to do so many things and GUI’s for command line functions never have the same options. Really who wants to repeat their typing all the time though? That is where alias’s come in. I set up an alias for things like changing to my htdocs folder for my local dev environment, SSHing into my servers. I also set up alias’s to hit certain directories on my servers that I am in regularly. Save your self some time and learn how to set up alias’s. [...]

  7. kujjhotika says:

    Hi Noushad,

    I am not sure if it is too late to answer your question. However, if you have not
    created any alias yet on your MAC system, here we go with the solution.

    On MAC instead of .bashrc (linux system) you just open the .profile file in your home
    directory. So that if you use vi .profile, then you can use any alias just by editing this file. For example,
    alias c=’clear’. Here c command will do the work same as the command clear.

  8. noushad says:

    Thanks man! yes that was linux newbie days. I could create alias on mac machine too. (.bash_profile on home folder).

  9. Vera says:

    Is there a way to see a list of all the aliases I’d set up?

  10. noushad says:

    you just need to type ‘alias’.

  11. Vera says:

    Thanks! Should’ve known.

  12. jeanmi1229 says:

    Fantastic Solution from kujjhotika. Your right you need to edit the .profile file to update your new aliases. Worked a treat for me. Well done.

  13. [...] I’m a devoted command line user. It’s faster to do so many things and GUI’s for command line functions never have the same options. Really who wants to repeat their typing all the time though? That is where alias’s come in. I set up an alias for things like changing to my htdocs folder for my local dev environment, SSHing into my servers. I also set up alias’s to hit certain directories on my servers that I am in regularly. Save your self some time and learn how to set up alias’s. [...]

  14. anurag says:

    how can i use – as an alias?

    alias -=’cd -’
    is not permitted.

Leave a Comment