Skip to content

Instantly share code, notes, and snippets.

@wangwenchao
Last active August 29, 2015 14:18
Show Gist options
  • Save wangwenchao/a89ca8e8746161e7a348 to your computer and use it in GitHub Desktop.
Save wangwenchao/a89ca8e8746161e7a348 to your computer and use it in GitHub Desktop.
bashrc_demo
# PATH
export PATH=$PATH:$HOME/bin:usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
# 为cd命令设定路径
export CDPATH=.:$HOME:/var/www
## set PS1 ##
PS1='{\u@\h:\w }\$ '
## 设定默认权限为644 ##
umask 022
## 为我自己的shell会话设定IST(印度标准时间) ##
TZ=Asia/Shanghai
### set shopt
## shopt set the shell assistance ##
# ref http://bash.cyberciti.biz/guide/Setting_shell_options
# 目录拼写纠正
shopt -q -s cdspell
# 保证每次终端窗口改变大小后会更新显示
shopt -q -s checkwinsize
# 打开高级模式匹配功能
shopt -q -s extglob
# 退出时附加命令历史而不是覆盖
shopt -s histappend
# 在命令历史使用多行
shopt -q -s cmdhist
# 在后台任务结束时立刻通知
set -o notify
# 禁用[CTRL-D]来结束shell
set -o ignoreeof
## 使用vi风格的行编辑接口,替代bash默认的emacs模式 ##
set -o vi
# Correct dir spellings
shopt -q -s cdspell
# Make sure display get updated when terminal window get resized
shopt -q -s checkwinsize
# Turn on the extended pattern matching features
shopt -q -s extglob
# Append rather than overwrite history on exit
shopt -s histappend
# Make multi-line commandsline in history
shopt -q -s cmdhist
# Get immediate notification of background job termination
set -o notify
# Disable [CTRL-D] which is used to exit the shell
set -o ignoreeof
# Disable core files
ulimit -S -c 0 > /dev/null 2>&1
## history ##
# 不往命令历史里写入相同的行
HISTCONTROL=ignoreboth
# 忽略这些命令
HISTIGNORE="reboot:shutdown *:ls:pwd:exit:mount:man *:history"
# 通过HISTSIZE和HISTFILESIZE设定命令历史的长度
export HISTSIZE=10000
export HISTFILESIZE=10000
# 为命令历史文件增加时间戳
export HISTTIMEFORMAT="%F %T "
# 附加到命令历史文件,而不是覆盖
shopt -s histappend
##
export PAGER=less
# Set vim as default text editor
export EDITOR=vim
export VISUAL=vim
export SVN_EDITOR="$VISUAL"
# Oracle database specific
export ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
export ORACLE_SID=XE
export NLS_LANG=$($ORACLE_HOME/bin/nls_lang.sh)
# Set JAVA_HOME
export JAVA_HOME=/usr/lib/jvm/java-6-sun/jre
# Add ORACLE, JAVA and ~/bin bin to PATH
export PATH=$PATH:$ORACLE_HOME/bin:$HOME/bin:$JAVA_HOME/bin
# Secure SSH login stuff using keychain
# No need to input password again ever
/usr/bin/keychain $HOME/.ssh/id_dsa
source $HOME/.keychain/$HOSTNAME-sh
# Turn on Bash command completion
source /etc/bash_completion
#
# alias cumstom command
# ref http://www.cyberciti.biz/tips/bash-aliases-mac-centos-linux-unix.html
# MS-DOS / XP cmd like stuff
alias edit=$VISUAL
alias copy='cp'
alias cls='clear'
alias del='rm'
alias dir='ls'
alias md='mkdir'
alias move='mv'
alias rd='rmdir'
alias ren='mv'
alias ipconfig='ifconfig'
# Other Linux stuff
alias bc='bc -l'
alias diff='diff -u'
# get updates from RHN
alias update='yum -y update'
# set eth1 as default
alias dnstop='dnstop -l 5 eth1'
alias vnstat='vnstat -i eth1'
# force colorful grep output
alias grep='grep --color'
# ls stuff
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'
alias bc='bc -l'
alias cd..='cd ..'
alias chgrp='chgrp --preserve-root'
alias chmod='chmod --preserve-root'
alias chown='chown --preserve-root'
alias cp='cp -i'
alias dnstop='dnstop -l 5 eth1'
alias egrep='egrep --color=auto'
alias ethtool='ethtool eth1'
alias c='clear'
alias down='sudo /sbin/shutdown -h now'
## 增加一些保护 ##
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
## Memcached ##
alias mcdstats='/usr/bin/memcached-tool 10.10.29.68:11211 stats'
alias mcdshow='/usr/bin/memcached-tool 10.10.29.68:11211 display'
alias mcdflush='echo "flush_all" | nc 10.10.29.68 11211'
## 默认命令参数 ##
alias vi='vim'
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias bc='bc -l'
alias wget='wget -c'
alias chown='chown --preserve-root'
alias chmod='chmod --preserve-root'
alias chgrp='chgrp --preserve-root'
alias rm='rm -I --preserve-root'
alias ln='ln -i'
# 彩色的grep输出
alias grep='grep --color=auto'
export GREP_COLOR='1;33'
# 彩色的ls
export LSCOLORS='Gxfxcxdxdxegedabagacad'
# Gnu/linux的ls
ls='ls --color=auto'
# BSD/os x的ls命令
# alias ls='ls -G'
#
# custom command
# ref http://bash.cyberciti.biz/guide/Chapter_9:_Functions
memcpu() { echo "*** Top 10 cpu eating process ***"; ps auxf | sort -nr -k 3 | head -10;
echo "*** Top 10 memory eating process ***"; ps auxf | sort -nr -k 4 | head -10;
}
# 在屏幕上显示10个最近的历史命令
function ht {
history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
}
# host和ping命令的替代
# 接受http:// 或 https:// 或 ftps:// 名称用作域或主机名
_getdomainnameonly(){
local h="$1"
local f="${h,,}"
# remove protocol part of hostname
f="${f#http://}"
f="${f#https://}"
f="${f#ftp://}"
f="${f#scp://}"
f="${f#scp://}"
f="${f#sftp://}"
# remove username and/or username:password part of hostname
f="${f#*:*@}"
f="${f#*@}"
# remove all /foo/xyz.html*
f=${f%%/*}
# show domain name only
echo "$f"
}
ping(){
local array=( $@ ) # get all args in an array
local len=${#array[@]} # find the length of an array
local host=${array[$len-1]} # get the last arg
local args=${array[@]:0:$len-1} # get all args before the last arg in $@ in an array
local _ping="/bin/ping"
local c=$(_getdomainnameonly "$host")
[ "$t" != "$c" ] && echo "Sending ICMP ECHO_REQUEST to \"$c\"..."
# pass args and host
$_ping $args $c
}
host(){
local array=( $@ )
local len=${#array[@]}
local host=${array[$len-1]}
local args=${array[@]:0:$len-1}
local _host="/usr/bin/host"
local c=$(_getdomainnameonly "$host")
[ "$t" != "$c" ] && echo "Performing DNS lookups for \"$c\"..."
$_host $args $c
}
#
#
export PAGER=less
export EDITOR=vim
export VISUAL=vim
export SVN_EDITOR="$VISUAL"
#
# custom Oracle env
#export ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
#export ORACLE_SID=XE
#export NLS_LANG=$($ORACLE_HOME/bin/nls_lang.sh)
#
# custom JAVA
export JAVA_HOME=/usr/lib/jvm/java-6-sun/jre
# 把ORACLE和JAVA加入到PATH里
export PATH=$PATH:$ORACLE_HOME/bin:$JAVA_HOME/bin
#
## Colorize the grep command output for ease of use (good for log files)##
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
#4: Start calculator with math support
alias bc='bc -l'
#4: Generate sha1 digest
alias sha1='openssl sha1'
#5: Create parent directories on demand
#mkdir command is used to create a directory:
alias mkdir='mkdir -pv'
#6: Colorize diff output
#You can compare files line by line using diff and use a tool called colordiff to colorize diff output:
# install colordiff package :)
alias diff='colordiff'
#7: Make mount command output pretty and human readable format
alias mount='mount |column -t'
#8: Command short cuts to save time
# handy short cuts #
alias h='history'
alias j='jobs -l'
#9: 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"'
#10: Set vim as default
alias vi=vim
alias svi='sudo vi'
alias vis='vim "+set si"'
alias edit='vim'
#11: Control output of networking tool called 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'
#12: Show open ports
#Use netstat command to quickly list all TCP/UDP port on the server:
alias ports='netstat -tulanp'
#13: Wakeup sleeping servers
#Wake-on-LAN (WOL) is an Ethernet networking standard that allows a server to be turned on by a network message. You can quickly wakeup nas devices and server using the following aliases:
## replace mac with your actual server mac address #
alias wakeupnas01='/usr/bin/wakeonlan 00:11:32:11:15:FC'
alias wakeupnas02='/usr/bin/wakeonlan 00:11:32:11:15:FD'
alias wakeupnas03='/usr/bin/wakeonlan 00:11:32:11:15:FE'
#14: 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.
## shortcut for iptables and pass it via sudo#
alias ipt='sudo /sbin/iptables'
# display all rules #
alias iptlist='sudo /sbin/iptables -L -n -v --line-numbers'
alias iptlistin='sudo /sbin/iptables -L INPUT -n -v --line-numbers'
alias iptlistout='sudo /sbin/iptables -L OUTPUT -n -v --line-numbers'
alias iptlistfw='sudo /sbin/iptables -L FORWARD -n -v --line-numbers'
alias firewall=iptlist
#15: Debug web server / cdn problems with curl
# get web server headers #
alias header='curl -I'
# find out if remote server supports gzip / mod_deflate or not #
alias headerc='curl -I --compress'
#16: Add safety nets
# do not delete / or prompt if deleting more than 3 files at a time #
alias rm='rm -I --preserve-root'
# confirmation #
alias mv='mv -i'
alias cp='cp -i'
alias ln='ln -i'
# Parenting changing perms on / #
alias chown='chown --preserve-root'
alias chmod='chmod --preserve-root'
alias chgrp='chgrp --preserve-root'
#17: Update Debian Linux server
#apt-get command is used for installing packages over the internet (ftp or http). You can also upgrade all packages in a single operations:
# distro specific - Debian / Ubuntu and friends #
# install with apt-get
alias apt-get="sudo apt-get"
alias updatey="sudo apt-get --yes"
# update on one command
alias update='sudo apt-get update && sudo apt-get upgrade'
#18: Update RHEL / CentOS / Fedora Linux server
#yum command is a package management tool for RHEL / CentOS / Fedora Linux and friends:
## distrp specifc RHEL/CentOS ##
alias update='yum update'
alias updatey='yum -y update'
#19: Tune sudo and su
# become root #
alias root='sudo -i'
alias su='sudo -i'
#20: Pass halt/reboot via sudo
#shutdown command bring the Linux / Unix system down:
# reboot / halt / poweroff
alias reboot='sudo /sbin/reboot'
alias poweroff='sudo /sbin/poweroff'
alias halt='sudo /sbin/halt'
alias shutdown='sudo /sbin/shutdown'
#21: Control web servers
# also pass it via sudo so whoever is admin can reload it without calling you #
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'
#22: Alias into our backup stuff
# if cron fails or if you want backup on demand just run these commands #
# again pass it via sudo so whoever is in admin group can start the job #
# Backup scripts #
alias backup='sudo /home/scripts/admin/scripts/backup/wrapper.backup.sh --type local --taget /raid1/backups'
alias nasbackup='sudo /home/scripts/admin/scripts/backup/wrapper.backup.sh --type nas --target nas01'
alias s3backup='sudo /home/scripts/admin/scripts/backup/wrapper.backup.sh --type nas --target nas01 --auth /home/scripts/admin/.authdata/amazon.keys'
alias rsnapshothourly='sudo /home/scripts/admin/scripts/backup/wrapper.rsnapshot.sh --type remote --target nas03 --auth /home/scripts/admin/.authdata/ssh.keys --config /home/scripts/admin/scripts/backup/config/adsl.conf'
alias rsnapshotdaily='sudo /home/scripts/admin/scripts/backup/wrapper.rsnapshot.sh --type remote --target nas03 --auth /home/scripts/admin/.authdata/ssh.keys --config /home/scripts/admin/scripts/backup/config/adsl.conf'
alias rsnapshotweekly='sudo /home/scripts/admin/scripts/backup/wrapper.rsnapshot.sh --type remote --target nas03 --auth /home/scripts/admin/.authdata/ssh.keys --config /home/scripts/admin/scripts/backup/config/adsl.conf'
alias rsnapshotmonthly='sudo /home/scripts/admin/scripts/backup/wrapper.rsnapshot.sh --type remote --target nas03 --auth /home/scripts/admin/.authdata/ssh.keys --config /home/scripts/admin/scripts/backup/config/adsl.conf'
alias amazonbackup=s3backup
#23: Desktop specific - play avi/mp3 files on demand
## play video files in a current directory ##
# cd ~/Download/movie-name
# playavi or vlc
alias playavi='mplayer *.avi'
alias vlc='vlc *.avi'
# play all music files from the current directory #
alias playwave='for i in *.wav; do mplayer "$i"; done'
alias playogg='for i in *.ogg; do mplayer "$i"; done'
alias playmp3='for i in *.mp3; do mplayer "$i"; done'
# play files from nas devices #
alias nplaywave='for i in /nas/multimedia/wave/*.wav; do mplayer "$i"; done'
alias nplayogg='for i in /nas/multimedia/ogg/*.ogg; do mplayer "$i"; done'
alias nplaymp3='for i in /nas/multimedia/mp3/*.mp3; do mplayer "$i"; done'
# shuffle mp3/ogg etc by default #
alias music='mplayer --shuffle *'
#24: Set default interfaces for sys admin related commands
#vnstat is console-based network traffic monitor. dnstop is console tool to analyze DNS traffic. tcptrack and iftop commands displays information about TCP/UDP connections it sees on a network interface and display bandwidth usage on an interface by host respectively.
## All of our servers eth1 is connected to the Internets via vlan / router etc ##
alias dnstop='dnstop -l 5 eth1'
alias vnstat='vnstat -i eth1'
alias iftop='iftop -i eth1'
alias tcpdump='tcpdump -i eth1'
alias ethtool='ethtool eth1'
# work on wlan0 by default #
# Only useful for laptop as all servers are without wireless interface
alias iwconfig='iwconfig wlan0'
#25: Get system memory, cpu usage, and gpu memory info quickly
## pass options to free ##
alias meminfo='free -m -l -t'
## get top process eating memory
alias psmem='ps auxf | sort -nr -k 4'
alias psmem10='ps auxf | sort -nr -k 4 | head -10'
## get top process eating cpu ##
alias pscpu='ps auxf | sort -nr -k 3'
alias pscpu10='ps auxf | sort -nr -k 3 | head -10'
## Get server cpu info ##
alias cpuinfo='lscpu'
## older system use /proc/cpuinfo ##
##alias cpuinfo='less /proc/cpuinfo' ##
## get GPU ram on desktop / laptop##
alias gpumeminfo='grep -i --color memory /var/log/Xorg.0.log'
#26: Control Home Router
#The curl command can be used to reboot Linksys routers.
# Reboot my home Linksys WAG160N / WAG54 / WAG320 / WAG120N Router / Gateway from *nix.
alias rebootlinksys="curl -u 'admin:my-super-password' 'http://192.168.1.2/setup.cgi?todo=reboot'"
# Reboot tomato based Asus NT16 wireless bridge
alias reboottomato="ssh admin@192.168.1.1 /sbin/reboot"
#27 Resume wget by default
#The GNU Wget is a free utility for non-interactive download of files from the Web. It supports HTTP, HTTPS, and FTP protocols, and it can resume downloads too:
## this one saved by butt so many times ##
alias wget='wget -c'
#28 Use different browser for testing website
## this one saved by butt so many times ##
alias ff4='/opt/firefox4/firefox'
alias ff13='/opt/firefox13/firefox'
alias chrome='/opt/google/chrome/chrome'
alias opera='/opt/opera/opera'
#default ff
alias ff=ff13
#my default browser
alias browser=chrome
#29: A note about ssh alias
#Do not create ssh alias, instead use ~/.ssh/config OpenSSH SSH client configuration files. It offers more option. An example:
# Host server10
# Hostname 1.2.3.4
# IdentityFile ~/backups/.ssh/id_dsa
# user foobar
# Port 30000
# ForwardX11Trusted yes
# TCPKeepAlive yes
#You can now connect to peer1 using the following syntax:
$ ssh server10
#30: It's your turn to share...
## set some other defaults ##
alias df='df -H'
alias du='du -ch'
# top is atop, just like vi is vim
alias top='atop'
## nfsrestart - must be root ##
## refresh nfs mount / cache etc for Apache ##
alias nfsrestart='sync && sleep 2 && /etc/init.d/httpd stop && umount netapp2:/exports/http && sleep 2 && mount -o rw,sync,rsize=32768,wsize=32768,intr,hard,proto=tcp,fsc natapp2:/exports /http/var/www/html && /etc/init.d/httpd start'
## Memcached server status ##
alias mcdstats='/usr/bin/memcached-tool 10.10.27.11:11211 stats'
alias mcdshow='/usr/bin/memcached-tool 10.10.27.11:11211 display'
## quickly flush out memcached server ##
alias flushmcd='echo "flush_all" | nc 10.10.27.11 11211'
## Remove assets quickly from Akamai / Amazon cdn ##
alias cdndel='/home/scripts/admin/cdn/purge_cdn_cache --profile akamai'
alias amzcdndel='/home/scripts/admin/cdn/purge_cdn_cache --profile amazon'
## supply list of urls via file or stdin
alias cdnmdel='/home/scripts/admin/cdn/purge_cdn_cache --profile akamai --stdin'
alias amzcdnmdel='/home/scripts/admin/cdn/purge_cdn_cache --profile amazon --stdin'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment