Bash Print Working Directory (pwd)
Finding your current location in the file system
📍 What is the pwd Command?
The pwd command prints your current working directory's full path. It shows exactly where you are in the file system, helping you stay oriented while navigating through directories and executing commands.
# Print current directory
pwd
Output:
/home/user/Documents
pwd Command Features
Full Path
Shows complete directory path
pwd
Physical Path
Resolves symbolic links
pwd -P
Logical Path
Shows path with symlinks
pwd -L
Simple Output
Clean, scriptable output
pwd
🔹 Basic pwd Usage
The pwd (Print Working Directory) command displays the absolute path of the current directory without arguments. This is invaluable when navigating complex directory structures or verifying locations before critical operations. It's frequently used in scripts to confirm working directories and provide users with precise location information during file operations and system navigation.
# Print current working directory
pwd
# Use in combination with other commands
cd Documents
pwd
# Check location after navigation
cd /var/log
pwd
Output:
/home/user /home/user/Documents /var/log
🔹 Physical vs Logical Path
The pwd command can display either logical paths (preserving symbolic links) or physical paths (resolving all links). The -L option shows logical paths with symlinks intact, while -P resolves symlinks to show actual directory locations. This distinction matters when working with linked directories, mounted filesystems, or complex directory structures involving symbolic references.
# Logical path (default, preserves symlinks)
pwd -L
# Physical path (resolves symlinks)
pwd -P
# Example with symbolic link
cd /usr/bin
pwd -L # Shows: /usr/bin
pwd -P # Shows: /usr/bin (or actual path if symlink)
Output:
# Logical path /home/user/projects/current # Physical path (if 'current' is a symlink to 'project-2024') /home/user/projects/project-2024
🔹 Using pwd in Scripts
The pwd command is essential in shell scripts for directory verification and path construction. Scripts can store current directories in variables, verify locations before operations, or create relative paths using pwd output. This ensures scripts function correctly regardless of execution locations and enables creation of portable, location-aware automation that adapts to different working environments and directory structures.
# Store current directory in variable
current_dir=$(pwd)
echo "Working in: $current_dir"
# Use pwd in conditional
if [ "$(pwd)" = "/home/user" ]; then
echo "You are in home directory"
fi
# Create backup with current path
backup_path="$(pwd)/backup"
echo "Backup location: $backup_path"
Output:
Working in: /home/user/Documents You are in home directory Backup location: /home/user/Documents/backup
🔹 Combining pwd with Other Commands
The pwd command integrates seamlessly with other Bash commands through command substitution and variable usage. Combinations like cd $(pwd)/subdir navigate relative to current locations, while ls $(pwd) lists current directory contents explicitly. These integrations create robust scripts that handle paths intelligently and adapt to varying directory contexts during execution.
# List files with full path
ls "$(pwd)"
# Copy file to current directory
cp /tmp/file.txt "$(pwd)"
# Create directory based on current location
mkdir "$(pwd)/new_folder"
# Display current path in prompt
echo "Currently in: $(pwd)"
# Find files in current directory
find "$(pwd)" -name "*.txt"
Output:
file1.txt file2.txt folder1 file.txt copied to /home/user/Documents Directory created: /home/user/Documents/new_folder Currently in: /home/user/Documents /home/user/Documents/notes.txt /home/user/Documents/readme.txt
🔹 pwd Environment Variable
Bash automatically maintains the $PWD environment variable storing the current working directory. This variable updates with directory changes and can be accessed directly without command execution. Using $PWD in scripts is faster than command substitution and provides identical information, making it ideal for performance-sensitive operations and frequent directory reference scenarios.
# Display PWD variable
echo $PWD
# Use PWD in commands
echo "Current location: $PWD"
# Compare with pwd command
echo "pwd command: $(pwd)"
echo "PWD variable: $PWD"
# Use in file paths
touch "$PWD/newfile.txt"
Output:
/home/user/Documents Current location: /home/user/Documents pwd command: /home/user/Documents PWD variable: /home/user/Documents # newfile.txt created in /home/user/Documents