Example 1
Basic pwd Usage
$ pwd
/home/username/Documents
Displays the absolute path of the current working directory. This is the most common usage and shows exactly where you are in the filesystem hierarchy.
Usage: Essential for verifying your location before executing commands that affect files
Example 2
Display Logical Path (with symlinks)
$ pwd -L
/home/username/projects/website
Shows the logical path including symbolic links. The '-L' option (logical) is the default behavior. If you're in a directory accessed via a symlink, it shows the symlink path.
Note: This is the default behavior, so 'pwd' and 'pwd -L' produce the same result
Example 3
Display Physical Path (resolve symlinks)
$ pwd -P
/var/www/html/production/site
Shows the physical path with all symbolic links resolved. The '-P' option (physical) displays the actual directory location, not the symlink path. Very useful when working with linked directories.
Example: If /home/username/projects/website is a symlink to /var/www/html/production/site, pwd -P shows the real location
Example 4
Use pwd in Scripts - Store Current Directory
$ CURRENT_DIR=$(pwd)
$ echo $CURRENT_DIR
$ echo $CURRENT_DIR
/home/username/scripts
Captures the current directory path in a variable for use in shell scripts. This is extremely useful for returning to the original directory after navigating elsewhere in a script.
Script Usage: Commonly used with 'cd' to save and restore directory locations
Example 5
Combine pwd with cd - Return to Previous Location
$ OLDDIR=$(pwd)
$ cd /tmp
$ cd $OLDDIR
$ cd /tmp
$ cd $OLDDIR
# Saves current dir, changes to /tmp, then returns
Demonstrates saving your current location before navigating away, allowing you to return later. This pattern is essential in shell scripts that need to change directories temporarily.
Alternative: Use 'cd -' to return to the previous directory without storing the path
Example 6
Use pwd to Create Full Path to File
$ echo "$(pwd)/myfile.txt"
/home/username/Documents/myfile.txt
Constructs an absolute path to a file in the current directory. This is useful when you need to reference a file with its full path, especially when passing file locations to other programs.
Use Case: Generating absolute paths for configuration files or logs
Example 7
Compare Logical vs Physical Paths
$ pwd -L && pwd -P
/home/username/web
/var/www/html/site
/var/www/html/site
Shows both the logical (symlink) and physical (actual) paths simultaneously. The '&&' operator executes both commands sequentially, making it easy to compare the two paths.
Useful For: Debugging symlink issues and understanding directory structure
Example 8
Use pwd in Command Substitution
$ tar -czf backup-$(basename $(pwd)).tar.gz .
# Creates: backup-Documents.tar.gz
Embeds pwd output within another command using command substitution. This example creates a tar archive named after the current directory. Combines pwd with basename to extract just the directory name.
Tip: basename extracts the last component of the path (directory name only)
Example 9
Set PS1 Prompt with pwd
$ PS1='\u@\h:$(pwd)$ '
username@hostname:/home/username/Documents$
Customizes the shell prompt to display the current directory using pwd. The prompt updates automatically as you navigate. This makes your prompt show the full path instead of just the directory name.
Better Alternative: Use \w in PS1 for built-in path display: PS1='\u@\h:\w$ '
Example 10
Use pwd to Verify Directory Before Destructive Operations
$ pwd
$ rm -rf *
$ rm -rf *
/home/username/temp
# Confirms location before deletion
# Confirms location before deletion
Always run pwd before executing destructive commands like 'rm -rf'. This safety check prevents accidental deletion of important files by confirming you're in the correct directory first.
CRITICAL SAFETY TIP: Never skip this step before running recursive delete commands!