keepcalculator

06 Numbers and Powers

pwd Command Examples: 10 Practical Uses

Card No. 06·48591 · Updated · Standard formula, shown below

The pwd command prints the full path of the current working directory, and it is one of the most fundamental commands in Unix-like systems (Linux, macOS, BSD). You use it to confirm where you are in the filesystem, and it returns an absolute path starting from the root (/). This article shows 10 practical examples, explains the two built-in options, and clears up the common confusion between the shell’s logical and physical directory tracking.

What pwd Actually Does

When you run pwd, the shell asks the operating system for the current directory’s inode and then walks the directory tree to construct the full path. The name stands for “print working directory” — it does not change anything, it only reports. The output is always an absolute path, meaning it starts with / (or a drive letter on some systems, but pwd is native to Unix).

Every time you cd into a folder, run a script, or open a terminal, the shell maintains a variable called PWD that holds the current path. The pwd command reads that variable (by default) and prints it. That is why it is instant and never fails — unless the directory has been deleted while you are inside it, in which case the physical lookup may fail.

10 Practical pwd Examples

  1. Basic usage — Type pwd and press Enter. You get something like /home/username/projects. This is the most common use: confirming your location before running commands that depend on paths.
  2. Use in scripts — In a shell script, pwd is often captured into a variable: current_dir=$(pwd). This lets the script know where it was launched from, which is useful for referencing relative files.
  3. With echo — echo "$(pwd)/file.txt" prints the full path to a file in the current directory. This is a quick way to copy a full path for use in other commands.
  4. Logical vs physical path — Run pwd -L to see the logical path (what the shell thinks, using symlinks) and pwd -P to see the physical path (resolving all symlinks to the real directories). The default is -L on most shells.
  5. After cd with symlinks — If you cd into a symlink like /tmp/link that points to /home/user/real, pwd -L shows /tmp/link, while pwd -P shows /home/user/real. This is critical when you need the real path for tools that do not follow symlinks.
  6. In command substitution — cp somefile.txt "$(pwd)/backup/" copies a file to a backup folder using the absolute path. This avoids issues if the current directory changes later.
  7. With find or grep — find "$(pwd)" -name "*.log" searches from the current directory but with an absolute path, which can be safer in scripts that change directories.
  8. In prompts — Some users put pwd in their shell prompt (though most use \w). This shows the full path at every command line, which is helpful when working deep in a tree.
  9. Verify after cd — After a cd that might fail (e.g., a typo), running pwd confirms whether you actually moved. If the directory does not exist, the shell prints an error and stays put; pwd shows you did not move.
  10. In a subshell — (cd /tmp && pwd) prints /tmp but does not change your current shell’s directory. This is a neat trick for running a command in another directory without affecting the parent shell.

pwd -L vs pwd -P: The Symlink Question

The two options are the source of most pwd confusion. The default behavior (which is -L in bash, zsh, and most shells) uses the PWD environment variable, which the shell updates when you use cd. If you cd into a symlink, the shell remembers the symlink path, not the target path. So pwd -L prints the symlink path.

In contrast, pwd -P ignores the variable and asks the kernel for the actual directory, resolving all symlinks. This is the “physical” path. For example, on macOS, /tmp is a symlink to /private/tmp. Running cd /tmp && pwd -P gives /private/tmp, while pwd -L gives /tmp.

Which should you use? For most interactive work, -L is fine because you want to see the path you typed. For scripts that need to reference files relative to the real location, -P is safer because symlinks can break relative paths. Many system administrators use pwd -P in cron jobs or deployment scripts to avoid surprises.

Common Problems and Misconceptions

  • “pwd is not found” — This happens if your PATH variable is broken or you are in a minimal environment (like a rescue shell). The command is usually at /bin/pwd or /usr/bin/pwd. You can run /bin/pwd directly.
  • “pwd shows a different directory than my prompt” — Your prompt might be customized to show a short name or a different variable. Trust pwd; it is the ground truth.
  • “pwd says directory does not exist” — If you delete the directory you are currently in, pwd -P will fail with an error like pwd: getcwd: No such file or directory. The -L version still prints the old path. To recover, cd to a known directory.
  • “pwd in a script gives the script’s location” — No, pwd always gives the current working directory of the process, not the script’s location. To get the script’s own directory, you need dirname "$0" (but beware of symlinks).
  • “pwd is a shell builtin, so it is the same everywhere” — It is a builtin in bash, zsh, and others, but there is also a standalone binary at /bin/pwd. The builtin is usually used, but the binary behaves slightly differently (it always uses physical path by default). You can force the binary with /bin/pwd.

pwd vs. Other Path Commands

The pwd command is often confused with dirname and basename. pwd gives you the current directory’s full path. dirname takes a file path and returns the directory part (e.g., dirname /a/b/c.txt gives /a/b). basename gives the last component (e.g., basename /a/b/c.txt gives c.txt).

In shell scripts, you often combine them: script_dir=$(cd "$(dirname "$0")" && pwd). This changes to the script’s directory and then prints the absolute path, which handles both relative paths and symlinks. This is a common idiom because $0 can be a relative path.

Another related tool is realpath, which resolves symlinks and prints the canonical path. realpath . is equivalent to pwd -P on most systems. However, realpath is not available on all systems by default (it is on Linux and macOS), while pwd is universal.

pwd in Different Shells

While the core behavior is the same, there are small differences:

  • bash — Default is -L. You can set set -P to make cd resolve symlinks, so pwd will always show physical paths.
  • zsh — Same default (-L), but you can set chpwd_functions to customize. The PWD variable is also used.
  • fish — pwd works the same, but there is no -L or -P option; it always prints the logical path. Use pwd -P is not available, but you can use realpath.
  • Windows (PowerShell) — The equivalent is Get-Location or $PWD. The command pwd is an alias for Get-Location in PowerShell. It prints the current directory path, but with a drive letter (e.g., C:\Users\name).

In all cases, the command is used for the same purpose: to know where you are. The differences only matter when symlinks are involved or when you are writing portable scripts.

Frequently Asked Questions

What does pwd stand for?

It stands for “print working directory”. It is a command that outputs the full path of the directory you are currently in. The name is historical: “print” means to output to the screen, not to send to a printer.

Why does pwd show a different path than my prompt?

Your shell prompt is customizable and often shows only the last part of the path (like ~ for home or just the folder name). pwd always shows the full absolute path. If they differ significantly, your prompt might be using a custom variable, but pwd is the reliable source.

How do I get the physical path instead of the symlink path?

Use pwd -P. This resolves all symlinks and shows the real directory. For example, on macOS, cd /tmp && pwd -P shows /private/tmp. If your shell does not support -P (like fish), use realpath . or readlink -f . on Linux.

Is pwd a builtin or an external command?

Both. Most shells (bash, zsh, ksh) have a builtin version that is used by default. There is also an external binary at /bin/pwd. The builtin is faster and supports the -L and -P options. You can force the external one by typing /bin/pwd.

Can I use pwd in a script to get the script’s directory?

No, pwd gives the directory from which the script was invoked, not where the script file is located. To get the script’s directory, use script_dir="$(cd "$(dirname "$0")" && pwd)". This changes to the script’s directory and then prints the absolute path, handling relative paths and symlinks.

What does “pwd: getcwd: No such file or directory” mean?

It means the current directory has been deleted while you are inside it. The shell’s PWD variable still holds the old path, but the physical directory no longer exists. To fix it, cd to a valid directory like cd / or cd ~.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →