One of the really useful features almost every Unix shell has is support for command aliases – a way to run a command or a series of Unix commands using a shorter name you get associated with such commands.
An example of a command alias in Unix shell
Here's one of the most useful aliases I have for Solaris systems:
solaris$ alias ls='/usr/local/gnu/bin/ls --color -F'
What is allows me to do is to simply type "ls" instead of the really long command line it refers to: /usr/local/gnu/bin/ls –color -F.
You see, the ls command which is shipped with Solaris, doesn't have many options of the more up-to-date GNU ls command, and working with many Linux systems I quite like some of them like color highlighting of different directory objects – files, directories and executables.
Try typing the longer command a few times in a row, and compare it to the "ls" to get the idea of how much of a productivity gain one command alias can be!
Why you should use command aliases
Like many other things in Unix, aliases are a way to become more productive. The general rule of thumb is this: if you have to run some command something more than once every day – consider creating an alias for it. These are just a few cases where it makes sense to employ them:
- if you repeatedly check whether some files exist or get updated
- if you're monitoring a certain aspect of your OS and you get the values using grep command
- if you're connecting to the same hosts using rsh or ssh
All of these and many more examples are greatly simplified if you alias them to some shorter commands.
Creating new aliases in bash
Setting up a new alias is quite easy, the syntax for alias command is very straightforward. Let's say I want to automate the confirmation of swap usage based on a free command in Linux:
redhat$ free total used free shared buffers cached Mem: 2075156 945712 1129444 0 177292 503416 -/+ buffers/cache: 265004 1810152 Swap: 2040244 0 2040244
The result I'm after is this command:
redhat$ free | grep Swap Swap: 2040244 0 2040244
And here's how I can create an alias called "swp" which refers to this series of commands:
redhat$ alias swp='free | grep Swap'
Once you execute this command, you can start using swp as a Unix command:
redhat$ swp Swap: 2040244 0 2040244
Important: such a creation of new aliases is going to be only active for your current Unix shell and sub-shells you may spawn. To make your alias permanent, you'll have to update one of your initialization scripts like. For Linux and bash, you should add the same alias command to your .bashrc file.
Removing aliases in Unix
In very much the same way, you can use the unalias command to get rid of a certain alias. The nature of this command is such that you'll most likely use it when creating and debugging new aliases. It's unlikely that you'll need to use it in your initialization scripts.
Following the example above, here's how to get rid of the swp alias and verify that it's gone:
redhat$ unalias swp redhat$ swp bash: swp: command not found
How to list your current aliases
If you run the alias command without any parameters, you'll be shown a full list of aliases currently configured for your user account, here's an example from one of my systems:
l. ls -d .* --color=tty ll ls -l --color=tty ls ls --color=tty vi vim
That's it for today! Stay tuned for a follow-up post which will share some of the examples for command aliases in Unix. If you have some – please leave a comment so that I can share it with others!
Nick Yeoman says
Great article post! I remember learning this in my first year of college. I've not used alias' since (except on windows to fix their ls to work as dir). This is a great reminder of how we should be saving ourselves time using available tools.
My favorite alias is:
ll ls -la –color=tty
( added the -a option to mine)
I also setup my ssh connections like so:
ssh-mysql ssh [email protected] -L 3306:localhost:3306
(the above is an ssh tunnel for mysql)
verdon vaillancourt says
great tip, thanks!
Here's one that I use. It's a little specific to me perhaps, but a good example of how the alias reduces an oft used command from a great long string to 4 characters
alias cloc='/Users/verdon/Library/Scripts/cloc-1.08.pl –read-lang-def=/Users/verdon/Library/Scripts/cloc-definitions.txt'
Note: cloc is a script for counting lines of code
Nick Yeoman says
I loved this article so much I had to write about it myself!
http://www.nickyeoman.com/blog/desktop-development/62-unix-aliases
bhagya says
Some more information about unix alias command
http://scripterworld.blogspot.com/2009/09/unix-alias-command-parameters-or.html
DT says
Some more information about unix alias command
http://scripterworld.blogspot.com/2009/09/unix-alias-command-parameters-or.html