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
pwdto confirm which directory they operated in. - Confirm symlinks: With
-P, you see the real path rather than the path you arrived by.
pwdcosts 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
2. Resolve Symlinks to the Real Path
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
-Pwhen 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
pwdshows the logical path by default. If you entered a directory through a symlink,pwdprints the symlink path. Use-Pfor the real one.- Whitespace in paths. Always quote the result when storing it:
DIR="$(pwd)", notDIR=$(pwd). pwdis notcd. It only reports location; it never changes it.
Related Commands
| Command | Description |
|---|---|
cd | Change the working directory |
ls | List the contents of a directory |
tree | Draw a directory tree from a starting point |
realpath | Resolve a path to its absolute, canonical form |
dirname | Strip the final component from a path |
pwd Cheatsheet
| Command | Description |
|---|---|
pwd | Print the current directory |
pwd -P | Print the physical (symlink-resolved) path |
pwd -L | Print the logical path (default) |
pwd --help | Show usage information |
DIR=$(pwd) | Capture the path in a variable |
dirname "$0" | Get the directory of the running script |