TrumanWong

pushd

Adds the directory to the top of the directory stack.

Summary

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

The main purpose

  • Add a directory to the top of the directory stack, switching the current working directory to that directory.

  • Rotate the directory stack so that the new top of the stack becomes the current working directory.

  • With no arguments, swaps the first two directories of the directory stack.

Options

-n suppresses changes to the current working directory caused by adding directories.

Parameters

+N (optional): In the list displayed by executing the dirs command without parameters, the Nth directory from the left will be the top of the stack, and the directory in front of it will be moved to the bottom. (counting from 0)

-N (optional): In the list displayed by executing the dirs command without parameters, the Nth directory from the right will be the top of the stack, and the directory in front of it will be moved to the bottom. (counting from 0)

dir (optional): The directory to push.

return value

Returns successful unless an illegal option is provided or an execution error occurs.

example

# Add a directory to the stack, changing the current working directory.
[user2@pc ~]$ dirs
~
[user2@pc ~]$ pushd ~/Desktop
~/Desktop ~
[user2@pc Desktop]$
#Add a directory to the stack, leaving the current working directory unchanged.
[user2@pc ~]$ dirs
~
[user2@pc ~]$ pushd -n ~/Desktop
~ ~/Desktop
[user2@pc ~]$ pushd -n ~/Pictures
~ ~/Pictures ~/Desktop

# Adjust the order.
[user2@pc ~]$ pushd +1
~/Pictures ~/Desktop ~
[user2@pc ~]$ pushd -1
~/Desktop ~ ~/Pictures
[user2@pc ~]$ pushd
~ ~/Desktop ~/Pictures

Notice

  1. The directory stack commands of bash include dirs popd pushd.
  2. The current directory is always at the top of the directory stack.
  3. This command is a built-in bash command. For related help information, please see the help command.

Reference link