Creating Useful Bash AliasesWritten by Mark Sanborn: Apr 23, 2008
Bash can be configured to recognize any word you want and link it to a command. This is called an alias. This is can be used for many reasons. For instance, you could assign a short word like dvdbackupnow to execute a very long command that you don’t want to memorize each time you want to back up a DVD. We will explore a few useful aliases and how to add them to bash.
To create an alias you need to modify ‘/home/user/.bashrc’. Towards the bottom of the file you can add lines for aliases. Most people will find that they already have at least one default alias. Usually the ‘ls’ command is mapped to ‘ls -G’. The ‘-G’ flag tells the program to print directories in color.
Once the ‘.bashrc’ file is modified you will have to re-login to that shell for the settings to take effect.
Lets say you maintain multiple servers using ssh with different IP addresses and varying user names. You could use aliases to minimize typing and having to memorize IP addresses and names.
alias server1='ssh 192.168.1.102 -l pete'
alias server2='ssh 192.168.1.103 -l larry'
alias server3='ssh www.myremoteserver.com -l bob'
You could make a system backup script and whenever you feel like you need to make a quick unscheduled backup you could use:
alias backupNow='sh /path/to/my/backupscript.sh'
You could make an alias to show open ports in your system.
alias openports='netstat -nape --inet'
It is often helpful to make an alias for really long directories that you navigate to a lot.
alias g2path='cd /go/to/this/path/that/is/really/long/and/annoying/to/type; ls'
For the ultra paranoid you can set up an alias to wipe your hard drive.
alias wipenow='dd if=/dev/urandom of=/dev/hda'
Using aliases with variables
You can use variables in aliases if you want to get more use out of them.
alias psx="ps -auxw ¦ grep $1"
This will except a variable in the alias. For example you could type this command:
$ psx ssh
This will print out detailed information about the ssh process.
By setting variables into your alias you can make your alias have multiple uses. For example our ‘psx’ alias can be used to find all the processes started by user, ‘mark’.
If you have a long command that you type frequently consider putting it in as an alias. If you think of any useful alias ideas let us know in the comments below!
Need to print shipping labels on your site?
Checkout my product RocketShipIt for simple easy-to-use developer tools for UPS™ FedEx™ USPS™ and more.