Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How do I use pushd and popd commands?

What are the practical uses of both pushd and popd when there is an advantage of using these two commands over cd and cd -?

EDIT: I'm looking for some practical examples of ud -, aliases for shortening cd .., etc.).
by

2 Answers

akshay1995
One simple use case for using dirs stack what you cannot do by just cd is:

pushd . adds current directory XX to dirs stack. Afterwards, you can move around using cd, and to return to XX you just do popd regardless of how "far away" are you in the directory tree (can jump over multiple levels, sideways etc). Especially useful in bash scripts.
sandhya6gczb
pushd and popd allow you to manipulate the directories on stack.

When you pushd a directory, you put the current directory on the stack and change directory to the one specified as a parameter.

popd will allow you to go back to the directory on the stack.

If you repeat, the directory traversal will be sort of preserved and you can come back to the saved directories in reverse order from what you saved them in.

Login / Signup to Answer the Question.