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.
Prerequisites
- A system running a Linux distribution.
- A user account with sudo privileges.
- Access to the terminal window or command line.
Linux cd Command Syntax
The cd
command in Linux has the following syntax:
cd [options] [directory]
In the command above:
cd
: Invokes thecd
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:
Use the pwd command to check the full path to the current working directory:
pwd
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
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
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
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
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 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 Root Directory
Add the slash symbol (/
) to the cd
command to move into the system's working directory:
cd /
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:
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 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.
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.