Sagar.BlogArticle
All posts
All posts
Linux

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.

January 6, 20254 min read
LinuxNavigationFilesystem

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-basics.sh
pwd
# /home/sagar

cd /etc
pwd
# /etc

cd ~
pwd
# /home/sagar

cd /var/log/nginx
pwd
# /var/log/nginx

Options: 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

OptionNameBehaviour
-LLogical (default)Shows path including symlinks as-is
-PPhysicalResolves 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 resolved

The $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.


Quick Check

You run `pwd` and get `/home/sagar/link` but you suspect it's a symlink. Which command shows the actual physical path?

Exercise
  1. Run pwd from three different directories (try /tmp, /etc, ~) and note the output
  2. Use $PWD to print a message: "I am currently in: /your/path"
  3. In a script context: save your current location to a variable, cd /tmp, do ls, then return to where you were