Tuesday, September 16, 2008

UNIX alias

How many times have you wished to have shortcuts for some long syntaxes/commands on a UNIX host. Here is a way to do away with such hassles, There is a command "alias" available in all the UNIX hosts nowadays which allows one to associate a letter or a shortest word with a long command.

Listed below are the ways of using "alias" in the two major shells of UNIX that I have worked with. Let say for an instance I want to see the number of databases running on a host with the command "ps -ef|grep pmon", this is now going to have an alias of pmon

ksh $> alias pmon='ps -ef|grep pmon'

Ksh $> pmon
oracle 12219 1 0 Jun 02 ? 11:32 ora_pmon_DEV1
oracle 26642 1 0 Mar 21 ? 99:51 ora_pmon_DEV2
oracle 23512 21604 0 16:34:50 pts/3 0:00 grep pmon
oracle 26901 1 0 Mar 21 ? 118:19 ora_pmon_DEV3


csh $> alias pmon 'ps -ef|grep pmon'

csh $> pmon
oracle 12219 1 0 Jun 02 ? 11:32 ora_pmon_DEV1
oracle 26642 1 0 Mar 21 ? 99:51 ora_pmon_DEV2
oracle 23512 21604 0 16:34:50 pts/3 0:00 grep pmon
oracle 26901 1 0 Mar 21 ? 118:19 ora_pmon_DEV3



Any valid UNIX command can be associated with an alias, below are a couple of more examples.

Alias to open ORATAB file of Oracle

ksh $> alias oratab='vi /var/opt/oracle/oratab'

csh $> alias oratab 'vi /var/opt/oracle/oratab'

Here is another example of Navigating to a directory like ORACLE_HOME.

ksh $> alias o_h='cd $ORACLE_HOME'
csh $> alias o_h 'cd $ORACLE_HOME'


Now the crux of this concept is where to place the "alias" entry so you need not execute this for every session you open. This entry has to be made in the .profile for a ksh enabled unix account and in the .cshrc for a csh enabled unix account.But make a backup of the .profile/.cshrc file before making any changes to it as a general rule of thumb to not mess up with the integrity of these files.

If you want to remove the aliases permanently, comment out/remove the alias line from the .profile/.cshrc file which takes effect only at the next login or run the unalias command in the current session as a temporary fix as shown below.

ksh $> unalias o_h
csh $> unalias o_h

No comments: