Beginners Guide for cd Command on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

The UNIX/Linux CD command is popularly used to move into different directories from the current working directory using the command line (or terminal).

When you open your terminal, Linux will use your home directory as the current working directory, so to navigate into a different directory, you can use the cd command.

Tutorial Details

DescriptionCD (Changing Directory)
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitescd
Internet RequiredNo

Syntax of the CD Command

The cd command takes two arguments: one is the option, and the other is the directory path.

$ cd [OPTION] [DIRECTORY]

The following are the known options for the cd command:

OptionsDescription
-LForce following symbolic links (enabled by default).
-PInstead of symbolic links, specifying this option will follow the physical directory structure of the user-requested directory.
-eIf the “-P” and current directory could not be determined successfully, exit with a non-zero status.
-@Present a file with extended attributes as a directory containing the file attributes.

Working Scenario in this Article

Although CD is a very common command like ls or echo, the fact that you are reading this article indicates that you are a new Linux user.

For new Linux users, you can check the following tree map of a Linux filesystem with multiple directories and nested directories that I will use to show you the usage of the cd command.

Filesystem tree map

Each time you move into a different directory, you can use the pwd command to check the absolute path of the current working directory.

Considering that, let’s move on to the practical.

Go to the Specified Directory

1. Execute the following command to move into the “~/Documents” directory.

$ cd Documents/

Output:

Navigating to Documents directory using cd command

2. Execute the following command to move into the “/var/www/html” directory from your current directory.

$ cd /var/www/html

Output:

Navigating to html directory using cd command

3. The following command will move you into the parent directory of the current directory (it’s one step above).

$ cd ..

Output:

Going one step back in directory tree using cd command

4. To go back to the previously active directory, use the “” dash symbol, as shown.

$ cd -

Output:

Reverting back to the previous directory using cd command

5. Enter into another user’s home directory (ex: “jake“) by specifying the home directory name with the “~” tilde symbol.

📝
To move into a different user home directory, you must have sufficient permission or be a root user.
$ cd ~jake

Output:

Navigating to different user home directory using cd command

6. Go to the root directory from the current working directory.

$ cd /

Output:

Navigating to root directory using cd command

7. Move into your own home directory by using the following command.

$ cd

#OR

$ cd ~

Output:

Navigating to current user home directory using cd command

8. Enter the directory with a space.

$ cd Documents/'directory with space'/

#OR

$ cd Documents/directory\ with\ space/

Output:

Navigating to directory with space in name using cd command

9. Switch to another directory using the current working directory.

$ cd ../proj/scripts/

Output:

Navigating to different directory from current using cd command

10. Move two directories from where you are now.

$ cd ../../

Output:

Going two steps back in directory tree using cd command

11. Move into another directory from your current working directory.

$ cd ../Downloads/

Output:

Navigating to Downloads directory using cd command

12. Navigate to another directory without specifying its name using “Tab” key.

$ cd ../Doc<TAB>/p<TAB>/s<TAB>/

Output:

Navigating to different directory using Tab key to auto fill name

Not that the “<TAB>” string is used to refer to the tab key on the keyboard, and also, the first character of the directory name you are specifying should be unique; if any other directory with the same character exists, then specify a few more characters.

13. Move into the first directory that starts with the letter “Dow” in your home directory.

$ cd ~/Dow*

Output:

Navigating to directory starting with the keyword using cd command

Note that in home directories, there are usually three directories that start with “D” (Desktop, Documents, and Downloads).

That is why, instead of “D“, two extra letters “ow” are specified to be more specific.

14. Put the current directory on the stack and move into another directory.

Pushd and popd commands are used to stack the present directory in memory and move into another directory.

When the job is done, fire the popd command, and the current location will change back to the stacked directory in memory.

For example, use the pushd directory to move into the “/var/www/html” path.

$ pushd /var/www/html/

Output:

Adding current path to stack using pushd command

Now do what you want, and when the job is done, execute the popd command, and you will be moved to the stacked directory in your memory.

$ popd

Output:

Navigating to directory using popd command

15. Listing all the content of the directory after moving into it.

$ cd ~/Documents/ && ls

Output:

Issuing ls command after moving to another directory using cd command

That’s all for now.

If you have a question or an example that should be included in this article, feel free to tell us in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.