With a directory stack, we can push the directories we visit sequentially into the directory stack, and when we’ve done a series of operations, we can go all the way back to the origin based on the record of the directory stack.

Directory Stack Commands

The directory stack provides us with three available commands.

dirs

Displays all directories recorded in the directory stack:

1
help dirs
img

Check dirs's definition and usage

pushd

Records the specified directory to the directory stack and switches the working directory to the recorded directory:

1
help pushd
img

Check pushd's definition and usage

popd

Remove the last directory recorded in the directory stack and switch the working directory to the removed directory:

1
help popd
img

Check popd's definition and usage

Preparing Directories

Let’s prepare some directories for demonstration:

1
2
mkdir -p a/b/c/d/e/f/g
tree a
img

Create nested directories and show their tree structure

Now that we have some nested directories, we can begin to demonstrate. The directory stack can store any directory we push into, and the nested directory is used here just for ease of operation.

Use of Commands

Adding Directories

Let’s adding directories to directory stack:

1
2
3
4
5
6
7
8
9
cd ~
dirs
pushd a
pushd b
pushd c
pushd d
pushd e
pushd f
pushd g
img

Switch to user's home directory, check to make sure that dirs only shows the tilde(~) and start pushing the nested directories into directory stack

Show Recorded Directories

By Default

Use dirs to print the recorded directories:

1
dirs
img

Show the directory stack recorded directories

With User Home Directory Name

Use dirs -l to print the recorded directories with user home directory’s name instead of a tilde:

1
dirs -l
img

Show the directory stack recorded directories with user home name instead of a tilde

Line by Line

Use dirs -p to print the recorded directories line by line:

1
dirs -p
img

Show the directory stack recorded directories line by line

Line by Line With Their Indexes

Use dirs -v to print the recorded directories line by line with their indexes:

1
dirs -v
img

Show the directory stack recorded directories line by line with their indexes

First Recorded Directory

Use dirs -0 to print the first recorded directory:

1
dirs -0
img

Show the first recorded directory

Last Recorded Directory

Use dirs +0 to print the last recorded directory:

1
dirs +0
img

Show the last recorded directory

Use Recorded Directories

Use From right

Use pushd -0 to change to the rightmost directory and use it as a working directory:

1
pushd -0
img

Use the first recorded directory

Use From left

Use pushd +1 to change to the directory before the leftmost one and use it as a working directory:

1
pushd +1
img

Use the previous recorded directory

Remove Recorded Directories

Remove From right

Use popd -0 to remove the rightmost directory:

1
popd -0
img

Remove the first recorded directory

Remove From left

Use popd +0 to remove the leftmost directory and use the previous directory as a working directory:

1
popd +0
img

Remove the last recorded directory and use the previous recorded directory as the working directory

References 6.8.1 Directory Stack Builtins

Buy me a coffeeBuy me a coffee