Cd Command in Linux (Change Directory)
4 min read
cd (“change directory”) command is used to change the current working directory in Linux and other Unix-like operating systems. It is one of the most basic and frequently used commands when working on the Linux terminal.
The current working directory is the directory (folder) in which the user is currently working in. Each time you interact with your command prompt, you are working within a directory.
In this tutorial, we will show you how to use the
cd command to navigate your system’s directory tree.
cd is a shell builtin, and its behavior may slightly differ from shell to shell. It uses the shell environment variables to determine necessary information for its execution.
We will cover the Bash builtin version of
The syntax for the
cd command is as follows:
cd [OPTIONS] directory
The command accepts only two options that are rarely used.
−L, Follow symbolic links . By default,
cdbehaves as if
-Loption is specified.
−P, Don’t follow symbolic links. In other words, when this option is specified, and you try to navigate to a symlink that points to a directory,
cdwill change into the directory.
In its simplest form, when used without any argument,
cd will take you to your home directory.
When navigating through the file system, you can use the
Tab key to autocomplete the names of directories. Adding a slash at the end of the directory name is optional.
To switch to a directory, you must have executable permissions for that directory it.
pwd command allows you to find out what directory you are currently in.
Absolute and Relative Path Names
When specifying a directory to change to, you can use either absolute or relative path names. The absolute or full path starts from the system root
/, and relative path starts from your current directory.
By default, when you log into your Linux system, your current working directory is set to your home directory. Assuming that
Downloads directory exists in your home directory, you can navigate to it by using the relative path to the directory:
You can also navigate to the same directory by using its absolute path:
In short, if the path starts with a slash (
/) it is the absolute path to the directory.
The Parent Directory
On Unix-like operating systems, the current working directory is represented by a single dot (
.). Two dots (
..), one after the other, are representing the parent directory or in other words the directory immediately above the current one.
If you type
cd ., you will change into the current directory or, in other words, the command will do nothing.
Suppose you are currently in the
/usr/local/share directory, to switch to the
/usr/local directory (one level up from the current directory), you would type:
To move two levels up to the
/usr directory (the parent’s parent), you could run the following:
Here is another example. Let’s say you are in the
/usr/local/share directory, and you want to switch to the
/usr/local/src. You can do that by typing:
Navigate to the Previous Directory
To change back to the previous working directory, pass the dash (
-) character as an argument to the cd command:
Navigate to the Home Directory
To navigate to your home directory simply type
cd. Another way to return directly to your home directory is to use the tilde (
~) character, as shown below:
For example, if you want to navigate to the
Downloads directory, which is inside your home directory, you would type:
You can also navigate to another user’s home directory using the following syntax:
Directories with Space in Their Names
If the directory you want to change to has spaces in its name, you should either surround the path with quotes or use the backslash (
\) character to escape the space:
cd 'Dir name with space'
cd Dir\ name\ with\ space
By now, you should have a good understanding of what is the current working directory and how to use the
cd command to navigate into a different directory.
If you have any questions or feedback, feel free to leave a comment.