Terminal Tip #1 – Aliases

This is my first Ubuntut, and for it I’d like to start off with a productivity tip. We’re going to create a bunch of aliases (shortcuts) for commands we use often.

The first step is to open up your user’s .bashrc file, conveniently located in your home directory with all the other configuration files. The bashrc file is a hidden file and its determines the behavior of interactive shells. For those of you who are adept at copying and pasting here is the code to get back to your home directory and open the file in Gedit.

cd ~ ; gedit .bashrc

Now that you’ve got your .bashrc file open we’re going to peruse on down to the bottom. You should find a block of text looking like this:

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

What this says is, “If the .bash_aliases file exists in your home directory use it”, rather simple. The reason we don’t just add aliases into the .bashrc file? If we use this method, then we have the ability to update the .bachrc file without losing our aliases. Another reason I use the .bash_aliases file is because I use a network of multiple machines and I like to keep the same aliases. So what we’re going to do to that is uncomment those lines, that is to say we’re going to remove the three # characters. Save and exit. After we’re done with that we’re going to create the file mentioned (.bash_aliases).

touch -bash_aliases ; gedit .bash_aliases

From this point on is pure creativity and necessity. Afterall,

"Necessity, who is the mother of invention."
-Plato, The Republic
Greek author & philosopher in Athens (427 BC - 347 BC)

And what I believe is a good antithesis,

"I don't think necessity is the mother of invention - invention . . . arises directly from idleness, possibly also from laziness. To save oneself trouble."
-Agatha Christie

Both apply for me. Now for the aliases, the syntax is rather simple. Here’s an example from my personal .bash_aliases file:

alias update='sudo apt-get update  ; sudo apt-get upgrade ; sudo apt-get dist-upgrade;'

I know it’s a bit much just to update, but you can tweak it however you’d like, a more concise version would be:

alias update='sudo apt-get upgrade'

To break this down for verbosity, the alias is placed before name of the alias you want to create, then without space there is the = character and a single quote, not a back tick. The last part is to place your command after the first single quote, then add the second single quote.

Before I end this ubuntut, here is a few of my own personal aliases:

alias rm='rm -v'
alias disku="du -sh * | sort -rn | head"
alias search="apt-cache search"
alias install="sudo apt-get install"
alias remove="sudo apt-get remove"
alias autoremove="sudo apt-get autoremove"
alias update="sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade"
alias go="gnome-open"
alias wget="wget -c"
alias handbrake="cd ~ && HandBrakeCLI"
alias email="fetchmail ; mutt"
alias music="mp3blaster"
alias web="sudo links2 -g"
alias reboot="sudo reboot"
alias restart="sudo shutdown -r now"

Thanks for checking this out. I hope you’ve picked up something handy,