Using the cd Command

In this guide, we will introduce you to the cd command and how you can use it to navigate your system.

cd command in linux

The cd command is one of the first things you should learn when navigating Linux, Unix, and Windows operating systems using the terminal.

cd stands for “change directory” and is used to change the current working directory. The working directory is the one that you are currently interacting with. Every time you specify a command, you are using it within the working directory.

For example, if you are in the folder /home/pi/ and use nano followed by a filename like “exampleFile” then it would be modifying the file located at /home/pi/exampleFile.

One advantage of learning this command is that it behaves reasonably similar across all operating systems that utilize it. Alongside the ls command, this is one of the first commands that you should learn when dealing with Linux.

The Syntax of the cd Command

The cd command has a very easy to understand syntax and features a minimal number of options.

Some shell environments might change the behavior of the command slightly, but for the most part, they are the same.

Below you can see the syntax of the cd command.

cd [OPTIONS] DIRECTORY

If you use the command without any arguments on Linux systems, it will automatically take you to your home directory.

To change to a directory, you are not required to type in the ending slash character (/).

To make navigating a filesystem faster, you can use the Tab key to autocomplete the names of directories.

In Linux, you must have executable permissions set on a directory to be able to change into it.

Using Absolute and Relative Directory Paths

One thing that you should learn about when using the cd command is the difference between absolute and relative directory names.

An absolute path is one that starts from the system root. An absolute path will begin with a slash (/).

For example, the following is an absolute path that starts from the root directory.

cd /home/pi/Bookshelf

A relative path is a path that starts from the current working directory. For a path to be relative, it must start either with a dot and a slash (./) or nothing.

For example, we have two relative paths below that would change the working directory to a folder called “Bookshelf” if it exists within the current working directory.

cd ./Bookshelf
cd Bookshelf

Simply put, an absolute path is a path that starts with a slash /, a relative one is one that does not.

Switching Back to the Home Directory

There are two ways of navigating to the home directory. The first way is to use the cd command without using any arguments.

The second way is to make use of the tilde (~) character. On Linux based systems, the tilde is used to represent the current users home directory.

For example, if we use cd followed by the tilde, the current working directory will be changed to our home directory.

cd ~

If you want to refer to a directory that exists within your home directory, you can use the tilde at the start of the path.

For example, let us say that we wanted to change into the “Bookshelf” directory that exists within our home directory. We can use the following command to do this.

cd ~/Bookshelf

It is also possible to change into another user’s home directory by using the following syntax.

cd ~USERNAME

All you need to do is specify the username directly after the tilde (~).

While typically, you should avoid using spaces within directory names, especially when dealing with Linux system systems.

To change to a directory that contains spaces, you will either need to wrap it in single quotes (' ') or double quotes (" ").

cd '/home/pi/directory that contains spaces'
cd "/home/pi/directory that contains spaces"

Alternatively, you can escape the space characters by using the backward slash character (\) next to each space.

cd /home/pi/directory\ that\ contains\ spaces

Typically, it will be easier to use quotes, then it will be to escape each space character.

Changing to the Parent Directory

It is possible to change to the parent directory of the current working directory with relative ease.

With the cd command, two consecutive dots (..) represent the parent directory.

For example, if you are currently sitting in the directory /home/pi/Bookshelf and want to switch to /home/pi/, all you need to do is utilize the following.

cd ../

You can even go up multiple levels by using multiple consecutive dots separated by the forward-slash character (/).

For example, if we wanted to go up to levels, from /home/pi/Bookshelf to /home we can utilize the following command.

cd ../../

It is also possible to change to another directory that exists within the parent directory.

For this example, let us say that you are currently in your home directory /home/pi, and you wanted to change into a user called pimylifeup’s home directory.

In that case, you can utilize the following command, where we change to the parent directory, then to the pimylifeup folder within that directory.

cd ../pimylifeup

Using cd to Switch to the Previous Directory

Using cd on Linux/Unix systems, it is possible to switch to the previous working directory.

To achieve this, you will need to make use of the hyphen character (-)

cd -

For example, if you are in /home/pi/ and change to /etc, you can use the hyphen (-) to return to the original directory.

You can also use the hyphen character to toggle between two directories.

~ $ cd /home/pi
~ $ cd /etc
/etc $ cd -
/home/pi
~ $ cd -
/etc
/etc $

You should now have a solid understanding of how you can use the cd command to navigate a system using its terminal.

If you feel like we have missed something or have any feedback, feel free to leave a comment.

We have guides on more Linux commands to help you get around the operating system.

Leave a Reply

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