15 Practical pwd Examples in Linux You Should Know

Vulnerability Manager Plus
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.

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.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.

What pwd Actually Does

pwd stands for print working directory. It shows the absolute path of the directory you’re currently in, starting from the root directory (/).

On most Linux systems, pwd is available in two forms: as a shell builtin in shells such as bash, zsh, and ksh, and as a standalone /bin/pwd command provided by GNU coreutils. These two versions can behave differently when symbolic links are involved.

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.

The examples in this guide were tested on Ubuntu 26.04 LTS and Fedora 42. The behavior described here is consistent across mainstream Linux distributions using GNU coreutils.

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.

Vulnerability Manager Plus

For example, pwd -L -P uses physical mode, while pwd -P -L uses logical mode.

There is also an important difference between the shell builtin and the GNU pwd binary when no option is specified. Bash’s builtin pwd defaults to logical mode (-L), while the GNU coreutils version defaults to physical mode (-P).

This is why the two commands can sometimes show different paths when you are inside a directory reached through a symbolic link.

1. Print Your Current Working Directory

The simplest use of pwd is to print the absolute path of the directory you’re currently in.

pwd
/home/tecmint

2. Create a Symlink and Move Into It

To see how pwd handles symbolic links, create a symlink to a system directory and move into it. We’ll use this directory in the next few examples.

ln -s /var/www/html/ htm
cd htm

3. Print the Logical Path with -L

Once you’re inside the symlinked directory, pwd -L shows the logical path, which preserves the symbolic link you used to reach the directory.

pwd -L
/home/tecmint/htm

4. Print the Physical Path with -P

The -P option shows the physical path instead. It resolves the symbolic link and displays the directory’s actual location on the filesystem.

pwd -P
/var/www/html

5. Check the Default Behavior of /bin/pwd

You can run the standalone pwd binary directly to see how it behaves without any options.

/bin/pwd
/var/www/html

This matches pwd -P, not pwd -L, showing that the GNU coreutils /bin/pwd binary defaults to resolving symbolic links.

Bash’s builtin pwd behaves differently. It tracks the logical path through the $PWD variable and defaults to -L. That’s why pwd and /bin/pwd can sometimes show different paths when you’re working inside a directory reached through a symbolic link.

If this -L vs -P difference just explained a confusing path in one of your symlinked projects, share this with a teammate who’s run into the same issue.

6. Check Your pwd Version

If you’re troubleshooting a script that relies on a particular coreutils behavior or option, it can help to check which version of pwd you’re using.

/bin/pwd --version
pwd (GNU coreutils) 9.11
pwd (GNU coreutils) 9.11
Copyright (C) 2026 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
If you’re working with coreutils commands in scripts and want to understand how they behave in practice, the 100+ Essential Linux Commands course on Protecmint is worth exploring.

Common Mistake: Assuming pwd Accepts a Path

Unlike commands such as cd command or ls command, pwd command doesn’t accept a directory path to operate on. It reports the working directory of the current shell.

For example:

pwd /home

Depending on whether you’re running the shell builtin or the standalone binary, you’ll get an error rather than a change of directory. pwd never changes your current directory.

7. Find Every pwd Available on Your System

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.

type -a pwd
pwd is a shell builtin
pwd is /bin/pwd

This is useful when pwd behaves differently from /bin/pwd, because it lets you confirm exactly which implementation your shell is resolving.

8. Store the Current Directory in a Variable

You can capture the output of pwd in a shell variable, which is useful when writing scripts that need to reuse the current directory.

a=$(pwd)
echo "Current working directory is: $a"
Current working directory is: /home/tecmint

Using command substitution with $(pwd) lets the shell run pwd and assign its output to the variable.

If storing the current path just saved you from hardcoding a directory in a script, share this with someone who still types paths out by hand.

9. Check the Current and Previous Directory

Bash keeps track of both the current directory and the directory you most recently left. These are available through the $PWD and $OLDPWD variables.

echo "$PWD $OLDPWD"
/home /home/tecmint

Here, $PWD contains the current working directory, while $OLDPWD contains the previous one. This is also what makes commands such as cd useful for quickly switching back to the directory you just left.

10. Show the Current Directory in Your Shell Prompt

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.

PS1='\w\$ '

For example:

~/tecmint$

The \w in Bash’s PS1 is a prompt escape that expands to the current working directory, using ~ for your home directory. It is preferable to putting $(pwd) directly in PS1, because command substitution in the assignment is evaluated when the assignment runs rather than automatically each time the prompt is displayed.

