Thursday, January 19, 2012

Unix Bash Alias Tutorial


An alias command is simple string substitution of one text for another, when it is used as the first word of a simple command.
In this article let us review how to set / unset bash aliases permanently and temporarily. Let us also review some useful bash alias examples that you can use in your bash startup files.

How to Set an Alias

Aliases can be defined on the command line, in .bash_profile, or in .bashrc, using the following syntax:
$ alias name='unix command with options'
  • alias – is a shell built-in
  • name – any user-defined simple name for the alias.
  • command – any unix command, which could be with the options.
This means that name is an alias for command. Whenever name is typed as a command, bash will substitute the corresponding command along with the options in its place.
Note: There are no spaces on either side of the equal sign. Quotes around command are necessary if the string being aliased consists of more than one word.
Executing this command in command line makes this as temporarily alias. That is, this alias is available until you exit the shell. Storing the alias in bash startup files makes it as permanent alias.
The following aliases might be useful. You can set these aliases in ~/.bashrc file.

Most Common Alias Examples

Following aliases are ready for you to use straight away. What is your favorite alias?

Open last modified file in vim

alias Vim="vim `ls -t | head -1`"

Find top 5 big files

alias findbig="find . -type f -exec ls -s {} \; | sort -n -r | head -5"

Grep for a bash process

alias psg="ps -aux ¦ grep bash"

List including hidden files with indicator and color

alias ls='ls -aF --color=always'

List in long format

alias ll='ls -l'

To clear all the history and screen

alias hcl='history -c; clear'

Make basic commands interactive, and verbose

alias cp="cp -iv"      # interactive, verbose
alias rm="rm -i"      # interactive
alias mv="mv -iv"       # interactive, verbose
alias grep="grep -i"  # ignore case

Easy to use aliases for frequently used commands

alias x="exit"

Clear the screen and list file

alias cls='clear;ls'

Filesystem diskspace usage

alias dus='df -h'

To navigate to the different directories

alias ..='cd ..'
alias ...='cd ../..'

Alias examples that should be modified for your environment

The alias examples provided in this section should be modified according to your environment before using them.

Remove the firefox lock

alias rm_fire_lock='/bin/rm .mozilla/firefox/NAME.default/.parentlock' # edit NAME

To login to the remote machine through ssh with loginname

alias server_name='ssh 192.168.1.1 -l tom' # change the ip & user name
alias ser2='ssh www.dbserver.com -l kgf' # create as many alias as required.

To login to remote cvs server

export CVS_RSH=/usr/local/bin/ssh
alias cvl='cvs -d :ext:username@cvs.server.com:/usr/local/cvsroot'  # change required.

Unmounting the cdrom

alias umnt='umount /mnt/cdrom'  # cdrom / thumb drive.

How to view all the aliases

Execute alias without arguments to view list of aliases set in a shell.
$ alias
alias ..='cd ..'
alias ...='cd ../..'
alias mnt='mount /mnt/cdrom'
alias umnt='umount /mnt/cdrom'
alias dus='df -h'
To view a particular alias, enter the command the format “alias aliasname” as shown below.
$ alias dus
alias dus='df -h'

How to temporarily stop using aliases

When you want to call the command instead of the alias, then you have to escape it and call.
$ \aliasname
For Example, alias cp=”cp -iv”, will ask you confirmation if you are about to overwrite a file. This can be annoying when you are copying lot of files that you already know you are going to overwrite. Probably you might want to temporarily use the regular cp command instead of the cp-alias.
So, if an alias cp exists, but you want to use the cp-command instead, escape the alias temporarily as shown below:
\cp * /backup/files/

How to remove an alias

unalias is a shell built-in to remove an alias. To remove a particular alias:
$ unalias hcl
 where unalias is a shell built-in.
 mnt is an alias name.
$ hcl
-bash: hcl: command not found

How to remove all aliases

unalias with -a option, removes all the aliases.
$ unalias -a
$ alias
NOTE: Shell functions are faster. Aliases are looked up after functions and thus resolving is slower. While aliases are easier to understand, shell functions are preferred over aliases for almost every purpose. You should be very careful replacing a standard command with an alias or a function.