pwd
Print the full path of the current working directory
By CMD Script Team · 4 min read · Last updated
pwd [OPTIONS]Options
| Flag | Description |
|---|---|
-L | Print the logical path, including symlinks as traversed (default) |
-P | Print the physical path, resolving all symlinks to their real target |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
pwd ("print working directory") prints the absolute path of the directory the current
shell session is in. It's one of the simplest commands available, but it's essential for
confirming your location before running path-sensitive or destructive commands.
Beginner examples
pwd— print the current directory's full absolute pathcd /var/log && pwd— confirm you actually landed where you intended aftercdpwd -P— print the path with all symlinks resolved to their real targets- Use it inside scripts to capture the starting directory:
START_DIR=$(pwd)
pwd
Advanced examples
- Capture the working directory to return to it later in a script:
START_DIR=$(pwd); cd /tmp; ...; cd "$START_DIR". - Compare logical vs physical location when navigating through symlinks:
cd /var/symlinked-dir && pwd(shows the symlink path) vspwd -P(shows the real path). - Use in a shell prompt or script to dynamically reference the current location without
hardcoding paths:
cp file.txt "$(pwd)/backup/". - Combine with
cd -Pfor a fully resolved change of directory followed by a confirming physicalpwd.
cd /var/log && pwd -P
Common mistakes
- Assuming
pwdandpwd -Palways print the same thing — they diverge whenever any component of the path traversed to reach the current directory is a symlink. - Hardcoding relative paths in scripts instead of anchoring them with
$(pwd)or an absolute path, causing scripts to behave differently depending on where they're invoked from. - Forgetting that
cd-ing via a relative path can leavepwd's logical view referencing a symlink chain that doesn't match the real filesystem structure shown byls -liorreadlink -f. - Not realizing
$OLDPWDand$PWDenvironment variables already hold this information without needing to shell out topwdrepeatedly in scripts.
Tips
- Use
pwd -P(orcd -Pthenpwd) when debugging symlink-related confusion, such as a build tool behaving differently depending on which symlink path was used to enter a directory. - In shell scripts, prefer the
$PWDenvironment variable over calling out topwdwhen you just need the current directory's value without spawning a subshell. - Combine
pwdwithcd "$(dirname "$0")"patterns in scripts to reliably locate the script's own directory before doing relative-path operations.
Best practices
- Always confirm your location with
pwdbefore running destructive commands likerm -rfwith relative paths, especially over SSH sessions where you might be in an unexpected directory. - In automation and CI scripts, capture and restore the working directory explicitly
(
OLDDIR=$(pwd); ...; cd "$OLDDIR") rather than assuming subsequent steps run from the same place. - Use
pwd -Pwhen writing scripts that must resolve real filesystem locations (e.g. for logging, auditing, or deduplication) rather than symlink-based logical paths.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Double-checking your location on a remote SSH session before running
rm -rf ./*to avoid deleting the wrong directory. - Debugging why a build script behaves inconsistently depending on whether it was invoked through a symlinked path versus the real directory.
- Capturing a starting directory in a deployment script so it can safely return there
after
cd-ing elsewhere to perform steps.
Common interview questions
- What's the difference between
pwdandpwd -P?pwd(orpwd -L) shows the logical path as navigated, which may include symlinks;pwd -Presolves all symlinks and shows the real physical path. - Why might
pwdshow a path that doesn't match whatls -lireveals about the directory's real location? Because the shell tracked a logical path through a symlink;pwd -Porreadlink -fwould reveal the true, resolved path. - How would you safely reference the current directory inside a shell script? Use
the
$PWDenvironment variable or capture$(pwd)into a variable, rather than assuming a fixed working directory across invocations.
Frequently Asked Questions
What's the difference between pwd and pwd -P?
Plain pwd (or pwd -L) prints the logical path as you navigated to it, which may include symlink components. pwd -P resolves every symlink in the path and prints the real, physical location on disk.
Why does pwd show a different path than what I typed to cd into a directory?
If you cd'd into a directory through a symlink, the shell's logical pwd remembers the symlink path you used. Running pwd -P (or cd -P then pwd) shows the fully resolved, symlink-free physical path instead.
Is pwd a shell builtin or a separate program?
Both usually exist — most shells (bash, zsh) have a pwd builtin that's faster and tracks the shell's logical directory stack, and there's also a standalone /bin/pwd binary. The builtin is used by default unless you invoke the full path.
Cheat sheet
Download a quick-reference cheat sheet for pwd.