Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

pwd Command Guide for Printing Your Current Directory

This guide helps you understand and use the pwd (print working directory) command, which tells you exactly where you are in the filesystem.


What is pwd, and Why Use It?

The pwd command prints the absolute path of the directory you are currently in. It answers the single most disorienting question on a command line: where am I?

Why Learn pwd?

  • Avoid mistakes: Most destructive commands depend on location. Knowing where you are prevents surprises.
  • Copy paths: Paste your current path into cd, cp, or a script instead of typing it by hand.
  • Debug scripts: Scripts often print pwd to confirm which directory they operated in.
  • Confirm symlinks: With -P, you see the real path rather than the path you arrived by.

pwd costs nothing to run and has no side effects. It is the cheapest safety check available.


1. Print the Current Directory

pwd
  • Prints the full path of the working directory.

Example:

pwd
/home/alice/projects/cli-wiki

pwd -P
  • -P (physical) reports the true filesystem path, following any symlinks.
pwd -L
  • -L (logical, the default) reports the path as written, including symlinks.

Example of the difference:

cd /usr/local
pwd -L   # /usr/local
pwd -P   # /opt/homebrew  (if /usr/local is a symlink)

Use -P when a script needs a path that actually exists on disk, for instance when building an archive.


3. Print the Directory of a File

pwd
  • Combined with a subshell, this is a classic idiom for “the folder containing this file”.
cd "$(dirname "$0")" && pwd
  • A common line at the top of shell scripts: it makes the script run from its own directory regardless of where you invoked it from.

4. Store the Path in a Variable

CURRENT_DIR=$(pwd)
echo "$CURRENT_DIR"
  • Captures the output for later use in the same script or session.

5. Use It With ls for Context

echo "Contents of $(pwd):" && ls
  • Prints a header line before listing, which is useful when working across several directories.

Common Pitfalls

  • pwd shows the logical path by default. If you entered a directory through a symlink, pwd prints the symlink path. Use -P for the real one.
  • Whitespace in paths. Always quote the result when storing it: DIR="$(pwd)", not DIR=$(pwd).
  • pwd is not cd. It only reports location; it never changes it.

CommandDescription
cdChange the working directory
lsList the contents of a directory
treeDraw a directory tree from a starting point
realpathResolve a path to its absolute, canonical form
dirnameStrip the final component from a path

pwd Cheatsheet

CommandDescription
pwdPrint the current directory
pwd -PPrint the physical (symlink-resolved) path
pwd -LPrint the logical path (default)
pwd --helpShow usage information
DIR=$(pwd)Capture the path in a variable
dirname "$0"Get the directory of the running script