Skip to content

pushd

Changes to a directory and remembers the previous one on a stack, so you can jump back later.

pushd output

Syntax

pushd [-n] [DIRECTORY | +N | -N]

Options

Option / argument Description
DIRECTORY Put DIRECTORY on top of the stack and change to it
(none) Swap the two top entries and change to the new top
+N Rotate the stack so that entry N (counted from the left, starting at 0) is on top
-N Same as +N, but counted from the right
-n Only change the stack, do not change the directory

The stack is shown with dirs: dirs -v numbers the entries, dirs -c clears the stack.

Examples

Change to a directory and remember where you came from:

pushd /etc/nginx
/etc/nginx ~

The output is the stack: the current directory on the left, the remembered ones to the right of it.

Add another directory:

pushd /var/log/nginx
/var/log/nginx /etc/nginx ~

Show the stack with numbers:

dirs -v
 0  /var/log/nginx
 1  /etc/nginx
 2  ~

Switch between the two top directories:

pushd
/etc/nginx /var/log/nginx ~

Jump to a specific entry of the stack:

pushd +2
~ /etc/nginx /var/log/nginx

Add a directory to the stack without changing into it:

pushd -n /tmp
~ /tmp /etc/nginx /var/log/nginx

When do I need it?

  • When you work in several places at once, for example editing a config in /etc/nginx and checking the result in /var/log/nginx
  • In scripts that briefly work in another directory and then return to where they started (together with popd)
  • When cd - is not enough because you need more than two directories

pushd without arguments

pushd alone swaps the two top directories. It works like cd -, but the other remembered directories stay on the stack.

The stack only lives in the current shell

The directory stack is lost when you close the shell or the SSH session. Every shell and every script has its own stack.

cd · pwd · popd