ファイルパラメータをインポートしてそのディレクトリに移動しますか? [コピー]

ファイルパラメータをインポートしてそのディレクトリに移動しますか? [コピー]

ファイル引数を受け入れ、引数のファイル部分を完全に無視してインクルードディレクトリに移動するUnixコマンドまたはシェルスクリプトはありますか?

ベストアンサー1

次の関数を作成できます。

cd_to_file_location() {                          
    location=$(dirname -- "$1")
    cd -- "$location"
}

# test it out
$ mkdir /tmp/foodir
$ touch /tmp/foodir/a
$ cd_to_file_location /tmp/foodir/a 
$ pwd
/tmp/foodir

おすすめ記事