Sunday, March 20, 2011

Toggling through the directory hierarchy

I recently came across pushd and popd commands and found them to be of great help. I use command line to do a most of my work, while I am working there are lot of time's when I move deep into the directory hierarchy and then return to the directory I was previously working in. This is sometime's  tiring and time consuming. If you are one of them and don't know about the pushd and popd command's then you are at the right place. Let me explain it with an example -

       $ pwd
       /home/linux_code

Now you wish to look at the code which might be under fs/minix and then later want to come back to the current directory then first push the current directory to the stack (that is what I think pushd stands for -push directory). It takes an argument (the directory to be saved/pushed).

      $pushd .
      $cd fs/minix

Once you are done with the work just use the popd command to return to the previously saved direcotory. 

      $popd
      $pwd
      /home/linux_code

Also note that, if you start pushing more than one directory on to the stack, then it will be popped in the reverse order (since stack follows Last In First Out (LIFO) order). So start using.