歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 分享有用的 bash 別名和函數

分享有用的 bash 別名和函數

日期:2017/2/28 14:24:22   编辑:Linux教程

作為一個命令行探索者,你或許發現你自己一遍又一遍重復同樣的命令。如果你總是用ssh進入到同一台電腦,如果你總是將一連串命令連接起來,如果你總是用同樣的參數運行一個程序,你也許希望在這種不斷的重復中為你的生命節約下幾秒鐘。

解決方案是使用一個別名(alias)。正如你可能知道的,別名用一種讓你的shell記住一個特定的命令並且給它一個新的名字的方式。不管怎麼樣,別名有一些限制,它只是shell命令的快捷方式,不能傳遞或者控制其中的參數。所以作為補充,bash 也允許你創建你自己的函數,這可能更長一些和復雜一點,它允許任意數量的參數。

當然,當你有美食時,比如某種湯,你要分享它給大家。我這裡有一個列表,列出了一些最有用bash別名和函數的。注意“最有用的”只是個說法,別名的是否有用要看你是否每天都需要在 shell 裡面用它。

在你開始你的別名體驗之旅前,這裡有一個便於使用的小技巧:如果你的別名和原本的命令名字相同,你可以用如下技巧來訪問原本的命令(LCTT 譯注:你也可以直接原本命令的完整路徑來訪問它。)

  1. \command

例如,如果有一個替換了ls命令的別名 ls。如果你想使用原本的ls命令而不是別名,通過調用它:

  1. \ls

提升生產力

這些別名真的很簡單並且真的很短,但他們大多數是為了給你的生命節省幾秒鐘,最終也許為你這一輩子節省出來幾年,也許呢。

  1. alias ls="ls --color=auto"

簡單但非常重要。使ls命令帶著彩色輸出。

  1. alias ll="ls --color -al"

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

  1. alias grep='grep --color=auto'

類似,只是在grep裡輸出帶上顏色。

  1. mcd(){ mkdir -p "$1"; cd "$1";}

我的最愛之一。創建一個目錄並進入該目錄裡: mcd [目錄名]。

  1. cls(){ cd "$1"; ls;}

類似上一個函數,進入一個目錄並列出它的的內容:cls[目錄名]。

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

簡單的給文件創建一個備份: backup [文件] 將會在同一個目錄下創建 [文件].bak。

  1. md5check(){ md5sum "$1"| grep "$2";}

因為我討厭通過手工比較文件的md5校驗值,這個函數會計算它並進行比較:md5check[文件][校驗值]。

  1. alias makescript="fc -rnl | head -1 >"

很容易用你上一個運行的命令創建一個腳本:makescript [腳本名字.sh]

  1. alias genpasswd="strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo"

只是瞬間產生一個強壯的密碼。

  1. alias c="clear"

清除你終端屏幕不能更簡單了吧?

  1. alias histg="history | grep"

快速搜索你的命令輸入歷史:histg [關鍵字]

  1. alias..='cd ..'

回到上層目錄還需要輸入 cd 嗎?

  1. alias...='cd ../..'

自然,去到上兩層目錄。

  1. extract(){
  2. if[-f $1 ];then
  3. case $1 in
  4. *.tar.bz2) tar xjf $1 ;;
  5. *.tar.gz) tar xzf $1 ;;
  6. *.bz2) bunzip2 $1 ;;
  7. *.rar) unrar e $1 ;;
  8. *.gz) gunzip $1 ;;
  9. *.tar) tar xf $1 ;;
  10. *.tbz2) tar xjf $1 ;;
  11. *.tgz) tar xzf $1 ;;
  12. *.zip) unzip $1 ;;
  13. *.Z) uncompress $1 ;;
  14. *.7z)7z x $1 ;;
  15. *) echo "'$1' cannot be extracted via extract()";;
  16. esac
  17. else
  18. echo "'$1' is not a valid file"
  19. fi
  20. }

很長,但是也是最有用的。解壓任何的文檔類型:extract: [壓縮文件]

系統信息

想盡快地知道關於你的系統一切信息?

  1. alias cmount="mount | column -t"

按列格式化輸出mount信息。

  1. alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'"

以樹形結構遞歸地顯示目錄結構。

  1. 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';}

安裝文件在磁盤存儲的大小排序,顯示當前目錄的文件列表。

  1. alias intercept="sudo strace -ff -e trace=write -e write=1,2 -p"

