Thursday, 4 July 2013

Centos 6/RHEL using alias in the Bash Shell


The alias command allows you to use just one word or letter instead of an entire line or code block in the bash shell.

The full command can include any options and filenames.

The alias is typed in to the shell before proceeding with the command, or placed in the ~/.bashrc file to make it a permanent feature for your system. A quick example of it is 

set -o vi

which allows you to use vi/vim keybindings in the bash shell.

To use an alias just open up the ~/.bashrc file with vi and place it under 'user specific aliases and functions'.

You probably already have some default aliases, to find them run the alias command.

$ alias

alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mc='. /usr/libexec/mc/mc-wrapper.sh'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'


The above are aliases which are set on installation and they are usually in the /etc/bashrc file, which we leave alone.

Other aliases are placed in the ~/.bashrc hidden file.

The first thing you can do (afer set -o vi) is change the shell appearance to make it both easier on the eye and functional.

See Vi and Vim commands

$ vi ~/.bashrc

*****************************************************************

bash_prompt_command()
{
   RTN=$?
   prevCmd=$(prevCmd $RTN)
}
PROMPT_COMMAND=bash_prompt_command
prevCmd()
{
   if [ $1 == 0 ] ; then
       echo $GREEN
   else
       echo $RED
   fi
}
if [ $(tput colors) -gt 0 ] ; then
   RED=$(tput setaf 1)
   GREEN=$(tput setaf 2)
   RST=$(tput op)
fi
export PS1="\[\e[36m\]\u.\h.\W\[\e[0m\]\[\$prevCmd\]>\[$RST\]"


*****************************************************************

The function above will return the shell prompt as green or red depending on the result of the previous command.

Other aliases
To create the alias use the following

alias name=value
alias name='command'
alias name='command arg1 arg2'
alias name='/path/to/script'
alias name='/path/to/script.pl arg1'

 
As an example, create the alias c for the clear command.

alias c='clear'
 
To clear the screen you just type the letter 'c'

(and hit enter)

$ c
 
Disable an alias temporarily
An alias can be temporarily disabled using the following.
  
path/to/full/command
 
/usr/bin/clear 

call alias with a backslash 
 
\c
 
Remove an alias
Use the command unalias to remove aliases.

unalias aliasname
 
To remove the alias c which was created earlier.

unalias c
 
You also need to delete the alias from the ~/.bashrc file.

Make aliases permanent

As an example, the alias c typed in to the shell remains in effect only during the current login session. Once you log out or reboot, the alias c will be gone. To avoid this problem, add alias to your ~/.bashrc file

vi ~/.bashrc
 
The alias c for the current user can be made permanent by entering the following

alias c='clear'
 
Save and close the file. System-wide aliases (i.e. aliases for all users) can be put in the /etc/bashrc file. Please note that the alias command is built into a various shells including ksh, tcsh/csh, ash, bash and others.

Os specific aliases
Add code as follows in ~/.bashrc


 Get os name via uname
_myos="$(uname)"
 

 add alias as per os using $_myos
case $_myos in
  Linux) alias foo='/path/to/linux/bin/foo';;
  FreeBSD|OpenBSD) alias foo='/path/to/bsd/bin/foo' ;;
  SunOS) alias foo='/path/to/sunos/bin/foo' ;;
  *) ;;
esac
 
You can define a variety of aliases as follows to save time.

Control ls output 

Colorize the output

alias ls='ls --color=auto'

Use a long listing format
alias ll='ls -la'
 

Show hidden files
alias l.='ls -d .* --color=auto'
 

Control cd command behavior
a quick way to get out of current directory

alias ..='cd ..'
alias .3='cd ../../../'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'
 

Control grep command output
grep command is a command-line utility for searching plain-text files for lines matching a regular expression

 Colorize the grep command output for ease of use (use for log files)
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
 

Calculator with math support
alias bc='bc -l'
  
Generate sha1 digest

alias sha1='openssl sha1'

  
Create parent directories
Using mkdir

alias mkdir='mkdir -pv'
 
Improve aesthetics of the mount command

alias mount='mount |column -t'
 

Command short cuts to save time
 short cuts
alias h='history'
alias j='jobs -l'
 

Create a new set of commands
alias path='echo -e ${PATH//:/\\n}'
alias now='date +"%T'
alias nowtime=now
alias nowdate='date +"%d-%m-%Y"'
 

Set vim as default
alias vi=vim
alias svi='sudo vi'
alias vis='vim "+set si"'
alias edit='vim'
 

Control output of ping
Stop after sending count ECHO_REQUEST packets
alias ping='ping -c 5' 

Do not wait interval 1 second, go fast
alias fastping='ping -c 100 -s.2'

  
Show open ports
Use netstat command to list all TCP/UDP port on the server

alias ports='netstat -tulanp'
 
Control firewall (iptables) output
Netfilter is a host-based firewall for Linux operating systems. It is included as part of the Linux distribution and it is activated by default. This post list most common iptables solutions required by a new Linux user to secure his or her Linux operating system from intruders. 


Update RHEL / CentOS / Fedora Linux
  
yum package management


alias update='sudo yum update'
alias updatey='sudo yum -y update'
 

Control web servers

pass via sudo so whoever is admin can reload it
 
alias nginxreload='sudo /usr/local/nginx/sbin/nginx -s reload'
alias nginxtest='sudo /usr/local/nginx/sbin/nginx -t'
alias lightyload='sudo /etc/init.d/lighttpd reload'
alias lightytest='sudo /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf -t'
alias httpdreload='sudo /usr/sbin/apachectl -k graceful'
alias httpdtest='sudo /usr/sbin/apachectl -t && /usr/sbin/apachectl -t -D DUMP_VHOSTS'