I just read this great article about Command Aliases in Unix shells. I felt so inspired by this post, I'm writing about how you can use aliases to speed up web development.

Introduction

If you don't have a basic understanding of the UNIX shell, this article is not for you. If you are familiar with basic command such as: ls, cd, cp, mv, pwd you should be able to follow along.

Creating an alias helps you to run long commands with a few keystrokes. Hopefully my examples will make this clear. Wikipedia can explain this better than I can.

All the below examples are using Borne Again Shell (bash).

Easy Example

Starting with something simple: The ls command is probably the most used command on the Linux command line. While I was doing some basic server administration, Bruce Alderson suggested I set-up an ll alias. I was constantly using ls -l to view files, and could save precious keystrokes by creating an alias. This is how you create an alias for ll to run the command ls -l.

alias ll='ls -l'

Now whenever you type "ll" you will receive the same results as you would typing "ls -l". Here is the gotcha, it is very important to remember that an alias will only last for your current session. In order to keep the alias around permanently you have to add the alias to your .bashrc in your home directory.

How to set-up .bashrc

placing a few aliases in .bashrc is okay, but we should really be organized. I place all of my aliases in .bash_aliases in my home directory. I didn't invent this, it is part of Ubuntu's documentation (lines 68 - 75 in default .bashrc). If your using Ubuntu you will want to un-comment the following code from your .bashrc file. If you are running a different distribution I recommend reading the documentation.

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

The above the alias file if it exists.

My Aliases

As I always share my code to help you learn, I've included my .bash_aliases file here. I'd love to get your feedback!

# Build Project Directories
# v1.0
# .bash_aliases
# Last Updated: Jul 24, 2009
# Documentation: 
# http://www.nickyeoman.com/blog/desktop-development/62-unix-aliases


#ls long hidden and human readable
alias ll='ls -lah'

#display only my mounted drives
alias disksize='df -h | grep ^/dev'

#display svn status
alias stat='svn status'

#mysql tunnel
alias mytun='sudo /etc/init.d/mysqld stop && ssh username@server.com -L 3306:localhost:3306'

Notes

  • You can remove an alias using unalias
  • You can see available aliases by using alias
  • You cannot use parameters with aliases(that is where bash scripts come in to play)

Reference

Comments

Show/Hide Comment form