Tuesday, March 31, 2009

Directory stack

Navigating directories using dirs, pushd and popd


Stack is a temporary data storage based on LIFO. The last item you push to stack, will be the first item you pop out.
we need pushd, popd and dirs to play around with stack
pushd: pushes or adds the directory to the directory stack
popd: pops or removes the directory from the directory stack (last in first out LIFO)
dirs: displays the contents of the stack
Directory stack can be really usful if you need to navigate to some directories which is very lengthy and you need to do it very often.


/tmp/very/long/directory/structure/that/is/too/deep
/tmp/hard/to/navigate/this/directory/structure
/some/structure/which/i/donot/prefer/navigating/often


Let us take the sample directory structures given above.
One way to make the navigation easy if you have to do it requenty is to set aliases, as given below
$ alias godeep="cd /tmp/very/long/directory/structure/that/is/too/deep"
OR, you can make use of the the directory stack.

The dirs command list the contents of the directory stack. The first entry it shows is not the contents, but the current directory. In this case the stack is empty.

$ cd /tmp/very/long/directory/structure/that/is/too/deep
$ pushd .

$ cd /some/structure/which/i/donot/prefer/navigating/often

$ pushd .

$ cd /some/structure/which/i/donot/prefer/navigating/often

$ pushd .

The out put of the dirs command will be as follows,






If you want to navigate to the third directory issue the following command
$ cd `dirs +3`

To remove the element from the stack issue the popd command as follows
$ popd

No comments: