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 uses for both of these commands or reasons for keeping stack with directories (when you have tab completion, cd -
, aliases for shortening cd ..
, etc.).
Best Answer
pushd
,popd
, anddirs
are shell builtins which allow you manipulate the directory stack. This can be used to change directories but return to the directory from which you came.For example
start up with the following directories:
pushd to dir1
dirs
command confirms that we have 2 directories on the stack now.dir1
and the original dir,somedir
. NOTE: Our "current" directory is~/somedir/dir1
.pushd to ../dir3 (because we're inside
dir1
now)dirs
shows we have 3 directories in the stack now.dir3
,dir1
, andsomedir
. Notice the direction. Every new directory is getting added to the left. When we start popping directories off, they'll come from the left as well.manually change directories to
../dir2
Now start popping directories
Notice we popped back to
dir1
.Pop again...
And we're back where we started,
somedir
.Might get a little confusing, but the head of the stack is the directory that you're currently in. Hence when we get back to
somedir
, even thoughdirs
shows this:Our stack is in fact empty.