Overview
pwd (Print Working Directory) is a command that displays the “absolute path” of the directory you are currently working in.
Linux directory structures can become deep and complex, and depending on terminal prompt settings, the current location may be abbreviated (e.g., ~/…/src). This command is used frequently to accurately understand where you are in the file system or to store the current directory path into a variable within a shell script.
Specifications (Arguments and Options)
Syntax
pwd [options]
Main Options
While typically used without options, you can control the behavior when navigating within symbolic links (similar to shortcuts).
| Option | Description |
-L | Displays the symbolic link as the path (Logical / Default). |
-P | Resolves symbolic links and displays the actual physical path (Physical). |
Basic Usage
Checking Your Current Location
The most basic usage is to execute the command without options. The full path from the root directory (/) to the current location is displayed.
pwd
Execution Result Example:
/home/user/documents/reports
Practical Commands
1. Checking the Physical Path Under a Symbolic Link
In system administration, it is common for a directory like /var/www/html to actually be a symbolic link to another disk. This procedure confirms the difference between the apparent path (logical) and the actual path where the files reside (physical).
Assume /home/user/logs is a symbolic link to /var/log.
# 1. Move to the link directory
cd /home/user/logs
# 2. Standard pwd (displays the link path)
pwd
# 3. pwd to show physical path (displays the actual entity path)
pwd -P
Execution Result Example:
/home/user/logs
/var/log
2. Assigning the Current Path to a Variable in a Script
When creating shell scripts, it is standard practice to save the result of pwd into a variable to perform processing based on the execution location.
# Store the current directory path in the variable CURRENT_DIR
CURRENT_DIR=$(pwd)
# Display for confirmation
echo "Current dir is: ${CURRENT_DIR}"
Execution Result Example:
Current dir is: /home/user/projects/script_test
Customization Points
- Default Behavior: In the bash shell of many Linux distributions,
pwdwithout options operates as-L(logical path). - Checking Aliases: In some environments,
pwdmight be aliased topwd -P. If the behavior seems unexpected, check it usingtype pwd.
Important Notes
- Deleted Directories: If the current directory is deleted in another terminal, running
pwdmay result in an error, orlsmay fail even if the path is displayed. In such cases, move to another directory usingcd ..or similar. - Paths Containing Spaces: Since output results may contain spaces, it is important to get into the habit of wrapping variables in double quotes, such as
"$DIR", when using them in scripts.
Advanced Application
Copying the Current Path to the Clipboard (Mac/WSL, etc.)
Manually selecting and copying long paths is tedious. It is more efficient to pass the output to a clipboard transfer tool via a pipeline.
# For macOS
pwd | pbcopy
# For Windows (WSL)
pwd | clip.exe
# For Linux (if xclip is installed)
pwd | xclip -selection clipboard
Conclusion
pwd is a simple command, but knowing the meaning of the -P option is a key to troubleshooting in Linux environments where symbolic links are heavily used. It is essential when you want to know your absolute location or obtain a path in a script. By being aware of using -P to find the actual location and ensuring you do not remain in a deleted directory, you can proceed with your work reliably without getting lost.
