The pwd command stands for "print working directory". It displays the full path of the current directory you are working in. It’s a simple yet essential command used frequently in Linux terminal sessions.
pwd [options]
pwdpwd -Ppwd -L-L: Use PWD from environment, even if it contains symbolic links (default)-P: Avoid symlinks and print the actual physical directory$ pwd
/home/user/projects
$ cd ../Documents
$ pwd
/home/user/Documents
pwd to confirm your location before using commands like cp, mv, or rm.pwd -P in scripts to avoid issues with symbolic links.The pwd command in Linux is one of the simplest yet most essential commands for any user working in a terminal. Whether you’re navigating directories, writing shell scripts, or managing file paths, knowing your current location in the filesystem is crucial. This is where the pwd command comes in. In this comprehensive guide, we will explore what pwd does, how it works, syntax, real-world use cases, options, and advanced usage patterns.
The pwd command stands for “Print Working Directory.” It displays the full path of the current directory you're working in. It is especially useful when you’ve navigated deep into nested folders and need to confirm your exact location in the file system.
pwd [OPTION]
The command has no required arguments but accepts a few optional flags.
$ pwd
/home/venkatesh/projects/webapp
This output shows that the user is currently in the webapp directory inside /home/venkatesh/projects/.
The pwd command supports a few options that influence its behavior.
This is the default. It prints the logical path, including symbolic links.
pwd -L
This prints the actual physical directory path, resolving all symbolic links to their real targets.
pwd -P
cd /var/www/html
ln -s /var/www/html /home/user/website
cd /home/user/website
pwd -L # Output: /home/user/website
pwd -P # Output: /var/www/html
This demonstrates how -L shows the path with symlinks, while -P resolves them.
pwd always returns an absolute path, which starts from the root directory (/). It’s useful for creating scripts that require full directory references rather than relative paths like ../.
You can combine pwd with other Linux commands using shell features like variables and piping.
CUR_DIR=$(pwd)
This stores the current directory path in a variable.
#!/bin/bash
echo "Current Directory: $(pwd)"
cd /tmp
echo "Now in: $(pwd)"
You can modify your shell prompt to include the current directory using pwd or shell variables like $PWD.
PS1="\u@\h:\$(pwd)\$ "
This custom prompt shows username, host, and current directory.
pwd is a command, whereas $PWD is a shell variable holding the current directory path. Both generally show the same output, but pwd always invokes a command while $PWD is faster in scripts.
cd and ls in complex workflowsMost Unix-like shells (bash, zsh, sh, fish) support pwd. Some implement it as a shell builtin for speed.
type pwd
Output: pwd is a shell builtin
Symbolic links are shortcuts pointing to other directories. The pwd command helps you detect whether you're in a real directory or a symlinked path using -P.
pwd results unless the environment is controlledpwd output to avoid unexpected behavior if the directory doesn’t existif conditions or [[ -d $(pwd) ]] checksecho "You are in: $(pwd)"tar -czvf archive.tar.gz $(pwd) – Compress current folderscp file.txt user@remote:$(pwd) – Transfer file to current remote dirls -la $(pwd) – List all files in the current directorycd . to force shell to refresh pwd info after moving directories externally.Under the hood, pwd uses the system call getcwd() (get current working directory) to retrieve the absolute path. In shells, $PWD is often updated dynamically by the shell during each cd command.
While graphical file managers display your location in a window, CLI users rely on pwd to show the same. It's often more efficient to use CLI in server environments or when scripting.
Interviewers often ask about pwd to test a candidate’s understanding of directory structures, absolute vs relative paths, and script-writing fundamentals.
Sample Question: How does pwd -L differ from pwd -P?
alias mydir='pwd -P'
This alias helps quickly get the resolved physical path of the current directory.
The pwd command might seem basic, but it's an indispensable tool for Linux users, from beginners to seasoned system administrators. Its ability to provide the absolute path of the working directory plays a vital role in navigation, automation, and scripting. When combined with other commands and used wisely, pwd enhances efficiency, accuracy, and control in terminal-based workflows.
In advanced scripting scenarios, pwd becomes a critical component. Especially in CI/CD pipelines, Dockerfile creation, deployment tasks, and bash automation, the current working directory is used to determine context and execution locations.
#!/bin/bash
ORIGINAL_DIR=$(pwd)
cd /tmp
# Do something in /tmp
cd "$ORIGINAL_DIR"
This snippet ensures that a script always returns to the original location, which is useful for writing reliable and reversible shell scripts.
When working on large projects with multiple folders, dynamically capturing the current working directory using pwd helps in:
Within Docker containers, pwd is used to understand the context of the running container. Developers often use it while building images or writing Dockerfiles.
FROM ubuntu:latest
WORKDIR /app
RUN echo "Current directory is $(pwd)"
Note: The shell may not interpret $(pwd) at Dockerfile build time without a wrapper script, but it’s still conceptually valuable.
Developers frequently work within Git repositories and use pwd to determine if they're in the correct working directory before issuing Git commands.
if [[ $(pwd) == *"/my-project"* ]]; then
git pull origin main
else
echo "Not inside my-project repo"
fi
Even though pwd is a Linux command, it’s often mimicked in programming languages like Python to get the current working directory.
import os
print("Current directory:", os.getcwd())
This is functionally the same as pwd in a bash terminal.
Backup and archive scripts often use pwd to dynamically create archive names or destination folders.
tar -czf "$(basename $(pwd))_backup.tar.gz" .
This archives the current folder with a name based on the directory you're in.
rsync -avz ./ /mnt/backup/$(basename $(pwd))/
Perfect for automated scripts that run in multiple project folders.
You can use pwd in command substitution ($(pwd)) to inject the current directory path into other commands.
cp file.txt /backups/$(basename $(pwd))/
Cron runs in a limited environment and doesn’t always have predictable starting directories. Using pwd inside cron jobs helps you log or verify where the task was executed.
#!/bin/bash
echo "Running backup from $(pwd) at $(date)" >> /var/log/cron-backup.log
pwd before and after executionWhile pwd is a command that runs a system-level or shell builtin, $PWD is a shell variable. In many cases, they behave the same, but here are some differences:
$PWD is faster since it doesn’t invoke a subprocess.
pwd -P is more accurate if you want the true path (resolving symlinks), which $PWD doesn’t always provide.
In bash, you can determine the directory of the script itself using pwd combined with ${BASH_SOURCE[0]}.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "Script is in $SCRIPT_DIR"
This is widely used in complex bash applications and software install scripts.
System administrators use pwd to confirm the working directory while switching between multiple user sessions, especially using su or sudo -i.
When running terminal multiplexers like tmux or screen, each session may run in a different working directory. pwd helps identify where you are inside each pane.
In non-English locales, directory names can include UTF-8 or Unicode characters. pwd still returns the full path correctly if your locale settings are configured to support Unicode (like en_US.UTF-8).
Some shells allow embedding pwd in colorful prompts using ANSI escape sequences. This improves readability in complex terminal sessions.
PS1='\[\e[32m\]\u@\h:\w\$\[\e[m\] '
The pwd command has existed since the early days of Unix (1970s). It was part of the original Unix operating system developed at Bell Labs and has since become a standard in all POSIX-compliant systems.
-L (logical) and -P (physical) optionsalias whereami='pwd'-P if symlink resolution is important.cd. In WSL or Git Bash, pwd works.cd $(pwd) to re-enter the same directory.