收藏一些实用的 bash 别名和函数

作为一个命令行探索者,你或许发现你自己一遍又一遍重复同样的命令。如果你总是用ssh进入到同一台电脑,如果你总是将一连串命令连接起来,如果你总是用同样的参数运行一个程序,你也许希望在这种不断的重复中为你的生命节约下几秒钟。

解决方案是使用一个别名(alias)。正如你可能知道的,别名用一种让你的shell记住一个特定的命令并且给它一个新的名字的方式。不管怎么样,别名有一些限制,它只是shell命令的快捷方式,不能传递或者控制其中的参数。所以作为补充,bash 也允许你创建你自己的函数,这可能更长一些和复杂一点,它允许任意数量的参数。

当然,当你有美食时,比如某种汤,你要分享它给大家。我这里有一个列表,列出了一些最有用bash别名和函数的。注意“最有用的”只是个说法,别名的是否有用要看你是否每天都需要在 shell 里面用它。

提升生产力

这些别名真的很简单并且真的很短,但他们大多数是为了给你的生命节省几秒钟,最终也许为你这一辈子节省出来几年,也许呢。

alias ls=”ls –color=auto”

简单但非常重要。使ls命令带着彩色输出。

alias ll=”ls –color -al”

以彩色的列表方式列出目录里面的全部文件。

alias grep=’grep –color=auto’

类似,只是在grep里输出带上颜色。

mcd() { mkdir -p “ 1";cd" 1”;}

我的最爱之一。创建一个目录并进入该目录里: mcd [目录名]。

cls() { cd “$1”; ls;}

类似上一个函数,进入一个目录并列出它的的内容:cls[目录名]。

backup() { cp “$1”{,.bak};}

alias c=”clear”

清除你终端屏幕不能更简单了吧?

alias histg=”history | grep”

快速搜索你的命令输入历史:histg [关键字]

alias ..=’cd ..’

回到上层目录还需要输入 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
}

很长,但是也是最有用的。解压任何的文档类型:extract: [压缩文件]

系统信息

alias cmount=”mount | column -t”

按列格式化输出mount信息。

alias tree=”ls -R | grep “: "|sedes/: //’ -e ‘s/[^-][^/]*//–/g’ >-e ‘s/^/ /’ -e ‘s/-/|/’”

以树形结构递归地显示目录结构。

sbs() { du -b –max-depth 1 | sort -nr | perl -pe ‘s{([0-9]+)}>>{sprintf “%.1f%s”, 1>=230?( 1/2**30, “G”): 1>=220?>( 1/2**20, “M”): 1>=210?( 1/2**10, “K”): ($1, “”)}e’;}

安装文件在磁盘存储的大小排序,显示当前目录的文件列表。

alias intercept=”sudo strace -ff -e trace=write -e write=1,2 >-p”

接管某个进程的标准输出和标准错误。注意你需要安装了 strace。

alias meminfo=’free -m -l -t’

查看你还有剩下多少内存。

alias ps? = “ps aux | grep”

可以很容易地找到某个进程的PID:ps? [名字]。

alias volume=”amixer get Master | sed ‘1,4 d’ | cut -d [ -f 2 | cut -d ] -f 1”

显示当前音量设置。

网络

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

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

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

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///’}

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

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

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

getlocation() { lynx -dump 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’”

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

总结

#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];thencase 1 in
*.tar.bz2) tar xjf 1;;.tar.gz)tarxzf 1 ;;
*.bz2) bunzip2 1;;.rar)unrare 1 ;;
*.gz) gunzip 1;;.tar)tarxf 1 ;;
*.tbz2) tar xjf 1;;.tgz)tarxzf 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 “: "|sedes/: //’ -e ‘s/[^-][^/]*//–/g’ -e ‘s/^/ /’ -e ‘s/-/|/’”
sbs(){ du -b –max-depth 1 | sort -nr | perl -pe ‘s{([0-9]+)}{sprintf “%.1f%s”, 1>=230?( 1/2**30, “G”): 1>=220?( 1/2**20, “M”): 1>=210?( 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///’}
alias ipinfo=”curl ifconfig.me && curl ifconfig.me/host”
getlocation() { lynx -dump 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”


参考文献

http://begeek.cn/post/5379.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值