pushd - macOS


Overview

pushd is a versatile command-line tool used to manipulate the working directory stack within the Terminal. It allows you to push the current directory onto the stack and move to a new directory, enabling easy navigation between multiple directories.

Syntax

pushd [-n] [-R] [-S] [-L] [-q] [-P] [-g] [-u] [-x] [-d] [dir]

Options/Flags

  • -n: Dry run mode. Shows the directory that would be pushed without modifying the stack.
  • -R: Recursively pushes all subdirectories of the specified directory.
  • -S: Suppresses output, making the command silent.
  • -L: Pushes the working directory that was set by the previous pushd command.
  • -q: Quiet mode. Suppresses unnecessary messages.
  • -P: Includes the path of the pushed directory in the stack.
  • -g: Globbing mode. Expands wildcards in the specified directory.
  • -u: Unsets any previous definitions of the CDPATH environment variable.
  • -x: Executes the specified directory as a command instead of pushing it.
  • -d: Deletes the specified directory from the stack.

Examples

Simple Usage:

pushd Documents

Navigating Up the Directory Hierarchy:

pushd ..

Pushing Multiple Directories:

pushd Documents Music

Using Dry Run Mode:

pushd -n Desktop

Recursively Pushing Subdirectories:

pushd -R Projects

Common Issues

  • Stack Overflow: If the working directory stack becomes too large, pushd may fail. Use the -d option to delete directories from the stack.
  • Invalid Directories: If the specified directory does not exist, pushd will return an error.

Integration

Chaining with Other Commands:

cd $(pushd -n Desktop)

Using with “popd”:

pushd Documents
mkdir new_folder
pushd new_folder
popd
  • popd: Pops the top directory from the stack and moves to it.
  • cd: Changes the working directory.
  • dirstack: Lists the directories on the working directory stack.