cd Command: How to Change Directory in Linux

December 22, 2023

Introduction

The current working directory is the directory your terminal window or command prompt currently operates in.

When you open a new terminal session in Linux, it starts in the Home directory. The cd command allows you to switch to a different directory.

Learn how to use the cd command and 11 different ways to change directories in Linux.

cd Command: How to Change Directory in Linux

Prerequisites

Linux cd Command Syntax

The cd command in Linux has the following syntax:

cd [options] [directory]

In the command above:

  • cd: Invokes the cd command.
  • [options]: Options change the way the command executes.
  • [directory]: Path to the directory you want to move into.

For instance, to move to the Desktop directory:

cd /home/phoenixnap/Desktop

The system displays the new current working directory in the terminal:

Path to the current working directory

Use the pwd command to check the full path to the current working directory:

pwd
Using the pwd command to show the current working directory

Linux cd Command Options

The cd command uses the following options:

  • -L: Force following symbolic links. This option is enabled by default.
  • -P: Do not follow symbolic links. This option resolves the symbolic link to determine the parent directory before moving to the user-requested directory.

How to Use Linux cd Command in Linux

The Linux cd command offers several ways to navigate and change the working directory. You can:

  • Use a relative path to navigate to a directory relative to your current location.
  • Enter an absolute path to move to any directory in the file system.
  • Go up one level to the parent directory or move directly to the root directory.
  • Find and move to directories even when you only know part of their name.

Note: The cd command is a built-in shell command. This means that its behavior varies slightly between shells since it uses shell environment variables. Learn more in our guide to environment variables in Linux.

Changing Directory

To change to a new working directory, use the cd command and enter the directory path:

cd [path to directory]

For instance, moving to Example_Directory, located in the Home directory:

cd Example_Directory
Using the cd command to change the current working directory

Changing Directory and List Content

Use the && flag to append the ls command to the cd command. The system changes to a new directory and lists its content simultaneously.

cd [path to directory] && ls

Using the previous example:

cd Example_Directory && ls
Using a combination of cd and ls command to change to a new working directory and list its contents

Note: Add different ls command options to change the way directory contents are displayed.

Changing Directory Using Absolute Path

Using an absolute path allows you to change to any directory in the Linux file system. For example, the following command allows you to change to the Downloads directory using its absolute path:

cd /home/phoenixnap/Downloads
Changing to a new working directory using the absolute path

Change the path to your own path.

Changing Directory Using Relative Path

A relative path is a path to a directory relative to your current location. A relative path is best used when changing to a subdirectory of the current working directory.

In the example above, the Downloads directory is a subdirectory of Home. Entering the Downloads relative path prompts the system to change to that directory:

cd Downloads
Changing to a new working directory using the relative path

Since the path to the current directory is /home/phoenixnap, omitting that part of the absolute path to the Downloads directory /home/phoenixnap/Downloads results in a relative path Downloads.

Changing to Previous Directory

Adding a dash symbol (-) to the cd command returns the shell to the previous working directory. For instance, after moving from Downloads to Example_Directory, return to Downloads with:

cd -
Changing back to the previous working directory

Changing to Parent Directory

Append two dots (..) to the cd command to change to the parent of the current directory:

For example, to move from Example01 to its parent directory Example_Directory:

cd ..
Changing to the parent of the current working directory

Changing to Root Directory

Add the slash symbol (/) to the cd command to move into the system's working directory:

cd /
Changing to the system root directory

Changing Back to Home Directory

In Linux, the Home directory represents the default working directory. Using the cd command without any options or paths changes back to the default working directory:

cd

The absence of the current working directory path indicates that you are in the default working directory:

Changing back to the default working directory

Another way to do this is to add the tilde symbol (~) to the cd command:

cd ~

Changing to Another User's Home Directory

Change to another user's Home directory by adding the tilde symbol (~) appended with the appropriate username.

cd ~[username]

For instance, changing to the Home directory of a user named alex:

cd ~alex
Changing to another user's Home directory

Changing to a Directory with Spaces in the Name

If the directory name contains blank spaces, you can put the name within single quotation marks (' '):

cd 'Directory name with blank spaces'

Alternatively, append a backslash symbol (\) to every word in the name except the last one:

cd Directory\ name\ with\ blank\ spaces

For example, use the following command to change to a directory named This is a directory using quotation marks:

cd 'This is a directory'
Changing to a directory in linux using the cd command and quotation marks.

Enter the following command to switch to the same directory using backlashes:

cd This\ is\ a\ directory
Changing to a directory in Linux using the cd command and backlashes.

Autocomplete Directory Name

If you don't know the name of the directory you are trying to move to, most Linux shells, like Bash and Zsh, have an autocomplete feature. After you start typing the directory name, press the Tab key on your keyboard to get autocomplete suggestions.

For instance, if you know that the directory name starts with di, type in di and press Tab. The system suggests an existing directory name that fits the partial input.

Conclusion

After reading this tutorial, you should be able to use the Linux cd command to navigate and change the current working directory in the terminal window.

Learn more about other Linux commands in our Linux commands cheat sheet.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How To Use shred Linux Command
March 31, 2021

Deleting a file in Linux or any other OS does not actually remove the data from the drive. The best practice is to use the shred command to permanently destroy sensitive data so that it cannot be recovered.
Read more
Linux set Command & How to Use it {9 Examples}
November 23, 2021

The set command allows you to show or change the shell and environment variables in a Linux system. Follow this tutorial to learn how to use the set command and see practical examples.
Read more
How To Use the touch Command in Linux
November 22, 2021

The touch command creates new files. However, advanced options deal with file timestamps. Follow this tutorial to learn how to use touch in the command line through examples.
Read more
tar Command in Linux With Examples
November 9, 2021

The tar command is is used to create and extract archived and compressed packages on Linux. Follow this tutorial to learn about the various options available and how to utilize the powerful tar command.
Read more