pwd looks like one of the simplest Linux commands, but there are a few details worth knowing once symlinks and shell scripts enter the picture. In this guide, we’ll go through practical pwd examples, along with our cd command guide for related directory navigation.One detail that can easily cause confusion is what happens when you use both -L and -P. If both options are specified, the last option takes effect.
What pwd Actually Does
This is why the two commands can sometimes show different paths when you are inside a directory reached through a symbolic link.
The location of a command’s manual page can vary between distributions. Instead of assuming a fixed path, let man tell you where the page is installed.
pwd -L
man pwd
13. Use pwd Inside a Conditional Shell Script
Useful when more than one version of a command exists across your $PATH.
PS1=’w$ ‘
The realpath command resolves the current path to its canonical physical location. Comparing it with pwd -P is a quick way to verify that both commands resolve the same physical path.
1. Print Your Current Working Directory
type -a pwd
echo “$PWD $OLDPWD”
PS1=’w$ ‘
pwd stands for print working directory. It shows the absolute path of the directory you’re currently in, starting from the root directory (/).
The easiest way to make this behavior clear is to try it yourself. Find a symlinked directory on your Linux system, cd into it, and run pwd -L followed by pwd -P. Seeing the logical and physical paths side by side makes the difference much easier to understand.
cd ~/tecmint/htm 2>/dev/null || cd ~/tecmint
echo “Parent directory: $(dirname “$(pwd)”)”
~/tecmint$
/bin/pwd
pwd [OPTION]
| Option | Description |
|---|---|
-L |
Shows the logical path, using $PWD even when the path contains symbolic links. |
-P |
Shows the physical path by resolving symbolic links to their actual target. |
--help |
Displays help information and exits. |
--version |
Displays version information and exits. |
/bin/pwd –version
ln -s /var/www/html/ htm
cd htm
3. Print the Logical Path with -L
chmod +x pwd-check.sh
./pwd-check.sh
which pwd
You can combine pwd with dirname to get the parent directory of your current working directory without actually moving there.
man -w pwd
By now, you should have a clear idea of how pwd command works beyond simply printing your current directory. You’ve seen how -L and -P handle symbolic links differently, why Bash’s builtin pwd and the /bin/pwd binary can return different paths, and how to use pwd in scripts, shell prompts, and path comparisons.
mkdir -p ~/tecmint && cd ~/tecmint
nano pwd-check.sh
You can configure Bash’s prompt to show the current directory automatically, so you don’t need to run pwd every time you want to check where you are.
pwd /home
#!/bin/bash
x=”$(pwd)”
if [ “$x” == “/home/$USER/tecmint” ]; then
echo “You’re in the tecmint directory”
else
echo “Not in tecmint, currently in: $x”
fi
For example:
Your shell may have both a builtin pwd and a standalone coreutils binary. The type command shows which one is being used and where other versions can be found.
You’ve probably typed pwd countless times without giving it much thought. But when you’re working inside a symlinked directory, writing a shell script that needs the current path, or troubleshooting why a command is running from an unexpected location, understanding how pwd reports the working directory becomes useful.
$(pwd) inside a script just replaced a fragile hardcoded directory check, pass this along to a teammate writing similar setup scripts.14. Get the Parent Directory Without Changing Directories
cd ~/tecmint/htm
pwd -P
realpath .
The simplest use of pwd is to print the absolute path of the directory you’re currently in.
15. Compare pwd with realpath
Use Bash’s built-in w or W prompt escapes instead which is designed to update automatically whenever the prompt is displayed.
pwd -P
For example, pwd -L -P uses physical mode, while pwd -P -L uses logical mode.
PS1=’$(pwd)> ‘
It’s easy to assume that PS1="$(pwd)> ” will keep showing your current directory. However, the $(pwd) command is evaluated when you assign the variable, so the prompt can remain stuck at that directory.