If a stale shell prompt has ever made you wonder whether you actually changed directories, pass this along to whoever manages your team’s shell configuration.

Common Mistake: Expanding $(pwd) When Setting PS1

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.

Use Bash’s built-in \w or \W prompt escapes instead which is designed to update automatically whenever the prompt is displayed.

PS1='\w\$ '

If you genuinely need command substitution in a custom prompt, keep the command inside single quotes so Bash can expand it when the prompt is displayed:

PS1='$(pwd)> '

Then change directories and check the prompt using cd /tmp and the prompt should now reflect the new directory.

11. Find the Absolute Path of the pwd Binary

Useful when more than one version of a command exists across your $PATH.

which pwd
/bin/pwd

Keep in mind that which only finds executables in your $PATH. It won’t identify Bash’s builtin pwd, which is why type -a pwd from the earlier example is more useful when you want to see all available implementations.

12. Find the pwd Manual Page

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.

man -w pwd
/usr/share/man/man1/pwd.1.gz

You can then open it normally with:

man pwd

13. Use pwd Inside a Conditional Shell Script

pwd becomes more useful in scripts when you need to check the directory from which a script is running before performing an action. First, create a test directory and script:

mkdir -p ~/tecmint && cd ~/tecmint
nano pwd-check.sh

Add the following:

#!/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

Make the script executable and run it:

chmod +x pwd-check.sh
./pwd-check.sh
You're in the tecmint directory

The script stores the current working directory in x and then compares it with the expected path before continuing.

If comparing $(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

You can combine pwd with dirname to get the parent directory of your current working directory without actually moving there.

cd ~/tecmint/htm 2>/dev/null || cd ~/tecmint
echo "Parent directory: $(dirname "$(pwd)")"
Parent directory: /home/tecmint

Here, pwd supplies the current directory, while dirname removes the final path component to return its parent directory.

15. Compare pwd with realpath

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.

cd ~/tecmint/htm
pwd -P
realpath .
/var/www/html
/var/www/html

Both commands resolve the htm symbolic link and show the actual target directory.

If you’re still getting familiar with Bash scripting, our Bash Scripting course goes deeper into variables, conditionals, command substitution, and practical path handling.
Conclusion

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.

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.

Have you ever run into a script that behaved unexpectedly because of a logical versus physical path, or a shell prompt that showed the wrong directory? Tell us what happened in the comments below.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

There are two ways to help:
Avishek
A Passionate GNU/Linux Enthusiast and Software Developer with over a decade in the field of Linux and Open Source technologies.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

18 Comments

Leave a Reply
  1. I want to print the filenames in my current directory.

    “pwd” doesn’t seem to print anything.

    what do I do to make it actually print the working directory?

    Reply
    • @David,

      Do you mean you want to list filenames? then use ls -l command. The pwd only print the current/present working directory location…I hope you get it now.

      Reply
  2. Great tutorial! Above, you make the following comment:
    “If no option is specified at the prompt, pwd will avoid all symlinks, i.e., take option ‘-P‘ into account.”
    However, in Fedora 20, pwd without options defaults to pwd -L.

    Reply
    • Dear teancum144,
      I would surely like to go through my Fedora 20 Local server to confirm. This is quiet possible. Thanks for your feedback.

      Reply
  3. A few corrections…

    If -L and -P are both used, -L does not have priority; they each override the other (last one wins). Also, for coreutils pwd the default depends on an environment setting. From “info coreutils”:

    “If `-L’ and `-P’ are both given, the last one takes precedence. If
    neither option is given, then this implementation uses `-P’ as the
    default unless the `POSIXLY_CORRECT’ environment variable is set.”

    As far as I know the shell built-in versions of pwd in all the POSIX-type shells (bash, dash, ksh, etc.) follow the POSIX requirement of defaulting to -L.

    The /usr/include/pwd.h header is unrelated – it provides the API for accessing /etc/passwd (or network equivalents).

    Reply
    • Yeah Geoff!

      avi@tecmint:~/htm$ /bin/pwd
      /var/www/html

      avi@tecmint:~/htm$ /bin/pwd -L
      /home/avi/htm

      avi@tecmint:~/htm$ /bin/pwd -P
      /var/www/html

      avi@tecmint:~/htm$ /bin/pwd -P -L
      /home/avi/htm

      avi@tecmint:~/htm$ /bin/pwd -L -P
      /var/www/html

      we need to correct the write-up. Thanks for pointing that out.

      Reply
  4. Never thought your pwd article could contain so much – especially liked the tie in with the “type” command – which I should have known but did not.

    Thanks for a surprisingly good article.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.