Linux cd Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will learn about the Linux cd command and how to navigate the file system using it. We will cover the purpose and syntax of the cd command, as well as explore the use of relative and absolute paths. By the end of this lab, you will have a better understanding of how to effectively change directories and move around the file system using the cd command.

The lab covers the following steps:

  1. Understand the Purpose and Syntax of the cd Command
  2. Navigate the File System Using cd Command
  3. Explore Relative and Absolute Paths with cd Command

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") subgraph Lab Skills linux/cd -.-> lab-422591{{"`Linux cd Command with Practical Examples`"}} linux/pwd -.-> lab-422591{{"`Linux cd Command with Practical Examples`"}} linux/mkdir -.-> lab-422591{{"`Linux cd Command with Practical Examples`"}} linux/ls -.-> lab-422591{{"`Linux cd Command with Practical Examples`"}} linux/touch -.-> lab-422591{{"`Linux cd Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the cd Command

In this step, we will learn about the purpose and syntax of the cd command in Linux. The cd command is used to change the current working directory.

The basic syntax of the cd command is:

cd [directory]

Here, [directory] is the path of the directory you want to change to. The path can be either absolute or relative.

For example, to change to the /home/labex/project directory, you can use:

cd /home/labex/project

This is an absolute path, as it starts from the root directory (/).

To change to a directory relative to the current working directory, you can use:

cd directory_name

This will change the current working directory to the directory_name directory, which is located within the current working directory.

You can also use the following shortcuts with the cd command:

  • cd ~ or just cd to go to the home directory (/home/labex)
  • cd - to go to the previous working directory
  • cd .. to go to the parent directory of the current working directory

Example output:

labex@ubuntu:~/project$ cd /home/labex/project
labex@ubuntu:/home/labex/project$ cd ..
labex@ubuntu:/home/labex$ cd -
/home/labex/project
labex@ubuntu:/home/labex/project$

In this step, we will learn how to navigate the file system using the cd command.

Let's start by creating a few directories and files in the ~/project directory:

cd ~/project
mkdir dir1 dir2 dir3
touch file1.txt file2.txt

Now, let's navigate to the different directories using the cd command:

cd dir1
## We are now in ~/project/dir1
cd ../dir2
## We are now in ~/project/dir2
cd ../../dir3
## We are now in ~/project/dir3

Here, we used the following techniques:

  • cd dir1 to navigate to the dir1 directory, which is a subdirectory of the current directory (~/project).
  • cd ../dir2 to navigate to the dir2 directory, which is a sibling directory of dir1. The .. refers to the parent directory.
  • cd ../../dir3 to navigate to the dir3 directory, which is two levels up from the current directory (~/project/dir3).

You can also use absolute paths with the cd command:

cd /home/labex/project/dir1
## We are now in /home/labex/project/dir1

Example output:

labex@ubuntu:~/project$ mkdir dir1 dir2 dir3
labex@ubuntu:~/project$ touch file1.txt file2.txt
labex@ubuntu:~/project$ cd dir1
labex@ubuntu:~/project/dir1$ cd ../dir2
labex@ubuntu:~/project/dir2$ cd ../../dir3
labex@ubuntu:~/project/dir3$ cd /home/labex/project/dir1
labex@ubuntu:/home/labex/project/dir1$

Explore Relative and Absolute Paths with cd Command

In this step, we will explore the difference between relative and absolute paths when using the cd command.

Relative Paths:
Relative paths are defined relative to the current working directory. For example, if the current working directory is /home/labex/project, then:

cd dir1
## This will change the directory to /home/labex/project/dir1

Here, dir1 is a relative path, as it is relative to the current working directory.

Absolute Paths:
Absolute paths are defined starting from the root directory (/). For example:

cd /home/labex/project/dir1
## This will change the directory to /home/labex/project/dir1

Here, /home/labex/project/dir1 is an absolute path, as it starts from the root directory.

Let's explore the differences between relative and absolute paths:

## Current working directory is /home/labex/project
cd dir1
## We are now in /home/labex/project/dir1
cd ..
## We are now back in /home/labex/project
cd /home/labex/project/dir2
## We are now in /home/labex/project/dir2

In the above example, we used both relative (dir1, ..) and absolute (/home/labex/project/dir2) paths with the cd command.

Example output:

labex@ubuntu:~/project$ cd dir1
labex@ubuntu:~/project/dir1$ cd ..
labex@ubuntu:~/project$ cd /home/labex/project/dir2
labex@ubuntu:~/project/dir2$

Summary

In this lab, we learned about the purpose and syntax of the cd command in Linux, which is used to change the current working directory. We explored how to navigate the file system using absolute and relative paths, as well as various shortcuts like cd ~, cd -, and cd ... We also practiced creating directories and files, and then using the cd command to navigate between them, demonstrating the flexibility and power of this essential Linux command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like