1
0

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.

Linuxのディレクトリ移動について

Last updated at Posted at 2018-08-05

はじめに

Linuxサーバーにsshして作業をするときは、頻繁にディレクトリを移動することになるかと思いますが、最近新しい移動コマンドを覚えましたので以下に記載します。

cdコマンドの基本

基本的には、以下の通りcdコマンドの引数に移動したいディレクトリを指定します。
pwdコマンドは現在位置するディレクトリを表示するコマンドです。

[shota@hostname ~]$ pwd
/home/hoshis
[shota@hostname ~]$ cd /var/tmp/
[shota@hostname tmp]$ pwd
/var/tmp

上記の通り、基本的には移動するディレクトリを指定すれば良いのですが
何回も移動するときに毎回ディレクトリを指定するのは少し煩雑です。

popd、pushd

こちらが、はじめにで触れていたコマンドのpopdpushdです。

①先ずは、pushd

[shota@hostname ~]$ pwd
/home/shota
[shota@hostname ~]$ pushd /var/tmp/
/var/tmp ~
[shota@hostname tmp]$ pwd
/var/tmp
[shota@hostname tmp]$ pushd /usr/bin/
/usr/bin /var/tmp ~
[shota@hostname bin]$ pwd
/usr/bin

使い方はcdコマンドと同様に移動したいディレクトリを引数に指定するとそのディレクトリに移動できます。

②次は、popd

[shota@hostname bin]$ pwd
/usr/bin
[shota@hostname bin]$ popd
/var/tmp ~
[shota@hostname tmp]$ pwd
/var/tmp

popdで前回移動したディレクトリに、戻ることができます。上手く使えば、毎回ディレクトリを指定する手間が省けるため便利です。

おまけ

cdコマンドは引数を指定しないで実行するとユーザーのホームディレクトリへ移動します。

[shota@hostname tmp]$ pwd
/var/tmp
[shota@hostname tmp]$ cd
[shota@hostname ~]$ pwd
/home/shota

また、cd -といったように"-"(ハイフン)を引数にすると、以前いたディレクトリに移動できます。
@tenmyoさん、補足いただきありがとうございます。

[shota@hostname tmp]$ pwd
/var/tmp
[shota@hostname tmp]$ cd
[shota@hostname ~]$ pwd
/home/shota
[shota@hostname ~]$ cd -
/var/tmp
[shota@hostname tmp]$ pwd
/var/tmp
1
0
4

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?