pwd
Overview
pwd (print working directory) writes the absolute path of the current directory. Shells often implement pwd as a builtin that can show a logical path (may include symlink components via $PWD) or a physical path (symlinks resolved).
Use it after deep navigation, in scripts that log context, and whenever logical vs physical paths matter (bind mounts, symlinked project roots).
Syntax
pwd [options]Common Options
| Option | Description |
|---|---|
-L |
Logical path ($PWD; may retain symlink names) — often default for the builtin |
-P |
Physical path (resolve all symlinks) |
--help |
Help (external /bin/pwd) |
help pwd documents the bash builtin; man pwd documents the external binary. Behavior can differ slightly.
Key Use Cases
- Confirm location after many
cds - Capture cwd in scripts and logs
- Compare logical vs physical paths through symlink trees
- Build absolute paths for configs and tooling
- Debug “wrong directory” issues in containers and chroots
Examples with Explanations
Basics
pwd
pwd -P
pwd -L
type pwd # builtin or external?
/bin/pwd -P # force external binaryCapture in scripts
current_dir=$(pwd)
current_dir=$PWD # logical; no subprocess
phys=$(pwd -P)
echo "running in $phys"Prefer "$PWD" when you want the logical path without spawning a process; call pwd -P when you need the real path.
Symlink illustration
mkdir -p /tmp/realdir
ln -sfn /tmp/realdir /tmp/linkdir
cd /tmp/linkdir
pwd -L # often /tmp/linkdir
pwd -P # /tmp/realdir
ls -ld . # may still show link path depending on toolsPrompt and logging recipes
# Show physical path in a custom prompt experiment
PS1='\u@\h $(pwd -P)$ '
# Log absolute cwd with a command
echo "$(date -Is) cwd=$(pwd -P) cmd=$*" >> ~/cmd.logResolve then act
# Ensure absolute before relative ops
cd "$(pwd)/subdir"
# Physical home resolution
cd -P ~
pwd -P
# Fail if cwd vanished (deleted out from under the shell)
pwd -P || echo "cwd invalid"Compare with realpath
pwd -P
realpath .
realpath -e .realpath . is often equivalent to physical cwd; realpath also works on arbitrary paths, not just cwd.
Directory stack awareness
pushd /var/log >/dev/null
pwd
popd >/dev/null
pwd
dirs -vNotes / Pitfalls
- Builtin vs
/bin/pwdcan disagree on default-L/-Psemantics; checktype pwdandhelp pwd. - If the directory was deleted while you were inside it,
pwdmay error or show a stale$PWD. - Don’t mix logical and physical paths blindly in scripts (e.g.
cdby logical path, thenpwd -Pfor logs). cdfollowing symlinks leaves a logical path unless you usecd -Porset -o physical(bash).- In subshells, cwd changes don’t affect the parent:
(cd /tmp && pwd)vs laterpwd.
2026-relevant notes
- Containers:
WORKDIRin the image defines initial cwd;pwdinside the container reflects that namespace’s mounts. - Network filesystems and bind mounts: physical paths help correlate with host mounts (
findmnt,df). - Pair with
realpathfor file paths; reservepwdfor directory context.
Additional Resources
help pwd(bash),man pwd