Many of us just use cd, cd .., cd <dir> to navigate through the file system in Linux. Directory or file-system navigation can be simplified by using following commands -

1) Many times we are in some directory say /home/user/foo and we need to go to the directory /usr/share/bar . After completing tasks in this directory, if one needs to get back to the previous directory, he would type in ‘cd /home/user/foo’ . Won’t it be far easy if we had some kind of back button as in Nautilus or Windows Explorer ? Its already there in Linux. Just type in
$ cd -
and you will reach back to the previous directory.

2) Another important tool to make navigation easier is ‘pushd’ and ‘popd’. Say you are in some directory /home/user/foo1 and then you wish to navigate to /home/user/foo2 and then to /home/user/foo3
After all these you need to back to previous directories. You can use pushd and popd like this :

$ pwd
/home/user/foo1
$ pushd .
$ cd /home/user/foo2
$ pushd .
$ cd /home/user/foo3

Now to get back to previous directories -
$ popd
$ pwd
/home/user/foo2

pushd actually push its argument directory to a stack and popd changes the current directory to the one on top of the stack.

Hope you enjoyed these tips.

Share and Enjoy:
  • Digg
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Reddit
  • Facebook
  • TwitThis
  • Live
  • Technorati
  • Furl
  • Sphinn
  • blogmarks
  • LinkedIn
  • Pownce



5 Comments to “Directory Navigation Tips in Linux”

  1. Garbu | October 8th, 2008 at 4:37 am

    Nice Tip !!

    I will translate it to spanish, they might know this useful command.

    Thanks a lot

    http://conociendolinux.wordpress.com

  2. Abhinav | October 8th, 2008 at 9:00 pm

    Hey this one is nice. I am fed up with changing directories in symfony, with code distributed in many files and many diff folders.

  3. Abhinav | October 8th, 2008 at 9:01 pm

    Anyways why the hell don’t you use my javascript calendar :P

    http://abhinavsingh.com/blog/2008/10/a-light-weight-javascript-calendar-for-your-websites-and-blogs/

  4. quodlibetor | October 8th, 2008 at 11:14 pm

    You know, pushd can actually be used as replacement for the cd command, so, instead of what you have above you could do:

    ~$ pwd
    ~$ pushd .
    ~$ pushd /home/user/foo2
    ~/foo2$ pushd /home/user/foo3
    ~/foo3$ popd
    ~/foo2$ popd
    ~$

    although, since `cd’ without an argument always takes you home, i can’t see using `pushd ~’ anywhere except in a script.

  5. Harsh | May 28th, 2009 at 3:53 pm

    `cd -` does the trick sans the pushd/popd requirement.

Leave a Comment