Live Chat!

directory stack

* * * * * 1 votes

November 6th, 2006 mysurface Posted in Common, Misc, dirs, popd, pushd | Hits: 3321 |

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

dirs

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

popd

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:

~
/root
~/mp3/Akiko
/var/games
~/dump

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`

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

    Cheers

  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.

Leave a Reply