Linux Directory Changing

LinuxLinuxBeginner
Practice Now

Introduction

Welcome to this Linux directory navigation lab. Directory navigation is one of the most fundamental skills for working with Linux systems. Being able to efficiently move between directories, create directory structures, and understand your current location in the filesystem is essential for all Linux users.

In this lab, you will learn how to navigate the Linux file system using the command line interface. You will practice using commands like cd (change directory), pwd (print working directory), and mkdir (make directory) to efficiently move around and create directory structures in a Linux environment.


Skills Graph

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

Understanding Your Current Location

In Linux, it is important to always know your current location in the filesystem. This first step will teach you how to determine your current directory and how to create a new directory.

Checking Your Current Directory

The pwd command (Print Working Directory) displays the full path of your current location in the filesystem:

pwd

You should see output similar to:

/home/labex/project

This means you are currently in the project directory, which is inside the labex user's home directory.

Creating a New Directory

Now that you know your current location, let's create a new directory called resources using the mkdir command (Make Directory):

mkdir resources

This command creates a new directory called resources inside your current directory. The command does not produce any output if successful.

To verify that the directory was created, you can list the contents of your current directory using the ls command:

ls

You should see resources listed in the output.

Changing Directories

Now that we have created a new directory, let's navigate into it using the cd command (Change Directory):

cd resources

This command changes your current directory to resources. Like mkdir, the cd command does not produce any output if successful.

To verify that you have successfully changed directories, use the pwd command again:

pwd

You should now see:

/home/labex/project/resources

This confirms that you are now in the resources directory.

Now that you've created and navigated to the resources directory, let's practice moving between different directories and returning to previous locations.

Returning to the Parent Directory

To move back to the parent directory (the directory that contains your current directory), you can use cd with .. as the argument:

cd ..

Check your current location:

pwd

You should see that you are back in the project directory:

/home/labex/project

Using Absolute Paths

You can also navigate to a directory using its absolute path (the full path starting from the root directory /).

Let's navigate back to the resources directory using its absolute path:

cd /home/labex/project/resources

Verify your location:

pwd

The output should be:

/home/labex/project/resources

Using the Home Directory Shortcut

Linux provides a shortcut ~ (tilde) to represent your home directory. You can use this to navigate using a path relative to your home directory.

Return to the project directory using the home directory shortcut:

cd ~/project

Verify your location:

pwd

The output should show:

/home/labex/project

In this step, you'll learn how to create multiple directories at once and navigate through a more complex directory structure.

Creating Multiple Directories with One Command

The mkdir command with the -p option allows you to create multiple nested directories in a single command. The -p option creates parent directories as needed.

Let's create a directory structure for a hypothetical engine system:

mkdir -p resources/engine/cylinders
mkdir -p resources/engine/pistons

The first command creates three directories: resources (if it doesn't already exist), engine inside resources, and cylinders inside engine.

The second command creates pistons inside the engine directory.

Verifying the Directory Structure

Let's verify the structure we created by listing the contents of the resources directory:

ls resources

You should see:

engine

Now, let's look inside the engine directory:

ls resources/engine

You should see:

cylinders  pistons

Let's navigate to the cylinders directory:

cd resources/engine/cylinders

Verify your location:

pwd

The output should be:

/home/labex/project/resources/engine/cylinders

To move up multiple directory levels at once, you can use multiple .. separated by /:

cd ../../..

This command moves up three levels: from cylinders to engine to resources to project.

Verify your location:

pwd

The output should be:

/home/labex/project

Summary

In this lab, you have learned several essential Linux directory navigation skills:

  1. Using pwd to determine your current location in the filesystem
  2. Creating directories with mkdir and nested directory structures with mkdir -p
  3. Changing directories using cd with various path types:
    • Relative paths (like resources or ..)
    • Absolute paths (like /home/labex/project/resources)
    • Home directory shortcut (~)
  4. Navigating up multiple directory levels at once

These directory navigation commands are fundamental to working efficiently in Linux environments. By mastering these commands, you have built a solid foundation for further Linux command line exploration.

As you continue your Linux journey, you'll find these skills invaluable for tasks like file management, software installation, and system configuration. Practice these commands regularly to build muscle memory and increase your efficiency when working with Linux systems.