Monday, January 31, 2011

Linux -- Directory Stack

I am just surprised at the way Data Structures are used in our day-today life not just the computers field. Now, its time to discuss the directory stack. The basic idea of stack is also applied to the directory stack, a list of recently-visited directories. Like any other stack, the directory stack also allows us to view the contents of stack, push an element into the stack, pop an element from the stack. But the difference is that the insertion and deletion operations can be done on any element in the stack, not just the top element of the stack.

Push operation is carried upon by the pushd command while Pop operation is carried upon by the popd command. These two operations can quickly navigate around the file-system. If the pop operation is issued when the stack is empty, the appropriate message is issued. The cd command changes the current element at the top of the stack i.e., the $DIRSTACK variable. The dirs builtin displays the contents of the directory stack. The contents of the directory stack are also visible as the value of the DIRSTACK shell variable.

In short, the pushd builtin adds directories to the stack as it changes the current directory, and the popd builtin removes specified directories from the stack and changes the current directory to the directory removed.

The pushd command is similar to the cd command, but pushd retains the directory that you are currently in, in its directory-stack. For example, the command pushd /usr/share displays the entire directory-stack. It knows that I came to /usr/share from the home directory. If I issue popd, this is going to take my directory-stack and bring me back to the previous directory in the directory-stack. The command saves the current directory on the top of the directory stack and then cd to dir. With no arguments, pushd exchanges the top two directories.

The popd command removes the top entry from the directory stack, and cd to the new top directory. When no arguments are given, popd removes the top directory from the stack and performs a cd to the new top directory.

The basic behavior of pushd, popd and dirs are shown in the figure below:



The DIRSTACK array needs some explanation at this point in time. The contents of the array can be displayed by the command "echo ${DIRSTACK[*]}". If only the second element in the array needs to be printed, then "echo ${DIRSTACK[1]}" as the arrays start with index as 0. The DIRSTACK array is shown in the below figure:




Variations of dirs:
dirs [+N | -N] [-clvp]
+N: Displays the Nth directory (counting from the left of the list printed by dirs when invoked without options), starting with zero.
-N: Displays the Nth directory (counting from the right of the list printed by dirs when invoked without options), starting with zero.
-c: Clears the directory stack by deleting all of the elements.
-l:  Produces a longer listing; the default listing format uses a tilde to denote the home directory.
-p: Causes dirs to print the directory stack with one entry per line.
-v: Causes dirs to print the directory stack with one entry per line, prefixing each entry with its index in the stack.

The variations of the dirs command are shown in the below figure:


Variations of pushd:
pushd [dir | +N | -N]
+N: Brings the Nth directory (counting from the left of the list printed by dirs, starting with zero) to the top of the list by rotating the stack.
-N: Brings the Nth directory (counting from the right of the list printed by dirs, starting with zero) to the top of the list by rotating the stack.
dir: Makes the current working directory be the top of the stack, and then executes the equivalent of `cd dir'.

Variations of popd:
popd [+N | -N] [-n]
+N: Removes the Nth directory (counting from the left of the list printed by dirs), starting with zero.
-N: Removes the Nth directory (counting from the right of the list printed by dirs), starting with zero.
-n: Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated.

The variations of pushd and popd are shown in the figure below:




Sources: Bash Reference

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...