Pages

Monday, March 5, 2012

Push and Pop Directories on to Stack

Share it Please

Linux allows us to push directories on to the stack and pop them when needed.

Pushd : used to push directories on to the stack
Popd   : used to pop directories from the stack
Dirs    : allows us to see what directories are available on the stack

One thing to remember is that what ever the directory that is currently pushed is the one that popped first. Consider if I push /etc/ and then /var, then if I use popd , /var will the one that gets popped. Consider the sample

[root@vx111a ~]# cd /etc/X11/
[root@vx111a X11]# pwd
/etc/X11
[root@vx111a X11]# pushd $PWD
/etc/X11 /etc/X11
[root@vx111a X11]# cd /var/ftp/pub
[root@vx111a pub]# pushd $PWD
/var/ftp/pub /var/ftp/pub /etc/X11
[root@vx111a pub]# cd /root/perl/
[root@vx111a perl]# pushd $PWD
~/perl ~/perl /var/ftp/pub /etc/X11
[root@vx111a perl]# dirs
~/perl ~/perl /var/ftp/pub /etc/X11
[root@vx111a perl]# popd $2
~/perl /var/ftp/pub /etc/X11
[root@vx111a perl]# popd $3
/var/ftp/pub /etc/X11
[root@vx111a pub]# popd $4
/etc/X11

We can also pop a particular location by accessing them using $<Num> like popd $4 which pops out the 4 element on the directory stack.

No comments :

Post a Comment