收藏一些實用的 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 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’”

在那些非技術人員的眼裏你看起來是總是那麼忙和神祕。

總結

#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 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”


參考文獻

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章