The pushd and popd commands are used in conjunction with cd - to manage a stack of directories, allowing you to navigate back and forth between multiple directories easily.
How to Use pushd and popd:
-
Using
pushdto Navigate:
When you usepushd, it saves the current directory on a stack and then changes to the specified directory.pushd /path/to/directory1 -
Navigate to another directory:
You can usepushdagain to navigate to another directory, which will save the previous directory on the stack.pushd /path/to/directory2 -
Using
popdto Return:
To return to the previous directory saved on the stack, usepopd.popdThis will take you back to
/path/to/directory1. -
Using
cd -:
You can also usecd -to switch back to the last directory you were in, but it won't affect the stack created bypushdandpopd. If you want to return to the last directory you were in before the current one, you can use:cd -
Example Workflow:
# Start in /home/user
pushd /path/to/directory1 # Stack: /home/user -> /path/to/directory1
pushd /path/to/directory2 # Stack: /path/to/directory1 -> /path/to/directory2
# Now in /path/to/directory2
popd # Returns to /path/to/directory1
# Now in /path/to/directory1
cd - # Returns to /home/user
# Now in /home/user
Summary:
- Use
pushdto save the current directory and navigate to a new one. - Use
popdto return to the last directory saved on the stack. - Use
cd -to quickly switch back to the last directory you were in, but it does not interact with the stack.
