Navigating Directories

As you're working with the command line, you always have a working directory — that is, the directory1 you're currently in. By default, when you open a command line, you're in your home directory, but you can move around to other directories, as needed. You do this via the cd command.

For example, consider the following screenshot:


I'm in my home directory in BASH, and as you can see from the Nautilus window behind the terminal, there is a directory underneath my home directory, called Documents. In the terminal, I can move to the Documents directory using the cd command, like this:

serna@i5015ubu:~$ cd Documents

(I don't always show the entire command prompted in these examples, but in this case I left it in so that you could see exactly where I was.)

If you're using the Tab key, to have BASH automatically complete the directory names, you'll notice that it appends an extra / character to the end of the directory name. This is fine — it isn't necessary, if you're typing in the names yourself, but it's not going to hurt anything, either.

The cd command is pretty simple: you give it one parameter, which is the directory to which you want to navigate. If you wish, you can specify a longer path, including directories and subdirectories. For example, if the Documents directory has a sub-directory, called OpenOffice, you could either go to that directory like this:

serna@i5015ubu:~$ cd Documents
serna@i5015ubu:~/Documents$ cd OpenOffice

or you could do it in one step by giving a longer path, like this:

serna@i5015ubu:~$ cd Documents/OpenOffice

There is also a special directory you can pass to cd, which is the .. directory. This is a way of specifying the parent directory. For example, if I'm in the ~/Documents/OpenOffice directory, I can move back up to the home directory like this:

serna@i5015ubu:~/Documents/OpenOffice$ cd ..
serna@i5015ubu:~/Documents$ cd ..

Or, in one step, like this:

serna@i5015ubu:~/Documents/OpenOffice$ cd ../..

The .. directory refers to the parent directory, and the . directory refers to the current directory — however, I can't remember ever having had to use the . directory. Ever.

When I'm naming the directories I want to move to, they're called “relative paths,” because they're relative to the directory I'm currently in — also called my “working directory.” If I type

cd Documents

then Linux will look for a directory called Documents which is a sub-directory of the directory I'm currently in.

However, you can also use something called an “absolute path,” which would be relative to the root directory. For example, if I'm in my home directory, and want to go to the /etc/X11 directory, the hard way to do it would be like this:

serna@i5015ubu:~$ cd ..
serna@i5015ubu:/home$ cd ..
serna@i5015ubu:/$ cd etc/
serna@i5015ubu:/etc$ cd X11/

(Remember that a user's home directory is a sub-directory of the /home directory; e.g. on my machine it's /home/serna.

The easy way would be to specify the absolute path, by simply starting it with the / character, and do it one step:

serna@i5015ubu:~$ cd /etc/X11/

And one last thing: If you're ever not quite sure where you are, you can always find out by using the pwd command (which is short for “present working directory”).

serna@i5015ubu:~/Documents/OpenOffice$ pwd

In my case, I have the whole path showing in the command prompt, so it's not real useful, although it did give me the full path to my home directory, which might or might not have been useful…

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.