Bash aliases
Section: Linux Articles
Date: 29 April 2007
Author: Tim Butler
When you run the bash shell (eg, start the GNOME Terminal under Linux), it looks for some configuration files in your home directory. One of these is ".Bashrc", which will be used in this quick guide. Open this file in your favourite text editor, remembering that it is a hidden file and you may not see it initially when you view your home folder.
Scroll down to the part of the configuration file that looks like this:
# some more ls aliases #alias ll='ls -l' #alias la='ls -A' #alias l='ls -CF'
This is where we can add in some aliases for common commands, making them always available when you open a Bash shell. Of course the lines can be anywhere in the file, but it's easiest to keep them all in the one location.
The examples fairly clearly show the syntax required, but here's a few examples I have in my .Bashrc file:
alias servername1='ssh username@servername1' alias servername2='ssh username@servername2'
With the aliases for the servernames, I can simply login to these servers simply by typing in the servername. This is made simpler by the fact that I also authenticate via keys rather than passwords (you can read my article on this here). While it may not seem like a big timesaver, when you call the command multiple times a day it makes light work of it all.
alias homebackup='rsync -var /home/butler servername:/backups/desktop-home'
To quickly backup my files to a remote server, I simply run my "homebackup" alias to sync this with one of my servers remotely. As with everything important these days, it always pays to keep regular backups! Now while backing up my home folder to a remote share all the time may sound time (and bandwidth) consuming, Rsync is far more efficient than you think. On average I try to run this command every few days and it takes me no more than 5 minutes to backup all this data, which is nearly approaching the 10GB mark. I have a neat little introduction to Rsync available here which details how and why this is so efficient.
alias twcode='cd /home/butler/Projects/TechWatch/webcode' alias twhtml='cd /home/butler/Projects/TechWatch/templates' alias twdevserver='python /home/butler/Projects/TechWatch/webcode/TechWatch/manage.py runserver'
These are just some examples of what I have setup when doing any development work, since I use the command line quite heavily. When updating projects such as this site, it normally requires changes in two different locations (the backend and the html). Adding some quick aliases in for changing directories means that I can flick between the two very quickly if required. A lot of Unix and Linux users normally do this through environment variables, however I find it easier to alias it all to a single command.
I also have a quick command to start the Django development server, so that I can easily test the changes I've made.
Please feel free to share your favourite Bash aliases in the comments area, so that we can all make our lives using Linux/Unix shells quicker and easier :)
Fox commented, on September 26, 2007 at 12:14 p.m.:
I use OS X, and for writing Perl scripts or simple java code I use TextWrangler. One problem is that the files saved by TextWrangler are not executable so I have the following alias defined:
alias cx='chmod u+x *'
which I can run in the source directories for the scripts or .java files to make them all executable.