接管某個進程的標准輸出和標准錯誤。注意你需要安裝了 strace。

  1. alias meminfo='free -m -l -t'

查看你還有剩下多少內存。

  1. alias ps?="ps aux | grep"

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

  1. alias volume="amixer get Master | sed '1,4 d' | cut -d [ -f 2 | cut -d ] -f 1"

顯示當前音量設置。

網絡

對於所有用在互聯網和本地網絡的命令,也有一些神奇的別名給它們。

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

下載整個網站:websiteget [URL]。

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

顯示出哪個應用程序連接到網絡。

  1. alias port='netstat -tulanp'

顯示出活動的端口。

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

大概的顯示你的谷歌郵件裡未讀郵件的數量:gmail [用戶名]

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

獲得你的公網IP地址和主機名。

  1. 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 's\ip address flag \\'|sed 's\My\\';}

返回你的當前IP地址的地理位置。

也許無用

所以呢,如果一些別名並不是全都具有使用價值?它們可能仍然有趣。

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

繪制內核模塊依賴曲線圖。需要可以查看圖片。

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

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

最後,這些別名和函數的很大一部分來自於我個人的.bashrc。而那些令人點贊的網站 alias.sh和commandlinefu.com我早已在我的帖子best online tools for Linux 裡面介紹過。你可以去看看,如果你願意,也可以分享下你的。也歡迎你在這裡評論,分享一下你的智慧。

做為獎勵,這裡有我提到的全部別名和函數的純文本版本,隨時可以復制粘貼到你的.bashrc。(如果你已經一行一行的復制到這裡了,哈哈,你發現你又浪費了生命的幾秒鐘~)

  1. #Productivity
  2. alias ls="ls --color=auto"
  3. alias ll="ls --color -al"
  4. alias grep='grep --color=auto'
  5. mcd(){ mkdir -p "$1"; cd "$1";}
  6. cls(){ cd "$1"; ls;}
  7. backup(){ cp "$1"{,.bak};}
  8. md5check(){ md5sum "$1"| grep "$2";}
  9. alias makescript="fc -rnl | head -1 >"
  10. alias genpasswd="strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo"
  11. alias c="clear"
  12. alias histg="history | grep"
  13. alias..='cd ..'
  14. alias...='cd ../..'
  15. extract(){
  16. if[-f $1 ];then
  17. case $1 in
  18. *.tar.bz2) tar xjf $1 ;;
  19. *.tar.gz) tar xzf $1 ;;
  20. *.bz2) bunzip2 $1 ;;
  21. *.rar) unrar e $1 ;;
  22. *.gz) gunzip $1 ;;
  23. *.tar) tar xf $1 ;;
  24. *.tbz2) tar xjf $1 ;;
  25. *.tgz) tar xzf $1 ;;
  26. *.zip) unzip $1 ;;
  27. *.Z) uncompress $1 ;;
  28. *.7z)7z x $1 ;;
  29. *) echo "'$1' cannot be extracted via extract()";;
  30. esac
  31. else
  32. echo "'$1' is not a valid file"
  33. fi
  34. }
  35. #System info
  36. alias cmount="mount | column -t"
  37. alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'"
  38. 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';}
  39. alias intercept="sudo strace -ff -e trace=write -e write=1,2 -p"
  40. alias meminfo='free -m -l -t'
  41. alias ps?="ps aux | grep"
  42. alias volume="amixer get Master | sed '1,4 d' | cut -d [ -f 2 | cut -d ] -f 1"
  43. #Network
  44. alias websiteget="wget --random-wait -r -p -e robots=off -U mozilla"
  45. alias listen="lsof -P -i -n"
  46. alias port='netstat -tulanp'
  47. gmail(){ curl -u "$1"--silent "https://mail.google.com/mail/feed/atom"| sed -e 's/<\/fullcount.*/\n/'| sed -e 's/.*fullcount>//'}
  48. alias ipinfo="curl ifconfig.me && curl ifconfig.me/host"
  49. 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 's\ip address flag \\'|sed 's\My\\';}
  50. #Funny
  51. kernelgraph(){ lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"'| dot -Tpng| display -;}
  52. alias busy="cat /dev/urandom | hexdump -C | grep \"ca fe\""

Copyright © Linux教程網 All Rights Reserved