Linux cd command

Table of Contents

Introduction

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.

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

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

Output

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

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.
Now let’s see another example but with a relative path.

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'

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

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 -

Conclusion

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 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 *