Understanding and Utilizing pushd and popd Commands in Linux

#Linux #pushd_&_popd

Understanding and Utilizing pushd and popd Commands in Linux

Inception

In the realm of Linux, navigating directories efficiently is a fundamental skill for any user. While the cd (change directory) command is widely known and used for moving between directories, pushd and popd commands offer a more advanced level of directory navigation, enabling users to work within a stack of directories. This article delves into the mechanics of pushd and popd, providing practical insights into their usage and benefits.

The pushd and popd commands offer a way to remember your paths history without searching into your history

The Basics of pushd and popd

What are pushd and popd?

The pushd (push directory) command is used to add directories to the top of the directory stack and navigate to them, while popd (pop directory) removes the top directory from the stack and navigates the user back to the previous location. This stack-based approach to directory navigation allows users to easily switch between directories without losing track of their locations.

⭐Think of Stack as a simple storage that allows you to push new path in using pushd and remove path out using popd That besides the navigation.

💡
Still have misunderstanding!? Don't worry, Keep reading.. The examples below provide more information.

How the Directory Stack Works

The directory stack is a list that stores the history of visited directories, similar to a stack in data structures where the last item added is the first to be removed (LIFO - Last In, First Out). pushd adds a directory to this stack and popd removes the top directory, making it easy to move back and forth between directories.


Using pushd Command

Basic Syntax

pushd [options] [directory]
💡
When used without any options or directory, pushd swaps the top two directories on the stack, making it easy to toggle between them.

Examples

  1. Navigating to a New Directory

     pushd /var/www/html
    

    This command adds/var/www/html to the stack and navigates to it.

  2. Swapping Directories

    Simply using pushd without any arguments swaps to the previous path, This mechanism allow swaps the top two directories in the stack, allowing for quick toggling.

💡
Explained "How to print stack content?" down below, don't be hurry, Keep Reading.....
  1. More Navigation's

    let's navigate to more paths

pushd /var    # will change the current directory to /var
pushd /tmp    # will change the current directory to /tmp
pushd /home   # will change the current directory to /home

Using popd Command

Basic Syntax

popd [options]

popd removes the top directory from the stack and navigates the user to the new top directory.

Examples

  1. Returning to the Previous Directory

    After navigating to several directories using pushd, using popd will take you back to the previous directory in the stack.

💡
Will Take full examples in a while

Print-out The Stack Content

To print out the content of the directory stack used by the pushd and popd commands use the dirs command

Examples

  1. Print in a Single Line:

    • Without any options, dirs simply prints the directory stack on a single line, with the top of the stack list on the left.
    dirs
  1. Verbose Mode:

    • The -v option prints the directory stack with one entry per line, prefixed by its position number in the stack. The directory at position 0 is the current directory.
    dirs -v
  1. Prettify Path:

    • The -p option prints the directory stack with one entry per line, similar to -v but without the position numbers.
    dirs -p
  1. Display Nth Entry:

    • You can display only the Nth entry in the stack (counting from 0) by using +N for the Nth entry from the top or -N for the Nth entry from the bottom.
    dirs +1  # Displays the second entry from the top of the stack
    dirs -0  # Displays the bottom entry of the stack
  1. Suppress Tilde Expansion:

    • The -l option ensures that the home directory is not abbreviated to a tilde (~), displaying the full path instead.
    dirs -l
  1. What I Prefer:

    • The -v -l options print-out the stack list with the position number and full path
dirs -v -l

Examples

Adding path to the Stack without navigating

  1. First, Let's print-out the current stack content
dirs -v -l
  1. Append a new path to the stack without navigating to it (i.e. still at the same path directory)
pushd +n /opt  # append the /opt path to the stack without navigating
  1. list the current stack content
dirs -v -l

⭐Navigating using the position number

pushd and popdcommand provide a way to use the position number in your stack for navigation without insert the full path.

  1. print-out the current stack list
dirs -v -l
  1. navigate to the index number 1
pushd +1
  1. revert back to the previous path
pushd
  1. navigate to the last value in the stack list
pushd -0

wipe a specific path from the stack

  1. print-out your current stack list
dirs -v -l
  1. wipe the path with the position number 1
popd +1

Interaction with cd Command

While pushd and popd offer advanced directory navigation, many users still rely on the cd command for its simplicity. However, using cd alongside pushd and popd can affect the stack.

Impact of cd on the Stack

When you use cd to change directories, it does not affect the directory stack. This means that the stack remains unchanged, and popd will still return you to the last directory pushed onto the stack, not the directory navigated to using cd.

Integrating cd with the Stack

It's easy to revert to old habits and use cd to change directory. If you do that, you'll stamp over the first directory in the stack. This is inevitable, as the first slot is reserved for the current working directory—none of the others change position.


Making Stack Persistent

The Pushd stack by default is not persistent, in other words ephemeral per session per user. The Stack is not stored in any file or database, it resides in memory and no written to disk, and each shell session works independently.

Designed for Temporary Use: The primary use case for pushd and popd is to temporarily bookmark directories for quick navigation during the session. This design intention supports temporary, session-based tasks rather than long-term or persistent directory tracking.

What if you have some paths that you want to make it persistent in your sessions, There's a work-around to make it persistent, by using ~/.bashrc or /etc/bash.bashrc files, These files contain a series of commands that are executed whenever a new Bash shell session is started in interactive mode, the ~/.bashrc for the current user, and the /etc/bash.bashrc for all users.

Past the following at the end of ~/.bashrc or /etc/bash.bashrc

# configs for Pushd Stack
dirstack=( "$HOME"
           /etc  # insert your paths here
           /var )

for dir in "${dirstack[@]}"; do
    pushd -n "$dir" >/dev/null
done

unset dirstack  # return the array value to 0, avoide overlapping

Save, open a new session and list the stack directories

dirs -v -l

Conclusion

Understanding and incorporating pushd and popd into your Linux command-line toolkit can significantly enhance your navigation efficiency and overall productivity. By mastering these commands, you can effortlessly manage your directory stack, making your Linux experience smoother and more intuitive.


Resources


That's it, Very straightforward, very fast🚀. Hope this article inspired you and will appreciate your feedback. Thank you.