Moving around

One of the things that annoys newbies the most is moving around using a shell. Most people thing it is easier and faster if you use the mouse and goes clicking around. The fact is that shells provide a lot of useful features for efficiently traversing the filesystem hierarchy. Here are some hints.

Note: Some of the features may vary from a shell to another or even between different versions of the same shell. I will use bash 3.00 for this post.

The basics

If you want to change to another directory, you use the following command:

$ cd

This is easy enough and probably you already know it. You can tell cd to go to another directory in two different ways: either using absolute paths or using relative paths. For example, and taking into account that we are located at /opt/gnome/include/glib-2.0 and that under this, we have two sub directories (glib and gobject). We want to go to the gobject directory

$ cd /opt/gnome/include/glib-2.0/gobject

or more simply

$ cd gobject

The problem comes up when you are at /home/your_home and you want to move to /opt/gnome/include/glib-2.0/gobject. Here is where auto completion in bash is handy. Pressing <TAB> bash auto completes the rest of the path… if there is no ambiguity. For example:

$ cd /opt/gnome<TAB>

does not auto-complete. Pressing <TAB> twice (like a double click) shows the problem:

$ cd /opt/gnome<TAB><TAB>
$ gnome gnome64

Once you type the “6”, you can ask bash to complete the path:

$ cd /opt/gnome6<TAB>

# And bash expands the path, so you end up with:

$ cd /opt/gnome64

A very interesting thing to take into account is how bash looks for the paths. If the path you type starts with / bash will go to it directly. However, if you use a relative path, bash searches in the CDPATH variable, just as it does with regular files and the PATH variable.

Going home

Often, you want to your home directory as fast as possible. There are several ways of doing this. The first one is using environment variables, cause of course, you can use them to move to other locations:

$ echo $HOME
$ cd $HOME
$ pwd

As a note, the pwd command prints the working directory.

Other way to achieve the same thing is by doing the following:

$ cd ~/


$ cd ~n0str0m0

Of course, you can move to other’s homes just doing the same thing with the appropriate user name.

Note: Try what happens when you press <TAB> while typing the user name or the environment variable…

But probably, the easiest way to go home is doing this:

$ cd

The cd command without arguments takes you home.

Dealing with links

Now, lets do a nice exercise. Create a directory hierarchy like the following:

$ cd
$ mkdir test
$ cd test
$ ln -s ~/ link_to_home
$ file link_to_home

link_to_home: symbolic link to `/home/n0str0m0′

And now, lets move to link_to_home.

$ pwd
$ cd link_to_home

That seems pretty obvious. However, link_to_home was pointing to your home directory… so why didn’t we go there? Cause we went inside the link and we followed it.

$ pwd
$ cd -P link_to_home

-P uses the physical structure, while -L follows the link. This two flags are also available for the pwd command. Just try them from inside link_to_home

Using the stack

Bash provides a stack of directories. There are three commands to deal with the stack:

  • dirs:
  • Shows the dir stack.

  • pushd:
  • pushes a dir into the stack and moves to it.

  • popd:
  • takes a dir out from the stack and moves to it.

For instance:

$ pushd /usr/local
/usr/local ~
$ pushd /var/log
/var/log /usr/local ~

The “output” of the pushd command is in fact the output of a dirs command that is executed by pushd:

$ dirs -p

The ~ symbol refers to your home directory (see above)

The -p flag just prints the stack looking like a real stack :)

Now, to traverse the directories in reverse order, you just have to do:

$ popd
$ popd

I don’t use these commands in a daily basis, though I think they are useful in automated tasks (bash scripts for example).


speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*Required Fields