Linux cd Command: Directory Changing

LinuxLinuxBeginner
Practice Now

Introduction

Welcome to the exciting world of Linux navigation! In this lab, you'll learn how to use the cd command, your digital compass in the Linux file system. Imagine you're an explorer in a vast, interconnected city of directories. The cd command is your trusty vehicle, allowing you to move swiftly between different "neighborhoods" (directories) of your Linux "city" (file system).


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`") subgraph Lab Skills linux/cd -.-> lab-209733{{"`Linux cd Command: Directory Changing`"}} linux/pwd -.-> lab-209733{{"`Linux cd Command: Directory Changing`"}} linux/mkdir -.-> lab-209733{{"`Linux cd Command: Directory Changing`"}} linux/ls -.-> lab-209733{{"`Linux cd Command: Directory Changing`"}} end

Understanding Your Starting Point

Before we start our journey, let's find out where we are in our Linux city.

  1. Open your terminal. This is your control panel for navigating the Linux file system. It might look intimidating at first, but think of it as your GPS device in this digital world.
Terminal
  1. Type the following command and press Enter:
pwd

This command stands for "print working directory" and shows your current location. Don't worry if you make a typo - you can always retype the command.

You should see an output similar to this:

/home/labex/project

This means you're in the project folder, which is inside the labex folder, which is inside the home folder. Think of it as your current address in the Linux city.

Note: If you see a different output, don't panic! It just means you're starting from a different location. The important thing is to understand what the output represents.

Your First Move - Going Home

Now that we know where we are, let's make our first move. We'll go to your home directory, which is like your base camp in the Linux city.

  1. Type the following command and press Enter:
cd ~

The tilde (~) is a shortcut that represents your home directory. It's like telling your GPS, "Take me home!"

  1. Now, let's check where we are:
pwd

You should see:

/home/labex

Congratulations! You've just made your first move using the cd command. If you don't see /home/labex, don't worry. Try the cd ~ command again, and make sure to include the space between cd and ~.

Exploring the Neighborhood - Moving to a Specific Directory

Now that we're home, let's venture out to a specific directory. We'll move to the project directory, which is where we started.

  1. Type the following command and press Enter:
cd project

This is like telling your GPS, "Take me to the project neighborhood." If you get an error saying the directory doesn't exist, don't worry! Try ls to list the directories available, and choose one you can see.

  1. Check your new location:
pwd

You should see:

/home/labex/project

You've successfully moved to a specific directory! If you're in a different directory, that's okay too. The important thing is that you've moved from your home directory to another one.

Moving Up - Returning to the Parent Directory

Sometimes, we need to move up in the directory structure. Let's go back to our home directory.

  1. Type the following command and press Enter:
cd ..

The two dots (..) represent the parent directory. It's like telling your GPS, "Take me to the neighborhood one level up."

  1. Check your location:
pwd

You should see:

/home/labex

You've moved up one level in the directory structure! If you're not in /home/labex, don't worry. The important thing is that you've moved up one level from where you were before.

Quick Return - Going Back to the Previous Directory

Linux provides a handy shortcut to return to the directory you were in before your last move.

  1. First, let's move to a different directory. If you're in your home directory, you can use:
cd project

If project doesn't exist, use ls to find an available directory and cd into it.

  1. Now, let's use the shortcut to return to the previous directory:
cd -

This command is like telling your GPS, "Take me back to where I just was!"

  1. Check your location:
pwd

You should be back in the directory you were in before step 1. This - (dash) is a useful shortcut when you need to quickly switch between two directories.

So far, we've been using relative paths - paths relative to our current location. Now, let's use an absolute path to move to a specific location, regardless of where we are.

  1. Type the following command and press Enter:
cd /etc

This is an absolute path, starting from the root directory (/). The /etc directory is an important system directory in Linux that contains configuration files. It's like giving your GPS the full address, starting from the country all the way down to the specific building.

  1. Check your location:
pwd

You should see:

/etc

You've navigated to a specific location using an absolute path! This directory exists on all Linux systems, so you should be able to access it without any errors.

  1. Let's take a quick peek at what's in this directory:
ls

You'll see a list of many configuration files and directories. Don't worry about understanding all of these now - we're just exploring!

  1. Now, let's return to our home directory using an absolute path:
cd /home/labex

This command will take you directly back to your home directory, no matter where you are in the file system.

Remember, using absolute paths is like using the full postal address - it works from anywhere, but it's often longer to type than relative paths.

Linux Easter Egg - The Maze of Twisty Little Passages

Now that you've mastered the basics of navigation, let's have some fun with a Linux easter egg that's all about navigation!

  1. First, let's make sure we're in our home directory:
cd ~
  1. Now, let's create a directory structure for our maze:
mkdir -p maze/twisty/little/passages

This command creates a nested directory structure. Don't worry about the details of this command for now - we'll learn about creating directories in a future lab.

  1. Now, let's navigate through our maze:
cd maze/twisty/little/passages
  1. Check where you are:
pwd

You should see:

/home/labex/maze/twisty/little/passages

Congratulations! You've navigated through a maze of twisty little passages, all alike! This is a reference to a classic text adventure game called "Colossal Cave Adventure," which heavily influenced early computer gaming and hacker culture.

  1. Let's return home:
cd ~

This lab may not be as complex as the original Colossal Cave, but it shows how you can use the cd command to navigate through even the twistiest of directory structures!

Summary

In this lab, we've explored the cd command, your trusty navigation tool in the Linux file system. We've learned how to:

  1. Check our current location using pwd
  2. Move to the home directory using cd ~
  3. Navigate to specific directories using relative paths
  4. Move up in the directory structure with cd ..
  5. Quickly switch between two directories using cd -
  6. Use absolute paths for precise navigation
  7. Have fun with a Linux easter egg by creating and navigating a maze
  8. Understand additional cd command options

The cd command has a few more options that can be useful in specific situations:

  • cd: Without any arguments, this takes you to your home directory.
  • cd /: This takes you to the root directory of the entire file system.
  • cd $HOME: This is another way to go to your home directory.

These options provide additional flexibility when navigating your Linux file system. Feel free to try them out!

With these skills, you're now equipped to navigate the Linux file system like a pro! Remember, practice makes perfect. The more you use these commands, the more comfortable you'll become with Linux navigation. Don't be afraid to explore – in Linux, you can always find your way back home with cd ~. Happy exploring in your Linux city!

Resources

Other Linux Tutorials you may like