How to Display the Current Working Directory in Linux Using the pwd Command

Updated on August 16, 2024
 How to Display the Current Working Directory in Linux Using the pwd Command header image

Introduction

The pwd command is a Linux utility used to display the working directory to enable smooth navigation in your file system. It's also known as print working directory (pwd) and enables filesystem navigation, script, file, and directory management.

This article explains how to use the pwd command in Linux to manage your working directory environment.

pwd Command Syntax

Below is the basic pwd command syntax:

console
$ pwd [options]

Within the above command, pwd outputs the full working directory path with specific filtering options.

pwd Command Options

Option Description
-L Display the logical working directory (default).
-P Display the physical working directory without symbolic links.

Understanding Logical and Physical Paths

  • Logical Path (-L): Displays the working directory path as it appears in your terminal environment including any symbolic links.
  • Physical Path (-P): Displays the working directory's physical location on the filesystem and resolves symbolic links to their real locations.

Practical Examples of the pwd Command

  1. View the active working directory.

    console
    $ pwd
    

    The above command outputs your full working directory path.

    Output:

    pwd command

  2. Display the logical working directory.

    console
    $ pwd -L
    

    The above command displays the logical working directory path including any symbolic links.

    Output:

    pwd -L command

  3. Display the physical working directory path.

    console
    $ pwd -P
    

    The above command outputs the full working directory path and resolves any symbolic links to display the directory structure on the filesystem.

    Output:

    pwd -P command

Practical Scenarios for Using pwd

Capturing the current working directory (pwd) is essential in shell scripting to enable the creation of relative paths or set up of the script's working directory. Follow the steps below to use pwd in a script.

  1. Create a new sample script file my_script.sh using a text editor such as Nano

    console
    $ nano my_script.sh
    
  2. Add the following contents to the file.

    bash
    #!/bin/bash
    current_dir=$(pwd)
    echo "The script is running in: $current_dir"
    
  3. Enable execute permissions on the script.

    console
    $ sudo chmod +x my_script.sh
    
  4. Run the script

    console
    $ bash my_script.sh
    

    Output:

    pwd in shell script

  5. Run the following command to use pwd while switching directories and verify the active position in your file system.

    console
    $ cd /home/user/documents && pwd
    

    Output:

    /home/user/documents

    The above command switches to the /home/user/documents directory and prints the working directory to verify the active file system position.

Advanced Usage Scenarios

  1. Combine pwd with cd to enable directory navigation.

    console
    $ cd /tmp && pwd
    

    The above command changes the working directory to /tmp and displays the active directory.

    Output:

    cd && pwd

  2. Use pwd with ls.

    console
    $ pwd && ls
    

    The above command displays the working directory and lists all included contents.

    Output:

    pwd && ls

  3. Store the working directory in a variable and view the result using echo.

    console
    $ current_dir=$(pwd) && echo "You are currently in: $current_dir"
    

    Output:

    store directory in variable

  4. Combine pwd with find to search search files.

    console
    $ find $(pwd) -name "*.log"
    

    The above command searches for all files with the .log extension in the active working directory.

    Output:

    pwd with find

  5. Use the pwd command in error handling.

    console
    $  if [ ! -d "/some/directory" ]; then
         echo "Directory not found, current location is: $(pwd)"
       fi
    

    The above command displays the working directory result to enable debugging in a scripts.

    Output:

    pwd error handling

  6. Use pwd in automation scripts.

    • Create a new sample script file pwd_scrpt.sh using a text editor such as Nano

      console
      $ nano pwd_script.sh
      
    • Add the following contents to the file.

      bash
      #!/bin/bash
      if [ $(pwd) != "/expected/directory" ]; then
          echo "Please navigate to /expected/directory before running this script."
          exit 1
      fi
      

      Save and close the file.

    • Enable execute permissions on the file.

      console
      $ chmod +x pwd_script.sh
      
    • Run the script

      console
      $ bash pwd_script.sh
      

      The above bash script automates tasks that depend on the active working directory to run perform a specific function.

    Output:

    pwd automation script

Conclusion

You have used the pwd command to view your working directory, navigate the filesystem, and write effective scripts. For more command options, run the man pwd command to view the pwd manual page.