Sunday, December 19, 2010

Working With Alias

In Linux, we are provided with a cool tool that can create new shortened command for a  given command. It is called alias. That is, it allows a user to create simple names or abbreviations (even consisting of just a single character) for commands regardless of how complex the original commands are and then use them in the same way that ordinary commands are used. The alias command is built into a number of shells including ash, bash (the default shell on most Linux systems), csh and ksh.To create alias type the command:
#alias  
Eg.#alias dir="ls" 

When we create the alias, the original command will still work.

Simply typing alias will list all the created aliases. To remove alias type:
unalias dir
To remove all aliases type:
unalias -a
Using the command alias will create alias for only current session & current user. Once you log off, all your aliases will be reset.

To create permanent aliases, you have to append the file .bashrc in the user directory (for root -> /root/.bashrc ; for other users -> /home/user/.bashrc). System-wide aliases can be put in the /etc/bashrc file. The system needs to be restarted before system-wide aliases can take effect.
Go to the directory of user & type 'ls -a' to see hidden files. Note that file name starting with (.) are hidden in Linux.
Open the file in vi/gedit & type the new lines at the end of file like this:
alias internet='sudo ifconfig' 
alias p='pwd' 
alias install='sudo apt-get -y install' 
alias remove='sudo apt-get -y remove' 
Likewise, you can add some more aliases.

No comments:

Post a Comment