Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash pushd Uso: Manage directory stack efficiently
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
    5. Tips

[Linux] Bash pushd Uso: Manage directory stack efficiently

Overview

The pushd command in Bash is used to change the current directory while simultaneously saving the previous directory in a stack. This allows users to easily navigate back to the previous directory using the popd command.

Usage

The basic syntax of the pushd command is as follows:

pushd [options] [arguments]

Common Options

  • +n: Rotate the directory stack to the right by n positions.
  • -n: Rotate the directory stack to the left by n positions.
  • -q: Suppress the output of the directory stack after the command is executed.

Common Examples

  1. Change to a directory and save the current one:
    pushd /path/to/directory
    
  2. Change to a directory and view the directory stack:
    pushd /path/to/directory
    
  3. Return to the previous directory:
    popd
    
  4. Rotate the stack to the right:
    pushd +1
    
  5. Suppress output while changing directories:
    pushd -q /path/to/directory
    

Tips

  • Use dirs to view the current directory stack at any time.
  • Combine pushd and popd for quick navigation between frequently used directories.
  • Remember that the directory stack is maintained only for the current session; it resets when you log out.