Aliases, loosing command identity…

Today, I was looking into my environment setup and realized, aliases are really useful. So for your enjoyment, here is a quick reference into how to setup and utilize the flexibility of aliases in your own Unix setup.

What is alias?

Aliases in any platform is a command that lets you replace one word for another.  Nothing really confusing here. The alias command is:

alias <NAME>=<VALUE>

An example:

alias list='ls –l'

This will replace any instance where you type list with “ls –l.”

Screen Shot 2014-01-16 at 6.52.45 PM

Listing isn’t a big deal, but say you have a really long command, imagine replacing that really long command with one word. Seeing the benefits now?

To get rid of an alias, either close your shell or type the command:

unalias <NAME>

This is great and all, but the alias will only work for the current shell it was called in. As soon as I close my shell, my list alias will disappear. What if I want an alias to be a permanent fixture. I want every shell I open to have my list alias. How do I do this? Well, the bash configuration files will help us here.

Bash Files for Unix Systems

When you login to a Unix system over bash shell, ~/.bash_profile is read. From this file, the shell gathers settings for that particular bash shell. If by chance ~/.bash_profile is corrupted or does not exist, ~/.profile is read instead. Say you are already logged into a Unix environment and you open a shell, instead of ~/.bash_profile being read, ~/.bashrc is instead reviewed for shell settings.

So ~/.bash_profile is read by a login shell and ~/.bashrc is read the other shell. I hate being repetitive. I want my aliases in one spot, I don’t want to have to manage two sets.

Well, there is a really easy fix for this problem! Ensure your ~/.bash_profile invokes ~/.bashrc! Check to see if your ~/.bash_profile has the following lines of code that perform the operation or just add them yourself.

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

Nice, now you can store all your aliases in one place, ~/.bashrc. I just stuck mine in at the bottom of the file.

Example Aliases

Sudo reboot every time:

alias reboot='sudo reboot'

Sudo update application package manager:

alias update='sudo apt-get upgrade'

List directories in color:

alias ls='ls --color=auto'

Forget vi, always use vim:

alias vi='vim'

Show open ports:

alias ports='netstat -tulanp'

Directory traversals made easy:

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

Go to your web directory:

alias www='cd /var/www/html'

Grep with color:

alias grep='grep --color=auto'

Remove recursively by force:

alias rm='rm –rf'

The possibilities are endless! Have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *


Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.