
Linux `pwd` Command
pwd (print working directory) displays the full path of the current directory in the file system. It’s one of the most fundamental commands for shell navigation and scripting.

Prerequisites
- Access to any Linux distribution shell (bash, zsh, sh, etc.)
- Basic familiarity with file system hierarchy (/home, /etc, /var, etc.)
- Terminal or SSH access to a Linux host
Command Overview
- `pwd` – Print the absolute path of the current working directory.
- `pwd [OPTIONS]` – Use options to modify output behavior.
- Exit code: `0` on success; non‑zero on error (e.g., inaccessible directory).
Common Options
Option | Description | Example | |
---|---|---|---|
`-L` | `--logical` | Display the logical path (default) | `pwd -L` |
`-P` | `--physical` | Resolve symbolic links to physical path | `pwd -P` |
`--help` | Show help and exit | `pwd --help` | |
`--version` | Show version info | `pwd --version` |
By default, most shells use the logical (-L
) path, preserving symbolic links in $PWD
.
Usage Examples
- Basic usage: simply type
pwd
to see your current directory: ```bash
$ pwd
/home/youruser/projects
``` - Follow symbolic links (`-L` logical): ```bash $ cd /tmp/link_to_dir $ pwd -L /tmp/link_to_dir ```
- Resolve to actual physical directory (`-P`): ```bash $ pwd -P /var/www/html/projects ```
- Use in a script to store current path: ```bash #!/usr/bin/env bash CURRENT_DIR=$(pwd) echo "Script is running from $CURRENT_DIR" ```
Use Cases & Integration
- Scripting: capture directory paths for backups, logs, or configuration files.
- Automation: use in build scripts to reference project root regardless of invocation location.
- Debugging: verify the working directory when troubleshooting path-related errors.
- Documentation: include `pwd` output in tutorials to orient users to directory structures.
Best Practices
- Combine `pwd` with variables (e.g., `$PWD`) in scripts for readability.
- Prefer `pwd -P` in scripts to avoid unintended symbolic-link behaviors.
- Avoid changing directories unnecessarily; always check with `pwd` before file operations.
- Use absolute paths (`$(pwd)/subdir`) to avoid context errors in cron jobs.
Next Steps
- Learn other navigation commands: `cd`, `ls`, `pushd`, and `popd`.
- Explore environment variables: `HOME`, `OLDPWD`, and `PWD`.
- Build complex scripts that dynamically detect project directories.
- Combine with `basename` and `dirname` for path component extraction.
For more Linux command tutorials, refer to the Linux Documentation Project and your distribution’s man pages (man pwd
).
Ad Space (Demo)