一波实用的Bash Shell整理

2019-09-23 09:25:36王振洲

    网络

对于所有用在互联网和本地网络的命令,也有一些神奇的别名给它们。

  alias websiteget="wget --random-wait -r -p -e robots=off -U mozilla"

下载整个网站:websiteget [URL]。

  alias listen="lsof -P -i -n" 

显示出哪个应用程序连接到网络。

2015629114111928.jpg (640×158)

  alias port='netstat -tulanp'

显示出活动的端口。

  gmail() { curl -u "$1" --silent "https://mail.google.com/mail/feed/atom" | sed -e 's/</fullcount.*/n/' | sed -e 's/.*fullcount>//'}

大概的显示你的谷歌邮件里未读邮件的数量:gmail [用户名]

  alias ipinfo="curl ifconfig.me && curl ifconfig.me/host"

获得你的公网IP地址和主机名。

  getlocation() { lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 'sip address flag '|sed 'sMy';} 

返回你的当前IP地址的地理位置。
也许无用

所以呢,如果一些别名并不是全都具有使用价值?它们可能仍然有趣。

  kernelgraph() { lsmod | perl -e 'print "digraph "lsmod" {";<>;while(<>){@_=split/s+/; print ""$_[0]" -> "$_"n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -;}

绘制内核模块依赖曲线图。需要可以查看图片。

  alias busy="cat /dev/urandom | hexdump -C | grep 'ca fe'" 

在那些非技术人员的眼里你看起来是总是那么忙和神秘。

2015629114137208.jpg (640×442)

做为奖励,这里有我提到的全部别名和函数的纯文本版本,随时可以复制粘贴到你的.bashrc。(如果你已经一行一行的复制到这里了,哈哈,你发现你又浪费了生命的几秒钟~)

   

#Productivity
  alias ls="ls --color=auto"
  alias ll="ls --color -al"
  alias grep='grep --color=auto'
  mcd() { mkdir -p "$1"; cd "$1";}
  cls() { cd "$1"; ls;}
  backup() { cp "$1"{,.bak};}
  md5check() { md5sum "$1" | grep "$2";}
  alias makescript="fc -rnl | head -1 >"
  alias genpasswd="strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d 'n'; echo"
  alias c="clear"
  alias histg="history | grep"
  alias ..='cd ..'
  alias ...='cd ../..'
  extract() {
    if [ -f $1 ] ; then
     case $1 in
      *.tar.bz2)  tar xjf $1   ;;
      *.tar.gz)  tar xzf $1   ;;
      *.bz2)    bunzip2 $1   ;;
      *.rar)    unrar e $1   ;;
      *.gz)    gunzip $1   ;;
      *.tar)    tar xf $1   ;;
      *.tbz2)   tar xjf $1   ;;
      *.tgz)    tar xzf $1   ;;
      *.zip)    unzip $1    ;;
      *.Z)     uncompress $1 ;;
      *.7z)    7z x $1    ;;
      *)   echo "'$1' cannot be extracted via extract()" ;;
       esac
     else
       echo "'$1' is not a valid file"
     fi
  }
   
  #System info
  alias cmount="mount | column -t"
  alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^/]*//--/g' -e 's/^/  /' -e 's/-/|/'"
  sbs(){ du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e';}
  alias intercept="sudo strace -ff -e trace=write -e write=1,2 -p"
  alias meminfo='free -m -l -t'
  alias ps?="ps aux | grep"
  alias volume="amixer get Master | sed '1,4 d' | cut -d [ -f 2 | cut -d ] -f 1"
   
  #Network
  alias websiteget="wget --random-wait -r -p -e robots=off -U mozilla"
  alias listen="lsof -P -i -n"
  alias port='netstat -tulanp'
  gmail() { curl -u "$1" --silent "https://mail.google.com/mail/feed/atom" | sed -e 's/</fullcount.*/n/' | sed -e 's/.*fullcount>//'}
  alias ipinfo="curl ifconfig.me && curl ifconfig.me/host"
  getlocation() { lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 'sip address flag '|sed 'sMy';}
   
  #Funny
  kernelgraph() { lsmod | perl -e 'print "digraph "lsmod" {";<>;while(<>){@_=split/s+/; print ""$_[0]" -> "$_"n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -;}
  alias busy="cat /dev/urandom | hexdump -C | grep "ca fe""