Home / Alt manpages / pwd(1)

  • pwd(1)
  • User command
  • linux

Find Your Real Working Directory with GNU pwd

GNU pwd sounds like the dullest command on the system, right up until your shell's builtin quietly disagrees with it about where you actually are. By the end you will print the directory a command is running in, choose between a symlink path and its resolved target, and check whether your shell ran its builtin or GNU coreutils pwd. The examples use GNU coreutils 9.4, supplied here by Ubuntu's coreutils package.

  • Prerequisites: a Linux shell and an ordinary user account. No elevated privileges are needed, and nothing in this guide changes system configuration.
  • Allow about five minutes, including the small temporary-directory test.

Before you start

Ask the shell how it resolves pwd:

type -a pwd

In Bash, the usual result starts with pwd is a shell builtin and then lists executable paths such as /usr/bin/pwd. A builtin is part of the shell process. It can have different defaults or options from the external program documented by the pwd(1) manpage.

Run the installed GNU program explicitly when you need the manpage's behaviour:

/usr/bin/pwd --version
/usr/bin/pwd --help

On the machine used for this guide, the version line is pwd (GNU coreutils) 9.4. The help output confirms that GNU pwd supports -L, -P, --help and --version.

Tip

If pwd --version is rejected by your shell, that is a useful sign the builtin ran. Use /usr/bin/pwd --version instead.

1. Print the current directory

Print the current working directory with the command normally used in scripts and interactive work:

/usr/bin/pwd

Expected output is one absolute path, for example:

/home/alex/project

GNU pwd prints the full filename of the current working directory. Its no-option default is physical mode, equivalent to -P: symbolic links in the path are resolved. The command prints the path followed by a newline and normally exits successfully.

Check the exit status if another command depends on the result:

if directory=$(/usr/bin/pwd); then
    printf 'working directory: %s\n' "$directory"
else
    printf 'could not determine the working directory\n' >&2
    exit 1
fi

Command substitution captures the path without its trailing newline. Quoting "$directory" keeps spaces in a directory name together. Do not parse the output by splitting it on spaces.

2. Choose a logical or physical path

A logical path keeps the spelling used to enter a directory, including symlink components. A physical path follows those symlinks to the directory the kernel resolves. Neither mode moves you; they only select the name printed.

  • Request the logical path when the path shown to a user should keep symlinks.
    /usr/bin/pwd -L
    # Long form:
    /usr/bin/pwd --logical
    GNU pwd -L uses the shell's PWD environment value even when it contains symlinks. Useful for prompts, messages and workflows where the user-facing route matters, but not the same as resolving the directory's physical location.
  • Request the physical path when you need the resolved filesystem location.
    /usr/bin/pwd -P
    # Long form:
    /usr/bin/pwd --physical
    This avoids all symlinks. Use it before comparing paths where two names may refer to the same directory, or when a tool's logs must show the underlying location.

Example

Reproduce the difference in a disposable directory under /tmp.

test_root=$(mktemp -d /tmp/pwd-demo.XXXXXX)
mkdir "$test_root/real"
ln -s "$test_root/real" "$test_root/link"
(cd "$test_root/link" && \
    printf 'logical:  '; /usr/bin/pwd -L
    printf 'physical: '; /usr/bin/pwd -P)

Expected output has the same prefix but different final components:

logical:  /tmp/pwd-demo.A1b2C3/link
physical: /tmp/pwd-demo.A1b2C3/real

The random suffix will differ. The test creates only a temporary directory, one directory and one symlink; it does not need root. When finished, remove the exact temporary directory and its contents with a file manager or a carefully scoped command such as rm -r -- "$test_root". Do not replace that variable with a broad path.

3. Handle shell builtin traps

Your shell may supersede the external command. Bash's builtin commonly defaults to logical output, while this installed GNU executable defaults to physical output. Test the actual command in your shell before putting its result in a script:

printf 'shell builtin: '; pwd
printf 'GNU coreutils:  '; /usr/bin/pwd

When the paths differ, choose deliberately. Use pwd -P or /usr/bin/pwd -P for a resolved location, and pwd -L or /usr/bin/pwd -L for a logical one. If a script must be portable across shells, do not assume an unqualified pwd has GNU's exact defaults. Calling /usr/bin/pwd makes this machine's executable explicit, although another system may install it elsewhere.

Do not confuse the printed directory with a permission check. A successful pwd does not grant access to files there. Conversely, an error can occur if the current directory has been removed or a path cannot be resolved. Capture the exit status, report the error, and stop a dependent operation rather than substituting a guessed directory.

Done means

  • Builtin identified: you can tell whether pwd is a builtin or an external command with type -a pwd.
  • Version confirmed: you can print the installed GNU implementation's version with /usr/bin/pwd --version.
  • Default known: you know this GNU coreutils 9.4 executable defaults to physical mode.
  • Mode chosen deliberately: you can select a symlink-preserving path with -L or a resolved path with -P.
  • Scripts made safe: they quote captured paths and check the command's exit status.