自製好用的linux 快速最愛目錄

在Windows 都會有一些可以快速切換目錄的軟體,但我在linux下都沒用過,雖然有pushd跟popd,但還是無法滿足我的需求。我希望像windows那樣可以按下某個快速鍵之後就切換到某個目錄去,其實主要原因是因為android build system路徑都很長~

寫了下面的script,放到.bashrc or /etc/bash.bashrc,這樣就可以使用 cc 會顯示最愛目錄,cc 1直接切換到最愛目錄一,cca可以把目前目錄加到最愛目錄,ccr可以從最愛目錄中把目前目錄刪除。

pushd看自己要不要用,後來覺得會印出太多訊息就mark掉了,ccr因為我有設定  set -o noclobber,所以要使用>| 才能覆寫檔案~

export FavoriteDir=~/FavoriteDir

function cchelp()
{
   echo cc number
   echo cca — add PWD to favorite folder
   echo ccr — remove PWD from favorite folder
   echo ccs — sort all the favorite folder
   echo cch — push PWD and change to new folder
   echo ccb — backup favorite folder
   echo ccp — back to last folder
}
function cc()
{
    if [ -z $1 ]
    then
    cat -n $FavoriteDir
    echo "Please select the target folder:"
          read DirIndex
    else
        DirIndex=$1
    fi
    echo $DirIndex | grep "^[0-9]\$" > /dev/null
    if [ $? -eq 0 ]
    then
#use index
        TargetDir=`cat $FavoriteDir | sed -n "$DirIndex"p`
    else
#use keyword, maybe be multi match, so we select first match
        TargetDir=`sed "s|$DirIndex|$DirIndex|p" $FavoriteDir | sed -n 1p`
    fi
    pushd -n `pwd` > /dev/null
    cd "$TargetDir"
}
function cca()
{
   IsFind=`grep $PWD $FavoriteDir`
   if [ "$IsFind" != "$PWD" ]
   then
      echo $PWD >> $FavoriteDir
      echo "’$PWD’ was added."
   fi
}
function ccr()
{
   #replace the pwd to empty line
   newfile=`sed "s|^"$PWD"$||gp" $FavoriteDir`
   echo $newfile >| $FavoriteDir
   #remove the empty line from file
   #sed ‘/^$/d’ $FavoriteDir >| $FavoriteDir
}

function ccs()
{
   sort $FavoriteDir -o $FavoriteDir
   cat $FavoriteDir
}

function ccb()
{
   cp $FavoriteDir $FavoriteDir.bak
   echo backup to "$FavoriteDir".bak
}

2010/01/22 更新程式,主要加入排序備份等功能,且cc的參數可以吃數字也可以吃關鍵字,假設你最愛目錄裡面有fakeInputDevice,那我打 cc fake 就可以直接進去~

This entry was posted in 工作. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s