一波實用的Bash Shell整理

來源:互聯網
上載者:User

一波實用的Bash Shell整理

   這篇文章主要介紹了一波實用的Bash Shell整理,適用於Ubuntu等Debian系的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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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: [壓縮檔]

  系統資訊

  想儘快地知道關於你的系統一切資訊?

  ?

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

  大概的顯示你的Google郵件裡未讀郵件的數量: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。(如果你已經一行一行的複製到這裡了,哈哈,你發現你又浪費了生命的幾秒鐘~)

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

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

 

#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\""

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.