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.
- A system running a Linux distribution.
- A user account with sudo privileges.
- Access to the terminal window or command line.
Linux cd Command Syntax
cd command in Linux has the following syntax:
cd [options] [directory]
In the command above:
cd: Invokes the
[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:
The system displays the new current working directory in the terminal:
Use the pwd command to check the full path to the current working directory:
Linux cd Command Options
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
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.
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.
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:
Changing Directory and List Content
&& 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
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:
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:
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:
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:
Changing to Root Directory
Add the slash symbol (
/) to the
cd command to move into the system's working 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:
The absence of the current working directory path indicates that you are in the default working directory:
Another way to do this is to add the tilde symbol (
~) to the
Changing to Another User's Home Directory
Change to another user's Home directory by adding the tilde symbol (
~) appended with the appropriate username.
For instance, changing to the Home directory of a user named alex:
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'
Enter the following command to switch to the same directory using backlashes:
cd This\ is\ a\ directory
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.
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.