pwd Command Explained

pwd Command Explained

pwd is one of the shortest, simplest commands in Linux, and one of the first anyone learns. It answers a single question: where am I in the filesystem right now?

Basic usage

pwd
# /home/colton/projects/linuxdork

Running pwd with no arguments prints the full absolute path of your current working directory, the directory your shell session is “in” at that moment. Every relative path you type, cd .., ls subfolder, cat notes.txt, is interpreted relative to this location.

Why it matters beyond the obvious

It seems trivial until you are several cd commands deep into a nested project structure, working across multiple terminal tabs, or running a script that changes directories internally. In all of these situations, assuming you know where you are without checking is a common source of mistakes, particularly with destructive commands.

cd ~/projects/linuxdork/src/content/blog
# ... several more cd commands later, in a different tab ...
pwd
# confirms exactly where you are before running rm -rf * or similar

Confirming your location with pwd before running any command that operates on the current directory broadly (like a wildcard delete or a recursive chmod) is a cheap habit that prevents expensive mistakes.

pwd vs pwd -P: logical vs physical paths

# A symlink pointing at a real directory elsewhere
ln -s /var/www/html ~/website

cd ~/website
pwd
# /home/colton/website        <- the logical path, as you navigated it

pwd -P
# /var/www/html                <- the physical path, symlink resolved

Plain pwd (and pwd -L, which is the explicit default) shows the path exactly as you traveled through it, including any symbolic links. pwd -P resolves every symlink in the path to show the canonical, real location on disk. This distinction rarely matters for everyday use, but becomes important when scripts need the true underlying path, for example, to correctly resolve further relative paths from that location, or when comparing two paths for equality where one might be a symlink to the other.

pwd vs the $PWD environment variable

echo $PWD
pwd
# both typically print the same value

The shell maintains $PWD as an environment variable, updating it automatically every time you cd. The pwd command queries the current directory directly rather than relying on that variable. In ordinary interactive use, the two are indistinguishable, but pwd is considered more dependable in edge cases, such as certain subshells, restricted environments, or situations where $PWD was unset or not exported correctly by whatever spawned the shell.

Using pwd in scripts

A very common script pattern captures the invocation directory so other paths can be built relative to it:

#!/bin/bash
SCRIPT_DIR=$(pwd)
echo "Running from: $SCRIPT_DIR"
cp "$SCRIPT_DIR/config.template" "$SCRIPT_DIR/config.local"

A related and often more robust pattern captures the directory the script file itself lives in, regardless of the directory the user happened to be in when they ran it:

#!/bin/bash
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
echo "Script lives in: $SCRIPT_DIR"

This second pattern is preferred in reusable scripts because it does not depend on the caller’s current directory at all; it resolves the script’s own location no matter where it is invoked from.

Built-in vs standalone binary

# Check which pwd will actually run
type pwd
# pwd is a shell builtin

# Force the standalone coreutils binary instead
/bin/pwd

Most shells, including bash and zsh, implement pwd as a built-in for speed, since built-ins avoid the overhead of spawning a separate process. A standalone binary, generally at /bin/pwd (which on modern systems is a symlink into /usr/bin/pwd), also exists as part of the GNU coreutils package, for use in minimal shells or contexts where the builtin is unavailable or behaves differently.

Frequently Asked Questions

What does the pwd command do?

pwd stands for print working directory. It outputs the full, absolute path of the directory your shell session is currently located in. It takes no arguments in typical use and simply prints one line of text: the current path, starting from the root directory /.

Why would I need pwd if I can see the path in my terminal prompt?

Many shell prompts are configured to show only the current directory name, not the full path, or truncate long paths for readability. pwd gives you the complete, unambiguous absolute path every time, which matters when writing scripts, confirming exactly where a command will operate, or when your prompt has been customized to hide path details.

What is the difference between pwd and pwd -P?

Plain pwd prints the logical path, meaning it shows the path you navigated through, including any symbolic links you passed through, exactly as typed. pwd -P prints the physical path, resolving every symbolic link along the way to show the real, canonical location on disk. If you cd into a symlinked directory, pwd shows the symlink path while pwd -P shows where that symlink actually points.

How is pwd different from the $PWD environment variable?

They usually show the same value, but they are different mechanisms. $PWD is a shell environment variable that the shell itself updates every time you cd. The pwd command is a program (or shell builtin) that queries the current directory directly. In almost all everyday cases echo $PWD and pwd print identical output, but pwd is more reliable in edge cases like subshells or scripts where $PWD might not have been set or exported correctly.

Does pwd work the same way in scripts as it does interactively?

Yes, pwd behaves identically whether typed directly into an interactive shell or used inside a script. It is commonly used in scripts to capture the directory a script was launched from, often stored in a variable like SCRIPT_DIR=$(pwd), so the script can reference other files relative to that location regardless of where it is run from.

Is pwd a built-in shell command or a separate program?

Both exist. Most shells, including bash and zsh, have pwd built directly into the shell for speed, and this built-in version is what runs by default. A standalone /bin/pwd binary also exists as part of coreutils for use in scripts or shells without a built-in version. You can force the standalone binary to run instead of the shell built-in with the full path: /bin/pwd.