The “cd” Command in Linux [6 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The cd command in Linux is a very useful command for navigation. The word cd stands for change directory. This command is used for changing the current directory of the user. cd is not your usual command it’s a shell built-in. In this article, I will explain the cd command in Linux and show you some useful examples.

A. Description

As the command word cd stands for Change Directory, it is pretty clear that this command helps a user to change the directory. The cd will take the user from the current directory (current location) to a specified directory.

B. Syntax

The syntax for the cd command is pretty simple which is the command itself followed by some specific options and destination directory name.

cd [OPTION]... [DIRECTORY]
Note: In the above syntax OPTION enclosed by a square bracket and followed by 3 dots represents that multiple options can be utilized at the same time. And anything inside the square bracket is not mandatory.

 C. Options

Many useful options are available for the cd command. I have listed some of the most used ones here. However, if you want to know more about options for the cd command you can always check the help page for the cd command. Remember man page doesn’t exist for the cd command.

help cd

Useful Options

  • -L: Force symbolic link to be followed. This option is used by default when no option is given.
  • -P: Use a physical directory structure rather than using symbolic links.
  • -e: When the -P is supplied if the current directory can’t be determined then exit with a non-zero status.
  • -@: on systems that support it, present a file with extended attributes as a directory containing the file attributes.

NB: The options in Linux CLI(Command Line Interface) are all case-sensitive. So, be cautious while using them.

Practical Examples of the “cd” Command in Linux

The cd command in Linux is an easy-to-use command which is used to change directory. Some of the most useful applications of the cd command have been illustrated below. In this article, I have worked with the following directories, you can also download those directories if you want to work with the same files. I have used the Directories below.

directory layout

Example 1: Change Directory to Root Directory Using the “cd” Command in Linux

To navigate into the Root directory you have to use the slash (/) symbol. This symbol specifies the root directory. Now follow the steps below to see how this works.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cd /

➌ Now, press the ENTER button.

Output >

As you can see from the picture below that my initial location was in my Home directory from there I changed my location to the Root directory by using the cd command. You can actually see the contents of the Root directory using ls.

moving to Root directory using "cd" Command

NB: pwd command tells your current working directory.


Similar Readings


Example 2: Change Directory to Home Directory Using the “cd” Command in Linux

To navigate into the Home directory you have to use the tilde (~) symbol. This symbol specifies the Home directory. Now follow the steps below to see how this works.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cd ~

➌ Now, press the ENTER button.

Output >

As you can see from the picture below, my initial location was on my Desktop. From there I changed my location to my Home directory by using the cd command.

moving to Home directory using "cd" Command

NB: pwd command tells your current working directory.

Example 3: Move Inside a Directory Using the “cd” Command in Linux

To move into a directory you have to use the cd command and the directory name. Now follow the steps below to see how this works. There is a directory named Sports in my home directory. I will access that directory. So follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cd Sports

➌ Now, press the ENTER button.

Output >

As you can see below picture my initial location was in my Home directory. From there I changed my location to the Sports directory by using the cd command.

going into Sports directory

NB: pwd command tells your current working directory.

Example 4: Move Into Nested directories

To move into directories that are inside other directories you have to give the entire path from your current directory to your destination directory to cd. From the diagram, you can see that there is a Bangladesh directory. Now I will access that directory. Follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cd Sports/Cricket/Bangladesh

➌ Now, press the ENTER button.

Output >

As you can see initially I was in my Home directory. From there I gave the entire path of the Bangladesh directory. And that command changed my current directory location to the Bangladesh directory.

Moving into Bangladesh directory

NB: pwd command tells your current working directory.

Example 5: Moving One Directory Backwards Using the “cd” Command

To move one location backward I will use the symbol “..” with the cd command. In the previous section, I changed my directory location to the Bangladesh directory. Now I will return to my Sports directory. Follow the steps below to see how this works.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cd ..

➌ Now, press the ENTER button.

➍ Type the following command again in the command prompt:

cd ..

➎ Finally, press the ENTER button.

Output >

As you can see I have jumped down 2 steps. I had to use cd .. twice because I jumped 2 steps.

jumping two steps down using cd

NB: pwd command tells your current working directory.


Similar Readings


Example 6: Moving Multiple Directory Backwards Using Single “cd” Command

To move multiple locations backward I will combine the 2 steps that I have used in the previous section. Now I will go to the Sports directory from the Bangladesh directory in one go. Follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cd ../../

➌ Now, press the ENTER button.

Output >

As you can see I have jumped down 2 steps. I had to use just one cd command.

jumping two steps down using one cd command

NB: pwd command tells your current working directory.

Conclusion

In this article, I have explained how the cd command in Linux works. After going through this article you should be able to navigate through your machine using the cd command. Which will make you well versed in Command Line Interface and make you less dependent on GUI(Graphical User Interface).


Similar Readings

Rate this post
Md. Rafsan Zani

Hello, I’m Md. Rafsan Zani. I have recently completed my Undergraduate from the Bangladesh University of Engineering and Technology (BUET). Currently, I’m pursuing higher studies abroad. I’m really interested in computer science and would like to learn a lot about the wonderful world of computers. Currently, I’m working as a Linux Content Developer Executive and find Linux really interesting. I certainly would like to learn more about Linux and implement them in my future studies. Read Full Bio

Leave a Comment