前/次の兄弟ディレクトリにCDを移動するには?

前/次の兄弟ディレクトリにCDを移動するには?

私はしばしばこのようなプロジェクトディレクトリのレイアウトを持っています。

project
`-- component-a
|   `-- files...
`-- component-b
|   `-- files...
`-- component-c
    `-- files...

component私は通常、ディレクトリの1つで作業します。ディレクトリにファイルがあるためです。シェルに戻るときは、通常兄弟ディレクトリに移動します。特に、各コンポーネントにスクリプトできない変更を適用する必要がある場合は、さらにそうです。このような場合には、私が働く前の兄弟や次の兄弟にも興味がありません。

コマンドを定義するprevか、next単にcd前または次のディレクトリに(アルファベット順にまたはその他に)移動できますか?cd ../com<TAB><Arrow keys>常にタイピングするのがやや昔ながらになっているからです。

答え1

Commandlinefuソリューションを使用しないでくださいもう一つの答え:安全ではなく非効率的です。代わりにbash。これを継続的に作成するには組み込まれていて.bashrc簡単なので、グローバルシーケンスを使用しています。一般的にグローバル注文はいただし、ほとんどのロケールではアルファベット順にソートされます。移動する次または前のディレクトリがない場合は、エラーメッセージが表示されます。具体的には、ルートディレクトリでまたはnext試してみるとエラーが表示されます。prev/

## bash and zsh only!
# functions to cd to the next or previous sibling directory, in glob order

prev () {
    # default to current directory if no previous
    local prevdir="./"
    local cwd=${PWD##*/}
    if [[ -z $cwd ]]; then
        # $PWD must be /
        echo 'No previous directory.' >&2
        return 1
    fi
    for x in ../*/; do
        if [[ ${x#../} == ${cwd}/ ]]; then
            # found cwd
            if [[ $prevdir == ./ ]]; then
                echo 'No previous directory.' >&2
                return 1
            fi
            cd "$prevdir"
            return
        fi
        if [[ -d $x ]]; then
            prevdir=$x
        fi
    done
    # Should never get here.
    echo 'Directory not changed.' >&2
    return 1
}

next () {
    local foundcwd=
    local cwd=${PWD##*/}
    if [[ -z $cwd ]]; then
        # $PWD must be /
        echo 'No next directory.' >&2
        return 1
    fi
    for x in ../*/; do
        if [[ -n $foundcwd ]]; then
            if [[ -d $x ]]; then
                cd "$x"
                return
            fi
        elif [[ ${x#../} == ${cwd}/ ]]; then
            foundcwd=1
        fi
    done
    echo 'No next directory.' >&2
    return 1
}

¹可能なすべてのディレクトリ名を処理するわけではありません。 出力を解析することはls決して安全ではありません。

²cd非常に効率的である必要はありませんが、6つのプロセスはやや過度です。

答え2

次の機能を使用すると、兄弟ディレクトリを変更できます(bash機能)。

function sib() {
    ## sib  search sibling directories 
    ##   prompt for choice (when two or more directories are found, current dir is removed from choices) 
    ##   change to directory after selection 
    local substr=$1
    local curdir=$(pwd)
    local choices=$(find .. -maxdepth 1 -type d -name "*${substr}*" | grep -vE '^..$' | sed -e 's:../::' | grep -vE "^${curdir##*/}$" | sort)
    if [ -z "$choices" ]; then
        echo "Sibling directory not found!"
        return
    fi
    local count=$(echo "$choices" | wc -l)
    if [[ $count -eq 1 ]]; then
        cd ../$choices
        return 
    fi
    select dir in $choices; do
        if [ -n "$dir" ]; then
            cd ../$dir
        fi
        break
    done
}

使用例:

$ tree
  .
  ├── component-aaa-01
  ├── component-aaa-02
  ├── component-bbb-01
  ├── component-bbb-02
  ├── component-ccc-01
  ├── component-ccc-02
  └── component-ccc-03
  7 directories, 0 files
  $ cd component-aaa-01/
  $ sib bbb-01
  $ pwd
  component-bbb-01
  $ sib bbb
  $ pwd
  component-bbb-02
  $ sib ccc
  1) component-ccc-01
  2) component-ccc-02
  3) component-ccc-03
  #? 3
  $ pwd
  component-ccc-03
  $ sib 01
  1) component-aaa-01
  2) component-bbb-01
  3) component-ccc-01
  #? 2
  $ pwd
  component-bbb-01

答え3

ヒントを見つけました。コマンドライン fu.com。見つけやすくするためにここに再公開し、next使用しながら説明とコマンドを追加します。

alias prev='cd ../"$(ls -F .. | grep '/' | grep -B1 -xF "${PWD##*/}/" | head -n 1)"'
alias next='cd ../"$(ls -F .. | grep '/' | grep -A1 -xF "${PWD##*/}/" | tail -n 1)"'

魔法は「$(...)」ブロックにあります。次のように、パイプを介して互いにいくつかのコマンドを渡します。

ls -F .. |   # list items in parent dir; `-F` requests filetype indicators
grep '/' |   # select the directories (written as  `mydir/`)
grep -B1 -xF "${PWD##*/}/" |   # search for the name of the current directory in the output;
                               # print it and the line preceding it
head -n 1    # the first of those two lines contains the name of the previous sibling

関連情報