Tuesday, July 16, 2013

Using alias in Linux - Temporary and Permanent

Sometimes we need to make alias of command or group of command including of any options, arguments and redirection.
We can pre-set a string to run any command using alias.

alias command allows a user to create simple name for commands including arguments or options.

The format is alias name='command'

For example:

alias install="sudo apt-get install"
alias poweroff="sudo shutdown -h now"

After making alias of above command, you can install package with install command instead of sudo apt-get install and you can shutdown the PC with simple command poweroff.

we can also use alias ls="ls -al" to create a alias of ls command with -al option.
now if we run ls command it will execute ls -al command.
Now if we need to run original ls command we can by adding a backslash before the command
example \ls it will execute the original ls command.

If we need to create permanent alias we have to put alias in .bashrc file under user home directory.

The alias should be typed in below the line that says # User specific aliases and functions.

System-wide aliases can be put in the /etc/bashrc file.

System must be restarted after creating permanent alias.