# Cheat Sheet #day44 - pwd

## `pwd` Cheatsheet

### Basic Usage

* **Print the Current Working Directory**
    
    ```bash
    pwd
    ```
    

### Common Options

* **Print the Logical Current Working Directory**
    
    ```bash
    pwd -L
    ```
    
* **Print the Physical Current Working Directory (Avoids Symlinks)**
    
    ```bash
    pwd -P
    ```
    

### Examples

* **Basic Usage**
    
    ```bash
    # Simply prints the current directory path
    pwd
    ```
    
* **Print the Logical Current Working Directory**
    
    ```bash
    # Prints the logical current directory path, including symlinks
    pwd -L
    ```
    
* **Print the Physical Current Working Directory**
    
    ```bash
    # 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
    
    ```bash
    # Store the current directory path in a variable
    CURRENT_DIR=$(pwd)
    echo "The current directory is $CURRENT_DIR"
    ```
    
* **Combining with Other Commands**
    
    ```bash
    # Change directory and print the new working directory
    cd /path/to/directory && pwd
    ```
    

### Examples with Explanations

* **Change Directory and Print Logical Path**
    
    ```bash
    cd /path/to/symlinked/directory
    pwd -L
    # Outputs: /path/to/symlinked/directory
    ```
    
* **Change Directory and Print Physical Path**
    
    ```bash
    cd /path/to/symlinked/directory
    pwd -P
    # Outputs: /actual/path/to/real/directory
    ```
    
* **Store the Current Directory Path in a Variable**
    
    ```bash
    # 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**
    
    ```bash
    pwd
    ```
    
* **Print the Logical Path (Including Symlinks)**
    
    ```bash
    pwd -L
    ```
    
* **Print the Physical Path (Resolved Symlinks)**
    
    ```bash
    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.
