We are in the home directory on the computer but we want to to work in the new Learning_unix
directory. To change directories in Unix, we use the cd command:
cd Learning_unix
ubuntu@:~/Learning_unix$
Notice that on this system the command prompt has expanded to include our current directory. This doesn’t happen by default on all Unix systems, but you should know that you can configure what information appears as part of the command prompt.
Let’s make two new subdirectories and navigate into them:
ubuntu@:~/Learning_unix$ mkdir Outer_directory
ubuntu@:~/Learning_unix$ cd Outer_directory
ubuntu@:~/Learning_unix/Outer_directory$
ubuntu@:~/Learning_unix/Outer_directory$ mkdir Inner_directory
ubuntu@:~/Learning_unix/Outer_directory$ cd Inner_directory/
ubuntu@:~/Learning_unix/Outer_directory/Inner_directory$
Now our command prompt is getting quite long, but it reveals that we are three levels beneath the home directory. We created the two directories in separate steps, but it is possible to use the mkdir
command in way to do this all in one step.
Like most Unix commands, mkdir
supports command-line options which let you alter its behavior and functionality. Command-like options are, as the name suggests, optional arguments that are placed after the command name. They often take the form of single letters (following a dash). If we had used the -p
option of the mkdir
command we could have done this in one step. E.g.
mkdir -p Outer_directory/Inner_directory
Note the spaces either side the -p
!
Let’s change directory to the root directory, and then into our home directory
ubuntu@:~/Learning_unix/Outer_directory/Inner_directory$ cd /
ubuntu@:/$ cd home
ubuntu@:/home$ cd ubuntu
ubuntu@:~$
In this case, we may as well have just changed directory in one go:
cd /home/ubuntu/
The leading /
is incredibly important. The following two commands are very different:
cd /home/ubuntu/
cd home/ubuntu/
The first command says go the ubuntu
directory that is beneath the home
directory that is at the top level (the root) of the file system. There can only be one /home/ubuntu
directory on any Unix system.
The second command says go to the ubuntu
directory that is beneath the home
directory that is located wherever I am right now. There can potentially be many home/ubuntu
directories on a Unix system (though this is unlikely).
Learn and understand the difference between these two commands.
Frequently, you will find that you want to go ‘upwards’ one level in the directory hierarchy. Two dots ..
are used in Unix to refer to the parent directory of wherever you are. Every directory has a parent except the root level of the computer. Let’s go into the Learning_unix
directory and then navigate up two levels:
ubuntu@:~$ cd Learning_unix/
ubuntu@:~/Learning_unix$ cd ..
ubuntu@:~$ cd ..
ubuntu@:/home$
What if you wanted to navigate up two levels in the file system in one go? It’s very simple, just use two sets of the ..
operator, separated by a forward slash:
cd ../..
Using cd ..
allows us to change directory relative to where we are now. You can also always change to a directory based on its absolute location. E.g. if you are working in the /home/ubuntu/Learning_unix
directory and you then want to change to the /tmp
directory, then you could do either of the following:
$ cd ../../../tmp
or…
$ cd /tmp
They both achieve the same thing, but the 2nd example requires that you know about the full path from the root level of the computer to your directory of interest (the ‘path’ is an important concept in Unix). Sometimes it is quicker to change directories using the relative path, and other times it will be quicker to use the absolute path.
Remember that the command prompt shows you the name of the directory that you are currently in, and that when you are in your home directory it shows you a tilde character (~
) instead? This is because Unix uses the tilde character as a short-hand way of specifying a home directory.
See what happens when you try the following commands (use the pwd
command after each one to confirm the results if necessary):
cd /
cd ~
cd
Hopefully, you should find that cd
and cd ~
do the same thing, i.e. they take you back to your home directory (from wherever you were). You will frequently want to jump straight back to your home directory, and typing cd
is a very quick way to get there.
You can also use the ~
as a quick way of navigating into subdirectories of your home directory when your current directory is somewhere else. I.e. the quickest way of navigating from the root directory to your Learning_unix
directory is as follows:
ubuntu@:~$ cd /
ubuntu@:/$ cd ~/Learning_unix