cd

Navigating directories is one of the most used (and most basic) skills you will develop when using Unix-like operating systems. cd is essential to properly navigate, a filesystem from a shell.

cd is short for "change directory". The command accepts an absolute path or a relative path as an argument, and changes your current working directory to the given location. This is something that you have been doing your whole life (like when you are trying to find a file on your computer; you change directories a lot!).

If you were to attempt to navigate into a folder using a GUI, you would double-click on the folder with the name that you would like to navigate to. Using cd, it is almost as easy. Simply type cd into your terminal, followed by the name of the directory that you would like to navigate to. If you wanted to navigate into the school folder on your computer, you would do following.

cd school

This changes your working directory to the path of the school directory. Remember, if you want to print your current working directory, you can use the pwd command.

Sometimes, you will find that you are in the wrong folder, and instead you want to be in the parent directory (the directory that contains the directory you are currently in). To go "up one level" to the parent directory, you type cd .. where .. is a shortcut for "parent directory". You can read more about .. here.

An example of this would be the following scenario. Let’s say that you mistakenly navigated into the school directory (/home/john/project/school), and want to go to the project directory (the parent directory for school). To do so, you could run the following command.

cd ..

Note that there is a space between cd and ..!

cd is a very powerful and simple tool. Soon you will find yourself using it without even knowing. Remember when we said "Using cd, is almost as easy" (as using a GUI) — well in fact, it is easier! If you are double-clicking through a deep set of folders, it could take you a while to get to the right place. With cd, you can navigate to your desired directory with a single command.

For instance, you can navigate directly to The Data Mine data sets folder as follows:

cd /anvil/projects/tdm/data/

When you want to navigate immediately to your home directory, you can type:

cd

without specifying a directory, and by default the shell will take you directly back to your home directory.

For instance, when Dr Ward types cd the shell takes Dr Ward directly to his home directory: /home/x-mdw

Absolute versus relative filenames

It is always possible to provide an absolute directory location when using cd for instance:

cd /anvil/projects/tdm/data/

In this example, /anvil is the base directory, with /projects inside it, etc. Note that we use / at the start of a directory location only when giving the absolute path (i.e., the full path) to a directory, i.e., when starting with the base directory and giving all of the directories from the root to the desired directory location.

On the other hand, it is possible to give relative paths, which do not start with a /. For instance, if we are already working in the directory /anvil/projects we can navigate to the data directory by typing:

cd tdm/data/

Notice that we did not start with a slash in the path. If we had (incorrectly) started with /tdm then the shell would look for /tdm at the base directory and would not find this directory.

We leave the slash off, when we want a relative path, in other words, when we want to navigate to a directory in the same place where we are already working.

Referencing files in one’s home directory

A tilde and $HOME both refer to a user’s home directory. For instance, ~/myfile.txt and $HOME/myfile.txt both refer to a file called myfile.txt in Dr Ward’s home directory. Each of these can be used from anywhere in the file system, to easily access files from one’s home directory.