fredag 14 juni 2013

Create and use aliases


Jump to: navigation, search
← Path name expansionHomeThe tilde expansion →

  • An alias is nothing but shortcut to commands.
  • Use alias command to display list of all defined aliases.
  • Add user defined aliases to ~/.bashrc file.

Contents

Create and use aliases

Use the following syntax:
alias name='command'
alias name='command arg1 arg2'

Examples

Create an aliase called c to clear the terminal screen, enter:
alias c='clear'
To clear the terminal, enter:
c
Create an aliase called d to display the system date and time, enter:
alias d='date'
d
Sample outputs:
Tue Oct 20 01:38:59 IST 2009

How do I remove the alias?

  • Aliases are created and listed with the alias command, and removed with the unalias command. The syntax is:
unalias alias-name
unalias c 
unalias c d
To list currently defined aliases, enter:
alias
alias c='clear'
alias d='date'
If you need to unalise a command called d, enter:
unalias d
alias
If the -a option is given, then remove all alias definitions, enter:
unalias -a
alias

How do I permanently add aliases to my session?

  • If you want to add aliases for every user, place them either in /etc/bashrc or /etc/profile.d/useralias.sh file. Please note that you need to create /etc/profile.d/useralias.sh file.
  • User specific alias must be placed in ~/.bashrc ($HOME/.bashrc) file.

Sample ~/.bashrc file

# make sure bc start with standard math library
alias bc='bc -l'
# protect cp, mv, rm command with confirmation
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
 
# Make sure dnstop only shows eth1 stats
alias dnstop='dnstop -l 5  eth1'
 
# Make grep pretty 
alias grep='grep --color'
 
# ls command shortcuts 
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
 
# Centos/RHEL server update 
alias update='yum update'
alias updatey='yum -y update'
# vi is vim 
alias vi='vim'
 
# Make sure vnstat use eth1 by default 
alias vnstat='vnstat -i eth1'

How do I ignore an alias?

Consider the following example:
alias ls='ls --color'
To ignore an alias called ls and run ls command, enter[1]:
\ls
OR
"ls"
Or just use the full path:
/bin/ls
$(which ls)

References

Skicka en kommentar