How to Change Directory in Linux Using the Cd Command

Being able to change directory is a fundamental part of using the terminal in Linux, macOS or Ubuntu. If you are just getting to grips with the command-line this tutorial will explain all of the different functions that the cd command has to offer.


Whenever you have a terminal window open you will be located in a single directory. This is known as the current working directory. You will need to use the cd command to navigate up and down the directory tree system and change the working directory.


The Cd Command Syntax

Firstly let's have a look at the cd command syntax.

cd: usage: cd [-L|-P] [dir]


cd takes two parameters, a set of optional flags and an optional path to a directory. The reason why the directory (dir) is optional is because we can use the second parameter to traverse up the file system hierarchy.


Here are the only two option flags that are available in cd:


  • -L - this means follow symbolic links. By default, cd follows symbolic links without having to add this flag.
  • -P - don't follow symbolic links, meaning the cd will change into the symlink directory with this option enabled.


Typically, you will never need to use these flags.


Basic Usage

The basic usage of cd would be to move into a directory within the current working directory.


cd directory


Relative vs Absolute Paths

A relative path is when you reference a directory relative to your current working directory.

If our current working directory is /var/www we could change paths like this:


cd example/public


An absolute path is a full path to the directory from the root of the file system, meaning it must begin with a / (forward slash).

cd /var/www/example/public


Moving Up A Directory

Passing .. (two dots) into the cd command will move you up one level.


cd ..


Now we have moved up from the public directory into the skillsugar directory.


Moving Up Multiple Directory Levels

For faster navigation, you can move up multiple levels in one cd command. We can do this by passing in a / (forward slash) before each .. (two dots). Let's say instead of moving from public to skillsguar, we wanted to go directly to the www folder. We could run a command like this:


cd ../..


You can also go down into a directory after traversing upward using cd. Let's take the previous command and tell it we also want to move into the example/public directory.

cd ../../example/public


We have moved up two levels and then down two into example/public.


Moving to the Home Directory

You can move to your home directory using the handy shortcut ~ (tilde)

cd ~


This shortcut  helps if you wish to access directories within the home directory. Let's say we wanted to go to the desktop. We would type:

cd ~/Desktop



Go to the Previous Directory

You can move to the previous directory you were in by passing - (dash) into the cd command.

cd -


Moving to Directories with Spaces in Their Names

Paths with spaces in their names must be escaped using a \ (backslash) as the beginning of each space character.

/var/www/example/file\ with\ space\ in\ name/public


Pro tip - press tab while typing a path and it will be autocompleted with space characters escaped.



You now know how to use the cd command to efficiently navigate around your file system, the difference between relative and absolute paths and how to move to directories with spaces in their names.

terminal file directory command