Cheat Sheet #day44 - pwd

pwd Cheatsheet
Basic Usage
Print the Current Working Directory
pwd
Common Options
Print the Logical Current Working Directory
pwd -LPrint the Physical Current Working Directory (Avoids Symlinks)
pwd -P
Examples
Basic Usage
# Simply prints the current directory path pwdPrint the Logical Current Working Directory
# Prints the logical current directory path, including symlinks pwd -LPrint the Physical Current Working Directory
# Prints the actual physical directory path, resolving symlinks pwd -P
Practical Tips
Default Behavior
- By default,
pwdbehaves likepwd -L, showing the logical path.
- By default,
Using with Symlinks
- When navigating directories with symbolic links, use
pwd -Pto see the actual physical path.
- When navigating directories with symbolic links, use
Shell Built-in vs External Command
pwdis 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
pwdin 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/directoryChange Directory and Print Physical Path
cd /path/to/symlinked/directory pwd -P # Outputs: /actual/path/to/real/directoryStore 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
pwdPrint the Logical Path (Including Symlinks)
pwd -LPrint 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.




