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
- Basic usage — Type
pwdand 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. - Use in scripts — In a shell script,
pwdis 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. - 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. - Logical vs physical path — Run
pwd -Lto see the logical path (what the shell thinks, using symlinks) andpwd -Pto see the physical path (resolving all symlinks to the real directories). The default is-Lon most shells. - After cd with symlinks — If you
cdinto a symlink like/tmp/linkthat points to/home/user/real,pwd -Lshows/tmp/link, whilepwd -Pshows/home/user/real. This is critical when you need the real path for tools that do not follow symlinks. - 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. - 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. - In prompts — Some users put
pwdin 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. - Verify after cd — After a
cdthat might fail (e.g., a typo), runningpwdconfirms whether you actually moved. If the directory does not exist, the shell prints an error and stays put;pwdshows you did not move. - In a subshell —
(cd /tmp && pwd)prints/tmpbut 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
PATHvariable is broken or you are in a minimal environment (like a rescue shell). The command is usually at/bin/pwdor/usr/bin/pwd. You can run/bin/pwddirectly. - “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 -Pwill fail with an error likepwd: getcwd: No such file or directory. The-Lversion still prints the old path. To recover,cdto a known directory. - “pwd in a script gives the script’s location” — No,
pwdalways gives the current working directory of the process, not the script’s location. To get the script’s own directory, you needdirname "$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 setset -Pto makecdresolve symlinks, sopwdwill always show physical paths. - zsh — Same default (
-L), but you can setchpwd_functionsto customize. ThePWDvariable is also used. - fish —
pwdworks the same, but there is no-Lor-Poption; it always prints the logical path. Usepwd -Pis not available, but you can userealpath. - Windows (PowerShell) — The equivalent is
Get-Locationor$PWD. The commandpwdis an alias forGet-Locationin 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
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It