Linux pwd Command: Directory Displaying

LinuxBeginner
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.

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 command prompt

  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 become useful when your current directory involves a symbolic link. In this step, you'll move into the symbolic link created during setup so you can see the difference directly.

Note: A symbolic link is like a shortcut to another directory. You do not need to master symbolic links yet. Focus on comparing the paths shown by each pwd command.

  1. Move into the symbolic link directory:

    cd /home/labex/project/symlink_dir
    
  2. Use pwd without any options:

    pwd
    

    You should see:

    /home/labex/project/symlink_dir
    
  3. Now use the -L option to show the logical path:

    pwd -L
    

    You should see the same symbolic link path:

    /home/labex/project/symlink_dir
    
  4. Finally, use the -P option to show the physical path on disk:

    pwd -P
    

    This time, you should see the real directory that the symbolic link points to:

    /home/labex/project/real_dir
    

Here, pwd and pwd -L both keep the symbolic link name in the result, while pwd -P resolves the link and shows the actual directory.

The key difference between the two options is:

  • -L (logical): Shows the path you used to reach the current directory, keeping symbolic link names in the result
  • -P (physical): Shows the actual directory on disk after resolving symbolic links

The important takeaway is that these commands can show different results when your current working directory includes a symbolic link.

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.