Sunday, 27 January 2013

Creating aliases in Ubuntu

Aliases are handy shortcuts that allow you to quickly perform frequently executed statements in the bash terminal. To see the currently active aliases simply type:

 $ alias

On my fresh install of Ubuntu this returns:

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

Typing ll in other words is equivalent to typing ls -alF. Nice. How about adding an alias of our own?

I frequently visit /mnt/Storage/tools on my filesystem - quite a mouthful (handful?) to type everytime. To add an alias to navigate to this folder I can simply enter:

$ alias tools='cd /mnt/Storage/tools'

Now all I need to do is type tools from anywhere in the terminal to jump straight to that location.

However, we've still one small problem - this alias will only remain available for the duration of the current bash session.

To create aliases that will be available the next time we fire up the terminal, simply create a ~/.bash_aliases file and declare them there. Once you've saved the file, type:

$ source ~/.bashrc

to refresh the current session and you're done. Happy aliasing!