提高Linux工作效率的bash技巧

看的這篇文章 備份地址,感覺有效果的是下面幾個。

幫你保持歷史操作,跳回到你經常使用的目錄。

下面是我的配置文件里腳本:

# USAGE:
# s bookmarkname - saves the curr dir as bookmarkname
# g bookmarkname - jumps to the that bookmark
# g b[TAB] - tab completion is available
# l - list all bookmarks

# save current directory to bookmarks
touch ~/.sdirs
function s {
  cat ~/.sdirs | grep -v "export DIR_$1=" > ~/.sdirs1
  mv ~/.sdirs1 ~/.sdirs
  echo "export DIR_$1=$PWD" >> ~/.sdirs
}

# jump to bookmark
function g {
  source ~/.sdirs
  cd $(eval $(echo echo $(echo \$DIR_$1)))
}

# list bookmarks with dirnam
function l {
  source ~/.sdirs
  env | grep "^DIR_" | cut -c5- | grep "^.*="
}
# list bookmarks without dirname
function _l {
  source ~/.sdirs
  env | grep "^DIR_" | cut -c5- | grep "^.*=" | cut -f1 -d "="
}

# completion command for g
function _gcomp {
    local curw
    COMPREPLY=()
    curw=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=($(compgen -W '`_l`' -- $curw))
    return 0
}

# bind completion command for g to _gcomp
complete -F _gcomp g

創(chuàng)建自己的命令包.

通過腳本,我可以將ssh key拷貝到任何網站服務器——只需要鍵入dur keyuser@somehost.

function dur {
  case $1 in
  clone|cl)
    git clone git@bitbucket.org:nicolapaolucci/$2.git
    ;;
  move|mv)
    git remote add bitbucket git@bitbucket.org:nicolapaolucci/$(basename $(pwd)).git
    git push --all bitbucket
    ;;
  trackall|tr)
    #track all remote branches of a project
    for remote in $(git branch -r | grep -v master ); do git checkout --track $remote ; done
    ;;
  key|k)
    #track all remote branches of a project
    ssh $2 'mkdir -p .ssh && cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub
    ;;
  fun|f)
    #list all custom bash functions defined
    typeset -F | col 3 | grep -v _ | xargs | fold -sw 60
    ;;
  def|d)
    #show definition of function $1
    typeset -f $2
    ;;
  help|h|*)
    echo "[dur]dn shell automation tools"
    echo "commands available:"
    echo " [cl]one, [mv|move]"
    echo " [f]fun lists all bash functions defined in .bashrc"
    echo " [def] <fun> lists definition of function defined in .bashrc"
    echo " [k]ey <host> copies ssh key to target host"
    echo " [tr]ackall], [h]elp"
    ;;
  esac
}

## 1.快速跳轉命令 一 z

要是每次都要進入一個目錄很深的文件夾下,像下面這樣:

# cd /root/py/auto/fabric

每次都要輸入好多個目錄名是不是很煩躁,下面有一個非常方便的操作可以取代它 一 z 命令

[圖片上傳失敗...(image-d45daf-1529634654215)]

z 的源碼在這里:https://github.com/rupa/z/blob/master/z.sh

你只需要把源碼復制到用戶目錄下的 z.sh 文件,然后在 .bashrc 這個文件的最后添加 “source /path/to/z.sh”,最后使用:

# source z.sh 

或者

# . .bashrc

就可以生效了,要跳轉到一個目錄,直接使用 z + 目錄名(此目錄必須是以前進入過的)。

z 這個腳本會為你每次進入的目錄分配一個權重,然后根據(jù)權重調到你所輸入的目錄中。

[圖片上傳失敗...(image-bef7f2-1529634654215)]

作者:田飛雨
鏈接:http://www.itdecent.cn/p/a6bc23bb1f15
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯(lián)系作者獲得授權并注明出處。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容