pushd, popd, dirs の基本的な使い方

pushd でスタックにディレクトリを追加する

pushdを使うと起こること。

  • cd を使ったときと同じようにディレクトリを変更する。
  • ディレクトリパスがディレクトリスタック(以降スタックと略記)に追加される。
  • スタックを表示する。

pushd を実行するたびに、スタックが増えていく。

 1[~/proj/temp]$ pushd python
 2~/proj/temp/python ~/proj/temp
 3[~/proj/temp/python]$ pushd ../sed
 4~/proj/temp/sed ~/proj/temp/python ~/proj/temp
 5[~/proj/temp/sed]$ pushd ../shell
 6~/proj/temp/shell ~/proj/temp/sed ~/proj/temp/python ~/proj/temp
 7[~/proj/temp/shell]$ pushd ../txt
 8~/proj/temp/txt ~/proj/temp/shell ~/proj/temp/sed ~/proj/temp/python ~/proj/temp
 9[~/proj/temp/txt]$ pushd ../
10~/proj/temp ~/proj/temp/txt ~/proj/temp/shell ~/proj/temp/sed ~/proj/temp/python ~/proj/temp
11[~/proj/temp]$

※プロンプトにカレントディレクトリを表示するよう設定している

最後の pushd では、ディレクトリ ~/proj/temp がすでにスタックにあるにもかかわらず、再び追加されることを示している。 また、スタックの一番左のエントリーは直近で追加されたもので、カレントディレクトリである。

dirs コマンド

スタックを表示するには、以下のように dirs を使用する。 スタックには影響せず、表示されるだけだ。

1[~/proj/temp]$ dirs
2~/proj/temp ~/proj/temp/txt ~/proj/temp/shell ~/proj/temp/sed ~/proj/temp/python ~/proj/temp
3[~/proj/temp]$

各ディレクトリの位置を数値で表示したい場合は、以下のように -v (vertical) オプションを使用する。

1[~/proj/temp]$ dirs -v
2 0  ~/proj/temp
3 1  ~/proj/temp/txt
4 2  ~/proj/temp/shell
5 3  ~/proj/temp/sed
6 4  ~/proj/temp/python
7 5  ~/proj/temp
8[~/proj/temp]$ 

チルダ ~ の代わりにホームディレクトリへのパスを表示したい場合は、 -l オプションを追加する。

1[~/proj/temp]$ dirs -v -l
2 0  /Users/dam/proj/temp
3 1  /Users/dam/proj/temp/txt
4 2  /Users/dam/proj/temp/shell
5 3  /Users/dam/proj/temp/sed
6 4  /Users/dam/proj/temp/python
7 5  /Users/dam/proj/temp
8[~/proj/temp]$

pushd でスタックをローテーションさせない方法

-n オプションを使うと、カレントディレクトリを変更せずにスタックに追加することができる。 カレントディレクトリが変わらないのでローテーションは発生しない。

以下に例を示す。 まず、現在のスタックを確認する。

1[~/proj/temp/txt]$ dirs -v -l
2 0  /Users/dam/proj/temp/txt
3 1  /Users/dam/proj/temp/shell
4 2  /Users/dam/proj/temp/sed
5 3  /Users/dam/proj/temp/python
6 4  /Users/dam/proj/temp
7[~/proj/temp/txt]$

ここで、 pushd に -n オプションを付けて ~/proj/temp をパラメータとして渡す。 そしてスタックをもう一度チェックする。

 1[~/proj/temp/txt]$ pushd -n ~/proj/temp
 2~/proj/temp/txt ~/proj/temp ~/proj/temp/shell ~/proj/temp/sed ~/proj/temp/python ~/proj/temp
 3[~/proj/temp/txt]$ dirs -v -l
 4 0  /Users/dam/proj/temp/txt
 5 1  /Users/dam/proj/temp
 6 2  /Users/dam/proj/temp/shell
 7 3  /Users/dam/proj/temp/sed
 8 4  /Users/dam/proj/temp/python
 9 5  /Users/dam/proj/temp
10[~/proj/temp/txt]$

~/proj/temp はスタックの 2 番目のエントリとして追加された。 スタックの一番上は常にカレントディレクトリなので、 一番上の位置を占めることはできない。 カレントディレクトリ ~/proj/temp/txt を離れなかったので、ローテーションは発生しない。

スタックの任意の位置のディレクトリに移動する

pushd で数値パラメータを使うと、 スタック内の任意のディレクトリに移動することができる。 移動するとスタックがローテーションする。 移動先として選んだディレクトリがスタックの最初のエントリーになる。

スタック内のディレクトリは位置番号で参照する。 スタックの上から数えても下から数えてもよい。 正の数、例えば +3 の場合は上から、負の数、例えば -2 の場合は下から数える。

 1[~/proj/temp/txt]$ dirs -v -l
 2 0  /Users/dam/proj/temp/txt
 3 1  /Users/dam/proj/temp
 4 2  /Users/dam/proj/temp/shell
 5 3  /Users/dam/proj/temp/sed
 6 4  /Users/dam/proj/temp/python
 7 5  /Users/dam/proj/temp
 8[~/proj/temp/txt]$ pushd +3
 9~/proj/temp/sed ~/proj/temp/python ~/proj/temp ~/proj/temp/txt ~/proj/temp ~/proj/temp/shell
10[~/proj/temp/sed]$ dirs -v -l
11 0  /Users/dam/proj/temp/sed
12 1  /Users/dam/proj/temp/python
13 2  /Users/dam/proj/temp
14 3  /Users/dam/proj/temp/txt
15 4  /Users/dam/proj/temp
16 5  /Users/dam/proj/temp/shell
17[ttys002 ~/proj/temp/sed]$

