Directory stack

In order to facilitate users to switch between different directories, Bash provides a directory stack function.

cd-

Bash can remember the directories the user has entered. By default, only the previous directory is remembered, and the cd - command can return to the previous directory.

# The current directory is /path/to/foo
$ cd bar

# Back to /path/to/foo
$ cd-

In the above example, the user's original directory is /path/to/foo, after entering the subdirectory bar, use cd - to return to the original directory.

pushd, popd

If you want to memorize multiple directories, you can use the pushd command and the popd command. They are used to manipulate the directory stack.

The usage of the pushd command is similar to the cd command, you can enter the specified directory.

$ pushd dirname

The above command will enter the directory dirname and put the directory on the stack.

When using the pushd command for the first time, the current directory will be put on the stack first, and then the directory to be entered will also be put on the stack, the position is above the previous record. Every time you use the pushd command in the future, the directory you want to enter will be placed at the top of the stack.

When the popd command has no parameters, it will remove the top record of the stack and enter the new top directory of the stack (that is, the original second directory).

Below is an example.

# Currently in the home directory, the stack is empty
$ pwd
/home/me

# Enter /home/me/foo
# The current stack is /home/me/foo /home/me
$ pushd ~/foo

# Enter /etc
# The current stack is /etc /home/me/foo /home/me
$ pushd /etc

# Enter /home/me/foo
# The current stack is /home/me/foo /home/me
$ popd

# Enter /home/me
# The current stack is /home/me
$ popd

# The directory remains unchanged, the current stack is empty
$ popd

The parameters of these two commands are as follows.

(1)-n parameter

The parameter of -n means that only the stack is operated, and the directory is not changed.

$ popd -n

The above command only deletes the record at the top of the stack, does not change the directory, and stays in the current directory after execution.

(2) Integer parameter

These two commands can also accept an integer as a parameter. The integer represents the record (starting from 0) at the specified position in the stack as the object of operation. The directory will not be switched at this time.

# Directory No. 3 from the top of the stack (starting from 0), move to the top of the stack
$ pushd +3

# Directory No. 3 from the bottom of the stack (starting from 0), move to the top of the stack
$ pushd -3

# Delete the No. 3 directory from the top of the stack (starting from 0)
$ popd +3

# Delete the No. 3 directory from the bottom of the stack (starting from 0)
$ popd -3

The integer numbers in the above example are all counted from 0, popd +0 is to delete the first directory, popd +1 is to delete the second, popd -0 is to delete the last directory, popd- 1 is the second to last to delete.

(3) Catalog parameters

pushd can accept a directory as a parameter, which means to put the directory on the top of the stack and enter the directory.

$ pushd dir

popd does not have this parameter.

dirs command

The dirs command can display the contents of the directory stack, and is generally used to view the results of pushd and popd operations.

$ dirs

It has the following parameters.

  • -c: Clear the directory stack.
  • -l: The tilde prefix is ​​not displayed in the user's home directory, and the complete directory is printed.
  • -p: Print the directory stack with one entry per line. The default is to print on one line.
  • -v: One entry per line, and the position number (starting from 0) is displayed before each entry.
  • +N: N is an integer, indicating that the Nth directory from the top of the heap is displayed, starting from zero.
  • -N: N is an integer, which means displaying the Nth directory from the bottom of the heap, starting from zero.