Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.).

share|improve this question

2 Answers 2

up vote 29 down vote accepted

pushd, popd, and dirs allow you to change directories but still be able to return to the directory from which you came.

For example

start up with the following directories:

$ ls
dir1  dir2  dir3

pushd to dir1

$ pushd dir1
~/somedir/dir1 ~/somedir
$ dirs
~/somedir/dir1 ~/somedir

dirs command confirms that we have 2 directories on the stack now. dir1 and the original dir, somedir.

pushd to ../dir3 (because we're inside dir1 now)

$ pushd ../dir3
~/somedir/dir3 ~/somedir/dir1 ~/somedir
$ dirs
~/somedir/dir3 ~/somedir/dir1 ~/somedir
$ pwd
/home/saml/somedir/dir3

dirs shows we have 3 directories in the stack now. dir3, dir1, and somedir. Notice the direction. Every new directory is getting added to the left. When we start poping directories off, they'll come from the left as well.

manually change directories to ../dir2

$ cd ../dir2
$ pwd
/home/saml/somedir/dir2

Now start popping directories

$ popd
~/somedir/dir1 ~/somedir
$ pwd
/home/saml/somedir/dir1

Notice we popped back to dir1.

Pop again...

$ popd
~/somedir    
$ pwd
/home/saml/somedir

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 though dirs shows this:

$ dirs
~/somedir

Our stack is infact empty.

$ popd
bash: popd: directory stack empty
share|improve this answer
1  
Thanks, I totally understand the concept of stack and how this commands work. However, I'm looking for some practical reasons for keeping stack with directories (when you have tab completion, cd -, aliases for shortening cd .., etc.). –  REACHUS May 25 '13 at 18:26
1  
I often use pushd & popd in scripts b/c they save me from having to remember where I was coming from, I can always just popd to get back from where I came. I usually do popd >/dev/null 2>&1 to make it silent. I use cd- everyday in my shell. There are some other time saving tips in this article as well: thegeekstuff.com/2008/10/…. –  slm May 25 '13 at 18:31
    
Take a look at $CDPATH. It's pretty powerful if you're looking for more efficient ways to get around via cd. –  slm May 25 '13 at 18:38
1  
@Garrett - none that I can conceive. –  slm Aug 22 at 1:11
1  
@Garrett @slm since cd - only tracks the last directory, i imagine it would be possible to have issues if you call a function which also changes directory internally. in that case, the function would end up resetting - to your current directory, not the directory you want to pop back to. pushd/popd is the safest method. Note: i haven't tested my theory. –  Binary Phile Nov 10 at 20:52

pushd and popd allow you to manipulate the directories on stack. When you pushd a directory, you put the current directory ion stack and change directory to the one specified as a parameter. popd will allow you to go back to the directory on 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.