pwd — Know Where You Are in the Filesystem
The pwd command prints your current location in the Linux filesystem. Small but essential — learn its options and how $PWD powers scripts.
pwd stands for Print Working Directory. It answers one simple question: "Where am I right now?" — and prints the full absolute path of your current location.
It sounds trivial, but once you're navigating deep into filesystems or writing scripts, knowing your exact location is critical.
Basic Usage
pwd
# /home/sagar
cd /etc
pwd
# /etc
cd ~
pwd
# /home/sagar
cd /var/log/nginx
pwd
# /var/log/nginxOptions: Logical vs Physical Paths
pwd has two options that matter when symbolic links (symlinks) are involved. A symlink is a shortcut that points to another location — like a Windows shortcut but at the OS level.
By default (-L), pwd shows where you think you are (through the symlink). With -P, it shows where you actually are on disk.
pwd Options
| Option | Name | Behaviour |
|---|---|---|
-L | Logical (default) | Shows path including symlinks as-is |
-P | Physical | Resolves symlinks — shows the real path on disk |
# Scenario: /home/sagar/mylink → /var/www/html (symlink)
cd /home/sagar/mylink
pwd # /home/sagar/mylink ← logical (default)
pwd -L # /home/sagar/mylink ← same as default
pwd -P # /var/www/html ← real path, symlink resolvedThe $PWD Variable
The shell automatically maintains a variable called $PWD that always equals your current directory — the same value that pwd prints. You can use it directly in scripts and commands without spawning a subshell.
echo $PWD # Exactly same as pwd, but no subshell
# Save location and restore later (common in scripts)
SAVED_DIR="$PWD"
cd /tmp
# do stuff...
cd "$SAVED_DIR" # Go back exactly where we started
# Use current path in a command
echo "Files in $(pwd):"
ls "$PWD"$PWD vs $(pwd)
$PWD is a shell variable — reading it is instant, no subprocess needed. $(pwd) runs an external command in a subshell and captures the output. Both give the same result, but $PWD is slightly faster and preferred in scripts.
You run `pwd` and get `/home/sagar/link` but you suspect it's a symlink. Which command shows the actual physical path?
- Run
pwdfrom three different directories (try/tmp,/etc,~) and note the output - Use
$PWDto print a message: "I am currently in: /your/path" - In a script context: save your current location to a variable,
cd /tmp, dols, then return to where you were