directory stack

November 6th, 2006 mysurface Posted in Common, dirs, Misc, popd, pushd | Hits: 42002 | 23 Comments »

We can push our directories into a directory stack, and pop when we needed. Sometimes we need to access many long path of directories, we can save it by pushing it to directory stack.

If you do not know what is stack, please help yourself to read this. Stack is a temporary data storage based on the principle of Last In First Out (LIFO). The last item you push to stack, will be the first item you pop out.

To play around with directory stack, we need pushd, popd and dirs.

Let say you are in the directory /var/games now, you can push this directory into the stack

pushd .

Now you go to another directory, let say “dump” at your home directory. Again you do this.

pushd .

Go to more directory and do the command line show above. They to see what you have at the directory stack, do this


For example my current dirs result shows:

~ /root /var/games ~/dump

The ~ indicate that I am currently at my home directory and I have additional dir store in stack.

Let me do this


After doing this, I will be at /root and dirs to see my stack again.

/root /var/games ~/dump

My current directory is at /root, which I have losing an item in the stack.

I usually do this for important directory. I usually list all of them in new line for each.

dirs -p

Let say the current stack of mine looks like:


I am currently at my home directory, let say I wanna travel to 2nd dir, which is ~/mp3Akiko, without poping out from stack, I can do this trick.

cd `dirs +2`

23 Responses to “directory stack”

  1. ITs really useful concept.
    i used to think, some mechanism should be available to remember previously visited folders

  2. Just to let you know, on my Ubuntu machine, the last command:
    cd ‘dir +2’

    Must be changed to:
    cd ‘dirs +2’

    I don’t know if this is a problem with your syntax or its just a different distro.


  3. ctrl_freak: ops, it is my typo, thanks for highlighting it, it should be dirs. I have make the changes.

  4. You can use dirs -v to show the stack number. Much more useful.

    I have cd `dirs +` as an alias.

  5. instead of doing:

    cd `dirs +2`

    you can just do:

    pushd +2

    This will rotate the stack, and cd to that directory for you.

  6. Manzambi Diamvutu Says:

    In some systems the output of dirs +n will be abbreviated as follows: ~/something/something where ~ is your home directory. In such cases the syntax cd `dirs +2` will fail.

    you can use: cd `dirs +2 -l`

    where -l gives the full path.

  7. Manzambi Diamvutu Says:

    I have created a small function and placed it in my profile and i can use it much like an alias. I call it ccd.

    All i do is: ccd n

    where n is a number and whoossh I\\\\\\\’m teleported to the directory.

    ## ccd – custom change directory
    ## a little something to make life easier
    ## position is a number in the stack, and
    ## can be obtained by running the following
    ## commad: dirs -v
    ## pushd .
    ## pushd will load directories into the stack.
    ## popd will remove then and naturally
    ## dirs -v will list them.
    if [ -z $position ]
    echo \\"You need to specify the position of the directory in the stack\\"
    cd `dirs +$1 -l`

  8. Interesting! Thanks.

  9. You actually put an exciting new spin even on a topic thats been discussed for a long time. Great stuff, just great!

  10. Safekeeping downwardly, Apple’s app outlet wins by a knot. It’s a huge action of all sorts of apps vs a rather sad selection of a containerful for Zune. Microsoft has plans, especially in the land of games, but I’m not certain I’d poorness to bet on the succeeding if this scene is measurable to you. The iPod is a often alter prize in that housing.

  11. We’re a gaggle of volunteers and opening a brand new scheme in our community. Your website offered us with helpful info to work on. You have performed an impressive task and our whole community can be grateful to you.

  12. Thank you a bunch for sharing this with all folks you really recognize what you’re speaking approximately! Bookmarked. Please additionally visit my website =). We will have a link alternate contract among us

  13. […] cdd function (based on this one) makes it possible to cd to a given directory from the stack by typing: cdd […]

  14. Or you can use tilde expansion.
    dirs -v #view list of folders with index number

    cd ~# #replace # with index number to change to that directory

  15. I found the same file but not including the trojan at

  16. Thanks to you for this article because it is vary clear and practically well understanding.

  17. If some one wants to be updated with most recent technologies afterward he must
    be visit this site and be up to date daily.

  18. Kits personnalisés pour motoneiges et 4 roues.

  19. Chrome OS fifty seven was launched on March 23,

  20. Unlike different software program, no set up is required.

  21. Cosmetic surgery is not something to rush into.

  22. Click here in your free Launch!

Leave a Reply