LoginSignup
3
1

More than 3 years have passed since last update.

たくさんの連番のディレクトリがある時,次のディレクトリに移動するコマンド

Last updated at Posted at 2017-07-27

背景

連番のディレクトリがあり,順番にディレクトリを移動して処理しなければならない場合がある.
その処理が定型的なものであれば,シェルスクリプトで対応できるが,各ディレクトリ内で,人手で処理しなければならない場合もある.
その場合,ディレトリの数が多ければ次のディレクトリがよくわからなくなる.
じゃあ,次のディレクトリに移動するコマンドを作れば良いと思って,作ってみた.

改善版

getnextdir

以前のやり方だと,.bash_profileに結構な量のスクリプトがあったので,それを別のシェルスクリプトに追い出した.
下のスクリプトをパスの通ったところに保存する.

getnextdir
#! /bin/sh

cwd=$(pwd)
current="../${cwd##*/}"

previous=FALSE

while getopts p OPT
do
  case $OPT in
    "p" ) previous=TRUE ;;
  esac
done

next=0
nextdir=$current

for i in $(find .. -maxdepth 1 -mindepth 1 -type d | sort)
do
    if [ $previous = TRUE ]; then
        if [ "$current" = "$i" ]; then
            break
        fi
        nextdir=$i
    else
        if [ $next -eq 0 ]; then
            if [ "$current" = "$i" ] ; then
                next=1
            fi
        elif [ $next -eq 1 ]; then
            nextdir=$i
            break
        fi
    fi
done

if [ "$nextdir" = "$current" ] ; then
    echo $current/..
    exit 1
else
    echo $nextdir
    exit 0
fi

.bash_profile

次に,.bash_profileに下のスクリプトを追加する.

.bash_profile
nextdir(){
    next=$(getnextdir)
    if [ $? -eq 0 ]; then
        cd $next
    else
        echo "Done"
        cd $next
    fi
    pwd
}

prevdir(){
    next=$(getnextdir -p)
    if [ $? -eq 0 ]; then
        cd $next
    else
        echo "Done"
        cd $next
    fi
    pwd
}

注意点

  • シェルスクリプトとして作成しようとしても実現できません.
    • 別のシェル環境内で移動し,そのシェルスクリプトが終了すると元の環境(元のディレクトリのまま)に戻ります.
    • 実現方法を探していると, source.)を使ってスクリプトを読み込めという記述が散見されるものの,目的が違うのでここでは採用していません.

参考サイト

以前のバージョン

次のディレクトリに移動する

たくさんの連番のディレクトリがあり,一つずつ中に移動して確認して,次のディレクトリに移動して...と繰り返していると,次のディレクトリが何かがわからなくなってくる.
次のディレクトリに移動するコマンドがあれば良いと思い,作ってみた.

.bash_profile
nextdir () {
    cwd=$(pwd)
    current="../${cwd##*/}"

    next=0
    nextdir=$current
    for i in $(find .. -maxdepth 1 -mindepth 1 -type d)
    do
        if [ $next -eq 0 ]; then
            if [ "$current" = "$i" ] ; then
                next=1
            fi
        elif [ $next -eq 1 ]; then
            nextdir=$i
            break
        fi
    done
    if [ "$nextdir" = "$current" ] ; then
        echo "Done"
        cd ..
    else
        cd $nextdir
    fi
    pwd
}

この関数を~/.bash_profile~/.bashrcに保存して次のコマンドを入力するとnextdirコマンドが有効になる.

$ . ~/.bash_profile

前のディレクトリに移動する.

同様に次のようにすることで,前のディレクトリに移動するコマンドも実現可能.

.bash_profile
prevdir () {
    cwd=$(pwd)
    current="../${cwd##*/}"

    prev=0
    prevdir=$current
    for i in $(find .. -maxdepth 1 -mindepth 1 -type d)
    do
        if [ "$current" = "$i" ] ; then
            break
        fi
        prevdir=$i
    done
    if [ "$prevdir" = "$current" ] ; then
        echo "Done"
        cd ..
    else
        cd $prevdir
    fi
    pwd
}
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1