選択したディレクトリより上のスタックにあるディレクトリは、スタックの一番下に移動される。 選択したディレクトリがスタックの一番上になり、カレントディレクトリとなる。

スタックの一番下にあるディレクトリに移動したい場合は、 -0 を指定する。

 1[~/proj/temp/sed]$ pushd -0
 2~/proj/temp/shell ~/proj/temp/sed ~/proj/temp/python ~/proj/temp ~/proj/temp/txt ~/proj/temp
 3[~/proj/temp/shell]$ dirs -v -l
 4 0  /Users/dam/proj/temp/shell
 5 1  /Users/dam/proj/temp/sed
 6 2  /Users/dam/proj/temp/python
 7 3  /Users/dam/proj/temp
 8 4  /Users/dam/proj/temp/txt
 9 5  /Users/dam/proj/temp
10[~/proj/temp/shell]$

スタックの一番下のディレクトリが一番上に移動し、他のすべてのディレクトリがスタックの下方にずれる。

popd コマンド

スタックからディレクトリを削除するには popd を使う。

例えばスタックから 3 番の位置にあるディレクトリを削除するには、次のように popd を実行する。

 1[~/proj/temp/shell]$ dirs -v -l
 2 0  /Users/dam/proj/temp/shell
 3 1  /Users/dam/proj/temp/sed
 4 2  /Users/dam/proj/temp/python
 5 3  /Users/dam/proj/temp
 6 4  /Users/dam/proj/temp/txt
 7 5  /Users/dam/proj/temp
 8[~/proj/temp/shell]$ popd +3
 9~/proj/temp/shell ~/proj/temp/sed ~/proj/temp/python ~/proj/temp/txt ~/proj/temp
10[~/proj/temp/shell]$ dirs -v -l
11 0  /Users/dam/proj/temp/shell
12 1  /Users/dam/proj/temp/sed
13 2  /Users/dam/proj/temp/python
14 3  /Users/dam/proj/temp/txt
15 4  /Users/dam/proj/temp
16[~/proj/temp/shell]$

3 番のディレクトリが削除され、その下にあったものがそれぞれ一つ上に移動した。

pushd と同様に popd でもスタックの下から数えることができる。 スタックから最後のディレクトリを削除するには、次のように実行する。

1[~/proj/temp/shell]$ popd -0
2~/proj/temp/shell ~/proj/temp/sed ~/proj/temp/python ~/proj/temp/txt
3[~/proj/temp/shell]$ dirs -v -l
4 0  /Users/dam/proj/temp/shell
5 1  /Users/dam/proj/temp/sed
6 2  /Users/dam/proj/temp/python
7 3  /Users/dam/proj/temp/txt
8[~/proj/temp/shell]$

ディレクトリを変更し、何かをしてから元のディレクトリに戻るには、 pushd と popd を一緒に使えばよい。
( pushd を使って別のディレクトリに移動する。 popd を使ってスタックの一番上のディレクトリを削除し、 2 番目の位置にあるディレクトリに移動する。 この 2 番目のディレクトリは、 pushd で移動する前にいたディレクトリなので、結局元いたディレクトリに戻ることになる。)

1[~/proj/temp/shell]$ pushd /tmp
2/tmp ~/proj/temp/shell ~/proj/temp/sed ~/proj/temp/python ~/proj/temp/txt
3[/tmp]$ popd
4~/proj/temp/shell ~/proj/temp/sed ~/proj/temp/python ~/proj/temp/txt
5[~/proj/temp/shell]$

スタックに積まれている順に移動することを繰り返す

pushd +1 を繰り返し実行すると、スタックを何度も何度も循環して移動することができる。

例えば以下のように、ホームディレクトリから ~/proj/temp/python に到達するまで、各ディレクトリを降下していったとする。

 1[~]$ pushd proj
 2~/proj ~
 3[~/proj]$ pushd temp
 4~/proj/temp ~/proj ~
 5[~/proj/temp]$ pushd python
 6~/proj/temp/python ~/proj/temp ~/proj ~
 7[~/proj/temp/python]$ dirs -v -l
 8 0  /Users/dam/proj/temp/python
 9 1  /Users/dam/proj/temp
10 2  /Users/dam/proj
11 3  /Users/dam
12[~/proj/temp/python]$

pushd +1 を繰り返し実行すると、スタック全体を繰り返しローテーションするので、辿ってきたディレクトリを繰り返し移動することができる。

 1[~/proj/temp/python]$ pushd +1
 2~/proj/temp ~/proj ~ ~/proj/temp/python
 3[~/proj/temp]$
 4[~/proj/temp]$ pushd +1
 5~/proj ~ ~/proj/temp/python ~/proj/temp
 6[~/proj]$
 7[~/proj]$ pushd +1
 8~ ~/proj/temp/python ~/proj/temp ~/proj
 9[~]$
10[~]$ pushd +1
11~/proj/temp/python ~/proj/temp ~/proj ~
12[~/proj/temp/python]$

cd はスタックの一番上を上書きする

ディレクトリを変更するのに cd を使うと、スタックの一番上のディレクトリが上書きされる(スタックの一番上はカレントディレクトリのために確保されて)。 他のディレクトリのスタックにおける位置は変わらない。

 1[~/proj/temp/python]$ dirs -v -l
 2 0  /Users/dam/proj/temp/python
 3 1  /Users/dam/proj/temp
 4 2  /Users/dam/proj
 5 3  /Users/dam
 6[~/proj/temp/python]$ cd ~/proj/temp/awk
 7[~/proj/temp/awk]$ dirs -v -l
 8 0  /Users/dam/proj/temp/awk
 9 1  /Users/dam/proj/temp
10 2  /Users/dam/proj
11 3  /Users/dam
12[~/proj/temp/awk]$