pushd Command in Linux


The pushd command in Linux saves the current working directory and moves to a new directory. It is a built-in shell command that makes navigation and directory management easier. It saves the directories to a stack and operates on the last in, first out (LIFO) principle.

Table of Contents

Here is a comprehensive guide to the options available with the pushd command −

Syntax of pushd Command

The syntax of the pushd command in Linux is as follows −

pushd [dir] [options]

In the above syntax, the [dir] field is used to specify the directory that needs to be pushed to the stack. The [options] field is used to specify the options to change the positions of the directories in the stack.

pushd Command Options

The arguments of the Linux pushd command are listed below −

Option Description
+N Moves the Nth directory (counting from the left in dirs output, starting at zero) to the top of the stack by rotating it.
-N Moves the Nth directory (counting from the right in dirs output, starting at zero) to the top of the stack by rotating it.
-n Modifies the stack without changing the current directory, preventing automatic directory switching.

Examples of pushd Command in Linux

In this section, the usage of pushd command will be discussed with examples −

  • Switching to a Directory and Saving the Current Directory
  • Adding a Directory without Switching to a New Directory
  • Rotating the Directory Stack to the Left
  • Rotating the Directory Stack to the Right

Switching to a Directory and Saving the Current Directory

To switch to a new directory and save the current working directory, use the pushd command in the following manner −

pushd Documents

The current directory will be Documents while saving the current directory to the stack. To display the stack, use the following command −

dirs -v
pushd Command in Linux1

The ~ is equivalent to the /home/user directory.

Adding a Directory without Switching to a New Directory

To add a directory without switching to a new directory, use the -n argument with the pushd command −

pushd -n /etc

The above command will push the directory to the second spot, and the present directory remains unchanged at the first spot.

dirs -v
pushd Command in Linux2

Rotating the Directory Stack to the Left

To rotate the directory stack to the left, use the +N argument. Take a look at the following example.

Let’s add the following directories to the stack.

pushd /etc
pushd /usr
pushd /var

To list the stack, use the following command −

dirs -v

Executing the following command moves the directory at index 1 (/var) (0 starting index) to the top −

pushd +1

To verify, execute the dirs -v command −

pushd Command in Linux3

Rotating the Directory Stack to the Right

To rotate the directory stack to the right, use the -N argument. Take a look at the following example.

Let’s add the following directories to the stack.

pushd /etc
pushd /usr
pushd /var

The stack will contain the current directory as well.

To list the stack, use the following command −

dirs -v

Executing the following command moves the second last directory (~) to the top (as the current directory).

pushd -1

To verify, execute the dirs -v command −

pushd Command in Linux4

Comparison Table of pushd Command

A comparison of all the options is given below −

Option Effect Example Result
+N Moves the Nth directory (counting from the left) to the top pushd +1 Moves second directory in dirs to the top
-N Moves the Nth directory (counting from the right) to the top pushd -1 Moves second last directory in dirs to the top
-n Modifies the stack without changing the current directory pushd -n /tmp Adds /tmp to the stack but stays in the same directory

Conclusion

The pushd command in Linux simplifies directory navigation by saving the current directory to a stack and moving to a new one. It follows a last in, first out (LIFO) structure, allowing efficient switching between directories.

The pushd command supports options for modifying the stack without changing directories and rotating entries within it. Practical examples include switching directories while preserving the current path, adding directories without switching, and rotating the stack left or right.

Using pushd along with dirs helps manage directory history effectively, improving workflow efficiency in a shell environment.

Advertisements