pushd – Add a directory to the top of the directory stack

The pushd command is used to add a directory to the top of the directory stack. The directory stack is a list of directories that are stored in memory and can be accessed using the dirs command. The pushd command is often used in conjunction with the popd command to switch between directories in the stack.

Overview

The syntax of the pushd command is as follows:

pushd [options] [directory]

The pushd command takes one optional argument, which is the directory to be added to the top of the directory stack. If no directory is specified, the current working directory is added to the stack. The pushd command also takes a number of options, which are described in the next section.

When a directory is added to the stack, it becomes the current working directory and is displayed at the top of the stack when the dirs command is run. The previous current working directory is added to the stack below the new directory. The pushd command can be used to switch between directories in the stack by specifying a number as an argument. For example, pushd +2 would switch to the second directory in the stack.

Examples

Here are some examples of how to use the pushd command:

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

In the first example, the /var/log directory is added to the stack and becomes the current working directory. In the second example, the /etc directory is added to the stack and becomes the current working directory, with the previous directory (/var/log) added below it. In the third example, the second directory in the stack (/var/log) is made the current working directory.

Options

The pushd command has a number of options that can be used to modify its behavior. These options are summarized in the table below:

Option Description
-n Do not execute cd after adding directory to stack.
-q Quiet mode. Do not print directory stack after adding directory.

Troubleshooting tips

If you receive an error message when using the pushd command, it may be because the directory you are trying to add to the stack does not exist or you do not have permission to access it. Make sure that the directory exists and that you have the necessary permissions before trying to add it to the stack.

If you are having trouble switching between directories in the stack using the pushd command, try using the dirs command to see the current contents of the stack. You can then use the number of the directory in the stack to switch to it using the pushd command.

Notes

  • The pushd command is often used in conjunction with the popd command to switch between directories in the stack.
  • The dirs command can be used to display the current contents of the directory stack.