Linux pwd Command: Directory Displaying

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we'll explore the pwd command in Linux, a fundamental tool for navigating your digital workspace. Imagine you're a detective in a vast library of information, and pwd is your trusty compass, always ready to tell you exactly where you are. Whether you're a beginner just starting your Linux journey or looking to solidify your understanding, this lab will equip you with the skills to confidently navigate the Linux file system.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") subgraph Lab Skills linux/cd -.-> lab-209734{{"`Linux pwd Command: Directory Displaying`"}} linux/pwd -.-> lab-209734{{"`Linux pwd Command: Directory Displaying`"}} end

Understanding Your Starting Point

Let's begin our exploration by understanding where we are in the file system. The pwd command, short for "print working directory," is your first tool in this journey.

  1. Open your terminal. You'll see a prompt waiting for your command. This prompt typically ends with a $ sign.

    Terminal
  2. Type the following command and press Enter:

    pwd
  3. You should see output similar to this:

    /home/labex/project

This output tells you that you're currently in the project folder, which is inside the labex user's home directory. This is what we call an absolute path - it starts from the root directory (/) and shows the full route to your current location.

What's happening here?

Think of the Linux file system as a tree. The / at the beginning represents the root of this tree. Each subsequent name separated by / is a branch or folder. So, /home/labex/project means you're in the project folder, which is inside labex, which is inside home, which is directly under the root.

If you're wondering why you started in the project folder, it's because the LabEx environment is set up this way for convenience. In a typical Linux system, you might start in your home directory (/home/username).

Exploring Your Current Directory

Now that we know where we are, let's take a closer look at our current directory.

  1. We're going to use the ls command to list the contents of our current directory. Type:

    ls
  2. You might see some files or directories listed. If the directory is empty, you won't see any output. That's perfectly normal!

  3. Now, let's use pwd again to remind ourselves where we are:

    pwd
  4. You should see the same output as before:

    /home/labex/project

This step helps you understand that pwd always shows your current location, regardless of what files or directories are in that location.

Exploring pwd Options

The pwd command has a couple of options that can be useful in certain situations. We'll explore these now.

Note: In this step, we'll encounter some new concepts like symbolic links. Don't worry if you don't fully understand these yet - we'll cover them in detail in future lessons. For now, just focus on how the pwd command behaves with different options.

  1. First, let's use the pwd command without any options:

    pwd

    You should see:

    /home/labex/project
  2. Now, let's use the -L option (logical path):

    pwd -L

    You should see the same output:

    /home/labex/project
  3. Finally, let's try the -P option (physical path):

    pwd -P

    Again, you'll see the same output:

    /home/labex/project

You might be wondering why all these commands give the same output. The -L and -P options become relevant when dealing with symbolic links, which are like shortcuts in Windows. In our current directory, we don't have any symbolic links that affect our current path, so all versions of the command show the same result.

To see the difference, we need to navigate into the symlink_dir:

cd symlink_dir
pwd -L
pwd -P

To see the difference between -L and -P options, we would need to navigate into a directory that is a symbolic link. However, navigating between directories involves using the cd command, which we haven't learned yet. Don't worry - we'll cover the cd command and dive deeper into symbolic links in future lessons.

For now, it's enough to know that pwd has these options available for specific use cases. As you continue your Linux journey, you'll encounter situations where understanding these options becomes more relevant.

Summary

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

  1. Use pwd to identify our current location in the file system.
  2. Understand the concept of absolute paths.
  3. Use pwd in conjunction with other commands like ls.
  4. Explore the -L and -P options of the pwd command and understand their differences when dealing with symbolic links.

These skills will serve as a foundation as you continue your Linux journey, helping you always know where you are in your digital workspace.

Resources

Other Linux Tutorials you may like