3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Mac】ターミナルで現在のディレクトリと同じパスをコピーする方法

Last updated at Posted at 2019-06-19

やりたいこと

  • 現在のディレクトリと同じ階層の新規ターミナルを開きたい時がある
  • それをコマンドだけでできるようにする

方法

  1. ターミナルを開き任意のディレクトリに移動する
  2. コマンド pwd|pbcopy を入力して実行
  3. 新規セッションでターミナルを開く
  4. コマンド cd と入力し、cmd + v を実行

なぜできるのか

  • pwd コマンドは現在のディレクトリパスを出力するコマンド
  • pbcopy コマンドはターミナルの出力結果をクリップボードにコピーするコマンド
  • cmd1 | cmd2 パイプ |cmd1コマンドの出力結果をcmd2のコマンドに渡すことができる
  • pwd で出力した現在のディレクトリパスを pbcopy に渡すことで現在のディレクトリパスをクリップボードにコピーできる

ついでにエイリアスに登録しておく

いちいち pwd|pbcopy と入力するのは面倒臭いので、 pwdcp とでも登録しておく

  1. ~/.bashrc または ~/.bash_profile ファイルを開く
  2. alias pwdcp="pwd|pbcopy" と入力して保存
  3. source .bashrc または source .bash_profile で変更を適用する

これでpwdcp コマンドで現在のディレクトリパスをコピーできるようになる

ちなみに

  • pbpaste というクリップボードの内容をターミナルにペーストするコマンドもある
3
5
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
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?