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

3 Answers 3

up vote 52 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
2  
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 '14 at 1:11
3  
@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 '14 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

One practical use that I have found is to toggle between directories using pushd and popd.

Suppose we have two directories dir1 and dir2 and I need to toggle between them continuously for some xyz reason.

Currently I am in somedir which has two directories dir1 and dir2:

alcohol@alcohol-machine:~/somedir$ ls
dir1  dir2
alcohol@alcohol-machine:~/somedir$ 

Now I switch to dir1

alcohol@alcohol-machine:~/somedir$ cd dir1
alcohol@alcohol-machine:~/somedir/dir1$ pwd
/home/alcohol/somedir/dir1

Now I will add dir1 to stack and switch to dir2

alcohol@alcohol-machine:~/somedir/dir1$ pushd /home/alcohol/somedir/dir2
~/somedir/dir2 ~/somedir/dir1
alcohol@alcohol-machine:~/somedir/dir2$

As you can see, I now have dir2 and dir1 on stack and I am currently into dir2.

Now to switch back to dir1, I will run pushd || popd.

alcohol@alcohol-machine:~/somedir/dir2$ pushd || popd
~/somedir/dir1 ~/somedir/dir2
alcohol@alcohol-machine:~/somedir/dir1$ 

Voila, I am into dir1 and I have dir1 and dir2 on stack. To switch back to dir2 again run pushd || popd

alcohol@alcohol-machine:~/somedir/dir2$ pushd || popd
~/somedir/dir1 ~/somedir/dir2
alcohol@alcohol-machine:~/somedir/dir1$ 

This a simple way to toggle between directories. Now, you might what to know, why I need to toggle between directories? Well one use case is, when I work on web application, I have my make file in one directory and my log files in another directory. Often when debugging the application, I need to switch between the log directory, to check the latest logs and then switch back to the make directory, to make some changes and build the application.

share|improve this answer
    
(1) The question asks, "What are the advantages of pushd and popd over cd -?"  You have clearly not answered that question, as you are describing exactly what cd - does.  (2) What is the point of saying pushd || popd?  Yes, I know that cmdA || cmdB means run cmdA and, if it fails, then run cmdB.  I'm asking what good it does in this context.  pushd with no arguments means "swap the top two elements on the directory stack"; it fails if the current directory is the only element on the directory stack.  In that case, popd will fail also. –  G-Man Aug 17 at 0:54

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.