LinuxCommandLibrary

pushd

Change directory and save previous location

TLDR

Switch to directory and push it on the stack

$ pushd [path/to/directory]
copy

Switch first and second directories on the stack
$ pushd
copy

Rotate stack by making the 5th element the top of the stack
$ pushd +4
copy

Rotate the stack 4 times to the left (the current directory stays at the top by replacing the 5th element)
$ pushd -n +4
copy

SYNOPSIS

pushd [dir | +N | -N]

PARAMETERS

dir
    Pushes the current working directory onto the stack, then changes to the specified dir.

+N
    Rotates the N-th directory (0-indexed from the left, top is 0) to the top of the stack and makes it the current working directory.

-N
    Rotates the N-th directory (0-indexed from the right, top is 0) to the top of the stack and makes it the current working directory.

(no arguments)
    Swaps the top two directories on the stack and changes to the new top directory.

DESCRIPTION

The pushd command allows users to manipulate the directory stack, a list of recently visited directories. When invoked with a directory path, it pushes the current working directory onto the stack, then changes to the specified new directory, and finally displays the updated stack. If no directory is specified, pushd swaps the top two directories on the stack, making the second directory the current working directory. The command can also be used with numerical arguments to rotate specific directories to the top of the stack. This functionality is invaluable for navigating complex file systems efficiently, allowing users to quickly jump between frequently used locations without typing full paths repeatedly. It works in conjunction with popd to remove directories from the stack and dirs to merely list its contents.

CAVEATS

pushd is a shell built-in command, meaning it is an integral part of your interactive shell (e.g., Bash, Zsh) rather than an external executable. Its behavior and available options can vary slightly between different shell implementations. The directory stack is specific to the current shell session.

THE DIRECTORY STACK DISPLAY

After any pushd operation, the shell typically displays the entire directory stack. The leftmost directory in this display is always the current working directory, and subsequent directories are older entries on the stack.

DIRSTACK ENVIRONMENT VARIABLE

The directory stack is internally represented by an array variable called DIRSTACK. Users can inspect or manipulate this variable directly, although it's generally recommended to use pushd, popd, and dirs for safe stack management.

HISTORY

The concept of a directory stack and commands like pushd, popd, and dirs originated in the C shell (csh) in the late 1970s at UC Berkeley. This innovation provided a much-needed mechanism for users to manage and navigate frequently visited directories more efficiently than with the basic cd command alone. It was later adopted by other popular shells, including Bash and Zsh, becoming a standard and highly useful feature in modern Unix-like environments.

SEE ALSO

popd(1), dirs(1), cd(1)

Copied to clipboard