How to Change Directory Using the cd Linux Command

cd-linux-command

The cd Linux command stands for “change directory,” and it is one of the most widely used Linux commands. When executed, the cd command will change your current working directory to the specified one. You can specify the new working directory in different ways, including using the absolute or relative paths.

This post digs in on the cd Linux command and gives various examples of how to utilize the command to change the current working directory quickly.

cd Linux Command Syntax

Using the cd command in Linux is easy, and it follows the below syntax.

The cd command can take two options, but you will rarely use them. The -L flag signals the command to follow symbolic links. The other is the -P option, which signals not to follow symbolic links.

cd Command Examples

We’ve introduced the cd command, but you need to give practical examples of how to use it to understand it. Below are different demonstrations of how to use the cd command in Linux.

Example 1: Working with Relative and Absolute Paths

Anytime you open your terminal, the current working directory is your /home/user/. You can verify this by running the pwd command to print the working directory, as in the example below.

pwd-command

To change the current working directory, you can use the absolute path that starts from the root (/) or the relative path that starts from the current directory.

For instance, when in the home directory, you can move to the Downloads/ by specifying its relative path.

cd-linux-command

However, if you are in another current directory, using the absolute path would be achieved as follows.

cd-command-add-path

Example 2: Change the Current Working Directory to the Parent Directory

The current directory is marked with a single dot, while the parent directory is marked with two dots (..) to signal one level up the current directory.

Assuming you are in the Downloads/, the parent directory is the home directory. Therefore, running the command below will position you in the home directory, which is one level up from the current location.

cd-linux-command-parent-directory

Here’s another example where we are in a subfolder and want to change the directory to its parent directory using the cd command. Notice how the pwd command prints the working directory before and after running the cd .. command.

cd-command

Instead of going one level up the current working directory, you can opt to take it two levels up. Doing so will position you in the parent directory of the parent directory of the current directory.

The example below shows our current working directory as Downloads/names. However, running the command below takes us to the home directory.

cd-up-level

You can take it further by specifying another directory two levels up from your current working directory. For instance, using the same current directory, Downloads/names, we can position ourselves in the Desktop/ which is in the home directory. For that, we would execute our command as shown below.

cd-command

Example 3: Change Directory to Previous Directory

It’s possible to quickly revert to the previous directory by adding a dash with the cd command. This option is handy when you don’t want to type the path to the target working directory when it’s the previous one you were in.

Here’s an example and the command to run.

change-directory-to-home-directory

Example 4: Change Directory to Home Directory

With the cd Linux command, you can quickly change the directory and access your home directory by typing the tilde sign (~). You can also navigate to your home directory by typing the cd command without any arguments. Either command will work.

cd-chome

Example 5: Change Directory to Root Directory

Similar to how we’ve accessed our home directory, it’s possible to run the cd command to change the directory to the root. For that, use the slash sign (/).

cd-root

Example 6: A Case of Directory Names with Spaces

Sometimes, you may want to change the current working directory, but the target directory has space in the name. There are two workarounds for it. First, use single quotes when specifying its name. Here’s an example.

cd-directory

Alternatively, enclose the directory name with a backslash. Doing so will escape the space character, and the cd command will run smoothly, like in the example below.

pwd-command

Conclusion

The cd Linux command helps with changing the current working directory, and you can use it with different options depending on what your target current working directory is. Throughout this post, we’ve given different examples of how to use the cd command in Linux to navigate the Linux file system. Hopefully, you now understand what the Linux cd command is and how to use it.

Found this helpful? - Share it!

1 thought on “How to Change Directory Using the cd Linux Command”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top