Advanced Directory Navigations Tips and Tricks in Linux

Directory navigation is one of the most basic concepts when it comes to understanding any command line system. Although it's not a very difficult thing to understand when it comes to Linux, there are certain tips and tricks that can enhance your experience, and help you do things faster. In this article, we will discuss some advanced directory navigation tips.

The Stuff We Already Know

Before jumping on to the advanced concepts, here is the basics of directory navigation that the article expects its readers to know:

  • 'pwd' command is used to display the current working directory.
  • 'cd' command is used to change the current working directory.
  • 'cd' followed by space and followed by a couple of periods (cd ..) brings the control back to the parent directory
  • 'cd' followed by just the name of a subdirectory changes to that subdirectory
  • 'cd' followed by a complete path changes to that directory

Advanced Tips

In this section we will discuss some directory navigation tips and tricks that will help you easily switch between directories.

Change to the home directory from anywhere

Your home directory is an important directory, and everyone switches back and forth quite frequently. While typing 'cd /home/<your-home-directory-name>', isn't a big deal, there is another way out which is not only easier but faster too. And that alternative is typing only 'cd'.

Here is an example :

$ pwd
$ cd
$ pwd

So you can see, no matter where the current control is, just type 'cd' command and you can immediately change to your home directory.

NOTE- To change to the home directory of a particular user, just type 'cd ~user_name'

Switch between directories using cd -

Suppose your current working directory is this:

$ pwd

and you want to switch to the directory /usr/bin/X11, and then switch back to the directory mentioned above. So what will you do? The most straight forward way is :

$ cd /usr/bin/X11
$ cd /home/himanshu/practice/

Although it seems a good way out, it really becomes tiring when the path to the directories is very long and complicated. In those cases, you can use the 'cd -' command.

While using 'cd -' command, the first step will remain the same, i.e., you have to do a cd <path> to the directory to you want to change to, but for coming back to the previous directory, just do a 'cd -', and that's it.

$ cd /usr/bin/X11
$ cd -
$ pwd

And if you want to again go back to the last directory, which in this case is /usr/bin/X11, run the 'cd -' command again. So you can see that using 'cd -' you can switch between directories easily. The only limitation is that it works with the last switched directories only.

Switch between directories using pushd and popd

directory navigation

If you closely analyse the 'cd -' trick, you'll find that it helps switching between only the last two directories, but what if there is a situation in which you switch multiple directories, and then want to switch back to the first one. For example, if you switch from directory A to directory B, and then to directory C and directory D. Now, you want to change back to Directory A.

As a general solution, you can type 'cd' followed by the path to directory A. But then again, if the path is long or complicated, the process can be time-consuming, especially when you have to switch between them frequently.

In these kind of situations, you can use the 'pushd' and 'popd' commands. The 'pushd' command saves the path to a directory in memory, and the 'popd' command removes it, and switches back to it too.

For example :

$ pushd .
/usr/include/netipx /usr/include/netipx
$ cd /etc/hp/
$ cd /home/himanshu/practice/
$ cd /media/
$ popd
$ pwd

So you can see that I used 'pushd' command to save the path to current working directory (represented by .), and then changed multiple directories. To come back to the saved directory, I just executed the 'popd' command.

NOTE- You can also use 'pushd' command to switch back to the saved directory, but that doesn't remove it from the memory, like 'popd' does.

2 Comments... add one

  1. There are many tools/shell scripts that are trying to make navigation more comfortable.

    One of which I've written:

    Here's an excerpt from the

    # make the current directory the default anchor:
    $ anc s

    # go to /etc, then /, then /usr/local and then back to the default anchor:
    $ cd /etc; cd ..; cd usr/local; anc

    # go back to /usr/local :
    $ anc b

    # add another anchor:
    $ anc a $HOME/test

    # view the list of anchors (the default one has the asterisk):
    $ anc l
    (0) /path/to/first/anchor *
    (1) /home/usr/test

    # jump to the anchor we just added:
    # by using its anchor number
    $ anc 1
    # or by jumping to the last anchor in the list
    $ anc -1

    # add multiple anchors:
    $ anc a $HOME/projects/first $HOME/projects/second $HOME/documents/first

    # use text matching to jump to $HOME/projects/first
    $ anc pro fir

    # use text matching to jump to $HOME/documents/first
    $ anc doc fir

    # add anchor and jump to it using an absolute path
    $ anc /etc
    # is the same as
    $ anc a /etc; anc -1

    # add anchor and jump to it using a relative path
    $ anc ./X11 #note that "./" is required for relative paths
    # is the same as
    $ anc a X11; anc -1

    # using wildcards you can add many anchors at once
    $ anc a $HOME/projects/*

    # use shell completion to see a list of matching anchors
    # and select the one you want to jump to directly
    $ anc pro[TAB]


Leave a Comment