pushd adds a directory to the top of the stack and popd removes a directory from the top of the stack.
To display directories in the directory stack (or history), we can use the dirs command as shown.
$ dirs OR $ dirs -v
– puts/ add directory paths onto a directory stack (history) and later allowing you to navigate back to any directory in history. While you add directories to the stack, it also echoes what’s existing in history (or “stack”).
The commands show how pushd works:
$ pushd /var/www/html/ $ pushd ~/Documents/ $ pushd ~/Desktop/ $ pushd /var/log/
From directory stack in the output above (directory index is in reverse order):
- /var/log is the fifth [index 0] in the directory stack.
- ~/Desktop/ is fourth [index 1].
- ~/Documents/ is third [index 2].
- /var/www/html/ is second [index 3] and
- ~ is first [index 4].
Optionally, we can use the directory index in the form
pushd +# or
pushd -# to add directories to the stack. To move into ~/Documents, we would type:
$ pushd +2
Note after this, the stack content will change. So from the previous example, to move into /var/www/html, we would use:
$ pushd +1
– removes a directory from the top of the stack or history. To list the directory stack, type:
To remove a directory from the directory stack indeed use
popd +# or
popd -#, in this case, we would type the command below to remove ~/Documents:
$ popd +1