Understanding and Utilizing pushd and popd Commands in Linux
#Linux #pushd_&_popd
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.
pushd
and popd
commands offer a way to remember your paths history without searching into your historyThe 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.
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]
pushd
swaps the top two directories on the stack, making it easy to toggle between them.Examples
Navigating to a New Directory
pushd /var/www/html
This command adds
/var/www/html
to the stack and navigates to it.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.
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
Returning to the Previous Directory
After navigating to several directories using
pushd
, usingpopd
will take you back to the previous directory in the stack.
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
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.
- Without any options,
dirs
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.
- The
dirs -v
Prettify Path:
- The
-p
option prints the directory stack with one entry per line, similar to-v
but without the position numbers.
- The
dirs -p
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.
- You can display only the Nth entry in the stack (counting from 0) by using
dirs +1 # Displays the second entry from the top of the stack
dirs -0 # Displays the bottom entry of the stack
Suppress Tilde Expansion:
- The
-l
option ensures that the home directory is not abbreviated to a tilde (~
), displaying the full path instead.
- The
dirs -l
What I Prefer:
- The
-v -l
options print-out the stack list with the position number and full path
- The
dirs -v -l
Examples
Adding path to the Stack without navigating
First, Let's print-out the current stack content
dirs -v -l
- 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
- list the current stack content
dirs -v -l
⭐Navigating using the position number
pushd
and popd
command provide a way to use the position number in your stack for navigation without insert the full path.
- print-out the current stack list
dirs -v -l
- navigate to the index number 1
pushd +1
- revert back to the previous path
pushd
- navigate to the last value in the stack list
pushd -0
wipe a specific path from the stack
- print-out your current stack list
dirs -v -l
- 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.