Linux cd command

Table of Contents


The Linux cd command is one of the basic commands and allows you to change directory.
While this command is pretty simple to use, this post tells you how to best use all of its parameters.

In the next sections we will start to describe the difference between absolute and relative path and then we will describe the possible uses of the Linux cd command.

Absolute and relative path

Before we start talking about the Linux cd command, it is best to remember the difference between absolute path and relative path.
If you already know the difference, please move on to the next section otherwise read on.

An absolute path starts from the root and typically it is recognized because it starts with the character /.

Example of absolute path
Example of absolute path

A relative path, on the other hand, starts from the current directory and never begins with the character /.

Example of relative path

As we said before, a relative path never starts with / character, indeed if you look at the picture above there is a tilde character before the path that represents the absoulte path before the relative one.

In this example:

  • ~ represents the absolute home directory path (/home/<your_user>/)
  • dev_in_simple_words is the relative path

How to know which directory we are in

In this tutorial and beyond, it can be useful to know which directory you are in.
Linux provides you with a command to use in the terminal to find out which directory you are in. This command is called pwd.

Its use is really simple, just type it in the terminal and you will see the absolute path of where you are with the current session of the cmd.

Here is an example of use:


cd command

The cd command allows you to move between directories.
As always I do, I suggest you open a terminal and type the following command to find all the information about this command.

cd --help


Linux cd command help output
Linux cd command help output

As shown in the previous figure, the structure of the cd command in Linux looks like this:

cd [-L|[-P [-e]] [-@]] [dir]

It is possible to distinguish the following parts:

  • cd is the main command
  • -L, -P, -e and -@ are the optional parameters
  • dir is the destination directory

cd command – change directory (absolute path)

Let’s start from the beginning and see how to change directory.

cd /usr/local

This command will allow you to change your working directory to /usr/local/.

In this particular example we have used an absolute path.
In the next section we will see another example using a relative path.

cd command – change directory (relative path)

In the previous chapter we saw how to change directories using an absolute path.
Of course, a relative path can also be used, and you can see an example here.

cd Desktop/

In this case we are in the home directory (/home/<user>/) and we decided to move to the Desktop that it is in the same directory.

cd command – directory with spaces

It may happen that some directories on your system have names that contain spaces.
In this case it is good to surround the name with quotes as in this case:

cd 'Directory with spaces in the name'

If you want to avoid the use of quotes, you can also use another technique.
in fact, you can escape the space character like this:

this\ is\ a\ test/

Here you can see the same example as before but with space escaping:

cd Directory\ with\ spaces\ in\ the\ name/

cd command – go to root directory

If you are interested in going straight to the root directory no matter where you are, this command is for you:

cd /

Using the cd command with only the / character will move you directly to the root folder.

cd command – go to home directory

The cd command also allows you to go directly to the home directory:

cd ~

To do this just use cd with the tilde symbol as shown in the example above or cd without any directory.
In this case it is understood that we are talking about the home directory.


cd command – change directory backwards

In the previous sections we have seen how to change directory but is it possible to go back without specifying the whole path again?
Obviously yes and also in this case the cd command comes to our aid.
To go back one directory just use the following command:

cd ..

This will move you to the previous level, but what if we wanted to go back 3 directories at once?

cd ../../../

With this command instead, it is possible to go back to the directory where you were the last time:

cd -


In this post we have seen how to use the linux cd command and all its options.
I hope this post was useful to you and if you have any question, please do not hesitate to ask me in a comment below.
If everything is clear and you want to know more about Linux commands move on to the next article.

Leave a Comment

Your email address will not be published. Required fields are marked *