Cheat Sheet #day44 - pwd

Cheat Sheet #day44 - pwd

pwd Cheatsheet

Basic Usage

  • Print the Current Working Directory

      pwd
    

Common Options

  • Print the Logical Current Working Directory

      pwd -L
    
  • Print the Physical Current Working Directory (Avoids Symlinks)

      pwd -P
    

Examples

  • Basic Usage

      # Simply prints the current directory path
      pwd
    
  • Print the Logical Current Working Directory

      # Prints the logical current directory path, including symlinks
      pwd -L
    
  • Print the Physical Current Working Directory

      # Prints the actual physical directory path, resolving symlinks
      pwd -P
    

Practical Tips

  • Default Behavior

    • By default, pwd behaves like pwd -L, showing the logical path.
  • Using with Symlinks

    • When navigating directories with symbolic links, use pwd -P to see the actual physical path.
  • Shell Built-in vs External Command

    • pwd is typically a shell built-in command, but an external version also exists at /bin/pwd.

    • The behavior might slightly differ between the built-in and the external command.

Advanced Usage

  • Using pwd in Scripts

      # Store the current directory path in a variable
      CURRENT_DIR=$(pwd)
      echo "The current directory is $CURRENT_DIR"
    
  • Combining with Other Commands

      # Change directory and print the new working directory
      cd /path/to/directory && pwd
    

Examples with Explanations

  • Change Directory and Print Logical Path

      cd /path/to/symlinked/directory
      pwd -L
      # Outputs: /path/to/symlinked/directory
    
  • Change Directory and Print Physical Path

      cd /path/to/symlinked/directory
      pwd -P
      # Outputs: /actual/path/to/real/directory
    
  • Store the Current Directory Path in a Variable

      # Useful in scripts for returning to the original directory after performing operations
      ORIG_DIR=$(pwd)
      # Change to another directory
      cd /another/directory
      # Do something in the new directory
      # ...
      # Return to the original directory
      cd "$ORIG_DIR"
    

Quick Reference

  • Print the Current Working Directory

      pwd
    
  • Print the Logical Path (Including Symlinks)

      pwd -L
    
  • Print the Physical Path (Resolved Symlinks)

      pwd -P
    

This cheatsheet covers the essential commands and options for using pwd effectively, from basic usage to advanced scripting and handling symbolic links. Adjust the commands according to your specific requirements and environment.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!