VICIgeek
Linux `pwd` Command

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.

Linux terminal showing pwd output

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

  1. `pwd` – Print the absolute path of the current working directory.
  2. `pwd [OPTIONS]` – Use options to modify output behavior.
  3. Exit code: `0` on success; non‑zero on error (e.g., inaccessible directory).

Common Options

OptionDescriptionExample
`-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

  1. Basic usage: simply type pwd to see your current directory:
  2. ```bash $ pwd /home/youruser/projects ```
  3. Follow symbolic links (`-L` logical):
  4. ```bash $ cd /tmp/link_to_dir $ pwd -L /tmp/link_to_dir ```
  5. Resolve to actual physical directory (`-P`):
  6. ```bash $ pwd -P /var/www/html/projects ```
  7. Use in a script to store current path:
  8. ```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

  1. Combine `pwd` with variables (e.g., `$PWD`) in scripts for readability.
  2. Prefer `pwd -P` in scripts to avoid unintended symbolic-link behaviors.
  3. Avoid changing directories unnecessarily; always check with `pwd` before file operations.
  4. 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.
Read Linux `pwd` Command

For more Linux command tutorials, refer to the Linux Documentation Project and your distribution’s man pages (man pwd).

Ad Space (Demo)