Close Menu
IOupdate | IT News and SelfhostingIOupdate | IT News and Selfhosting
  • Home
  • News
  • Blog
  • Selfhosting
  • AI
  • Linux
  • Cyber Security
  • Gadgets
  • Gaming

Subscribe to Updates

Get the latest creative news from ioupdate about Tech trends, Gaming and Gadgets.

What's Hot

Linux Kernel is Getting Ready to Purge Old 32-bit Arm Platform Code

September 19, 2026

Cybersecurity 2026: The Year AI Became The Battlefield And What Comes Next

September 19, 2026

AI Skills for Different Roles: What You Need to Learn in 2026

September 19, 2026
Facebook X (Twitter) Instagram
Facebook Mastodon Bluesky Reddit
IOupdate | IT News and SelfhostingIOupdate | IT News and Selfhosting
  • Home
  • News
  • Blog
  • Selfhosting
  • AI
  • Linux
  • Cyber Security
  • Gadgets
  • Gaming
IOupdate | IT News and SelfhostingIOupdate | IT News and Selfhosting
Home»Linux»pwd Command in Linux: 15 Practical Uses You Should Know
Linux

pwd Command in Linux: 15 Practical Uses You Should Know

MarkBy MarkSeptember 19, 2026No Comments11 Mins Read
pwd Command in Linux: 15 Practical Uses You Should Know


Introduction

The pwd command, short for "print working directory," might seem deceptively simple, yet mastering its nuances is crucial for efficient Linux directory navigation and robust shell scripting. Beyond merely displaying your current location, pwd interacts uniquely with symbolic links, offering both logical and physical path interpretations. This comprehensive guide will demystify pwd, exploring its options, behavioral differences between shell builtins and standalone binaries, and practical applications that will significantly enhance your command-line prowess. Dive in to uncover how understanding pwd can prevent common scripting pitfalls and elevate your Linux expertise.


Understanding the `pwd` Command: Beyond the Basics

You’ve likely typed pwd countless times without a second thought. But when you’re working inside a symlinked directory, crafting a shell script that demands the correct path, or troubleshooting why a command executes from an unexpected location, a deeper understanding of how pwd reports the working directory becomes invaluable.

What pwd Actually Does

pwd stands for "print working directory." It’s fundamental to Linux filesystem management, showing 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 common shells like Bash, Zsh, and Ksh, and as a standalone /bin/pwd command provided by GNU coreutils. These two versions can exhibit different behaviors, particularly when symbolic links are involved.

pwd [OPTION]

OptionDescription
-LShows the logical path, using $PWD even when the path contains symbolic links.
-PShows the physical path by resolving symbolic links to their actual target.
–helpDisplays help information and exits.
–versionDisplays version information and exits.

The examples in this guide were tested on Ubuntu 26.04 LTS and Fedora 42, ensuring consistency across mainstream Linux distributions utilizing GNU coreutils. A subtle detail that often causes confusion is the interaction of both -L and -P. If both options are specified, the last one takes precedence. For example, pwd -L -P employs physical mode, whereas pwd -P -L utilizes logical mode.

There’s also a critical distinction 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 precisely why the two commands can sometimes yield different paths when you are navigating through a directory via a symbolic link.

Practical `pwd` Command Examples for Linux Users

Let’s dive into practical scenarios to solidify your understanding of pwd and its power in Linux directory navigation.

1. Print Your Current Working Directory

The most straightforward use of pwd is to simply display the absolute path of your current location.

pwd
/home/tecmint

2. Create a Symlink and Navigate Into It

To observe pwd‘s behavior with symbolic links, create a symlink to a system directory and then cd into it. We’ll use this setup for subsequent examples.

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

3. Print the Logical Path with -L

Once inside the symlinked directory, pwd -L will display the logical path, preserving the symbolic link you followed.

pwd -L

/home/tecmint/htm

4. Print the Physical Path with -P

Conversely, the -P option reveals the physical path. It resolves the symbolic link and shows the directory’s actual location on the filesystem.

pwd -P

/var/www/html

5. Check the Default Behavior of /bin/pwd

You can directly execute the standalone pwd binary to see its default behavior without any options.

/bin/pwd

/var/www/html

This output matches pwd -P, confirming that the GNU coreutils /bin/pwd binary defaults to resolving symbolic links. Bash’s builtin pwd, however, behaves differently, tracking the logical path via the $PWD variable and defaulting to -L. This divergence is a common source of confusion when navigating symlinked structures in Linux.

6. Check Your `pwd` Version

When troubleshooting a script that relies on specific coreutils behavior or options, knowing your pwd version can be incredibly helpful.

/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.

Common Mistake: Assuming `pwd` Accepts a Path

Unlike commands such as cd or ls, the pwd command does not accept a directory path as an argument. It exclusively reports the working directory of the current shell.

For example:

pwd /home

Depending on whether you’re using the shell builtin or the standalone binary, you’ll encounter an error rather than a directory change. pwd never alters your current directory.

7. Find Every `pwd` Available on Your System

Your shell might have both a builtin pwd and a standalone coreutils binary. The type -a command will reveal which one is in use and where other versions are located.

type -a pwd

pwd is a shell builtin
pwd is /bin/pwd

This is particularly useful when pwd behaves differently from /bin/pwd, allowing you to confirm precisely which implementation your shell is resolving.

8. Store the Current Directory in a Variable

Capturing the output of pwd in a shell variable is an indispensable technique for Bash scripting and shell scripting, especially when you need to reuse the current directory path.

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

Current working directory is: /home/tecmint

Using command substitution with $(pwd) enables the shell to execute pwd and assign its output to the specified variable.

9. Check the Current and Previous Directory

Bash intelligently keeps track of both your current directory and the one you most recently exited. These are accessible via the $PWD and $OLDPWD variables, offering handy Linux directory navigation shortcuts.

echo "$PWD $OLDPWD"

/home /home/tecmint

Here, $PWD holds the current working directory, while $OLDPWD stores the previous one. This feature also empowers commands like cd - for rapidly switching back to your last directory.

10. Show the Current Directory in Your Shell Prompt

Configure your Bash prompt (PS1) to automatically display the current directory. This eliminates the need to run pwd repeatedly to ascertain your location, greatly improving your command-line efficiency.

PS1='\w\$ '

For example:

~/tecmint$

The \w escape sequence in Bash’s PS1 expands to the current working directory, using ~ for your home directory. This is generally preferred over embedding $(pwd) directly in PS1, as command substitution during assignment is evaluated only once, not dynamically with each prompt display.

Common Mistake: Expanding `$(pwd)` When Setting PS1

It’s a common misconception that PS1="$(pwd)> " will continuously update your prompt with the current directory. However, the $(pwd) command is evaluated only when the variable is assigned, leading to a prompt that remains static at the directory it was set in.

Instead, rely on Bash’s built-in \w or \W prompt escapes, which are designed to update automatically whenever the prompt is displayed:

PS1='\w\$ '

If you genuinely require command substitution in a custom prompt that updates dynamically, ensure the command is enclosed within single quotes. This tells Bash to expand it each time the prompt is displayed:

PS1='$(pwd)> '

Then, change directories (e.g., cd /tmp), and your prompt should now accurately reflect the new directory.

11. Find the Absolute Path of the `pwd` Binary

This is useful for Linux system administration when multiple versions of a command might exist across your $PATH.

which pwd

/bin/pwd

Remember that which only locates executables within your $PATH. It won’t identify Bash’s builtin pwd, which is why type -a pwd (as seen earlier) is more comprehensive for seeing all available implementations.

12. Find the `pwd` Manual Page

The location of a command’s manual page can vary between distributions. Instead of guessing 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 particularly powerful in shell scripting when you need to check the directory from which a script is executing before performing specific actions.

First, create a test directory and script:

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

Add the following content:

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

This script stores the current working directory in variable x and then compares it against an expected path before proceeding. This is a robust way to add conditional logic based on location in your Bash scripting.

14. Get the Parent Directory Without Changing Directories

You can elegantly combine pwd with dirname to obtain the parent directory of your current working directory without actually navigating there, a useful trick for dynamic path manipulation in Linux.

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 strips the final path component to return its parent directory.

15. Compare `pwd` with `realpath`

The realpath command resolves a given path to its canonical, physical location. Comparing its output with pwd -P is a quick way to verify that both commands resolve to the same actual physical path on the Linux filesystem.

cd ~/tecmint/htm
pwd -P
realpath .

/var/www/html
/var/www/html

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

Conclusion

By now, you should have a clear understanding of how the pwd command functions far beyond simply printing your current directory. You’ve explored how -L and -P options handle symbolic links differently, grasped why Bash’s builtin pwd and the /bin/pwd binary can return disparate paths, and learned to leverage pwd effectively in shell scripts, custom prompts, and advanced path comparisons.

The most effective way to internalize these behaviors is through hands-on practice. Locate a symlinked directory on your Linux system, cd into it, and then execute pwd -L followed by pwd -P. Seeing the logical and physical paths side by side will illuminate the distinctions with clarity.

Have you ever encountered unexpected script behavior due to a logical versus physical path discrepancy, or a shell prompt showing the wrong directory? Share your experiences in the comments below! If this article proved helpful, consider sharing it with a colleague on your team.


FAQ

Question 1: What is the primary difference between pwd -L and pwd -P?
Answer 1: The core difference lies in how they handle symbolic links. pwd -L (logical mode) shows the path including any symbolic links you’ve traversed, essentially displaying the path you’ve logically followed. pwd -P (physical mode) resolves all symbolic links in the path, showing the absolute, physical location of the directory on the Linux filesystem, regardless of the symlinks used to reach it.

Question 2: Why might pwd and /bin/pwd produce different outputs?
Answer 2: This often occurs when you are in a directory accessed via a symbolic link. Bash’s builtin pwd defaults to logical mode (-L), tracking the path through the $PWD variable. In contrast, the standalone /bin/pwd (from GNU coreutils) defaults to physical mode (-P), resolving symbolic links to their actual target. This means pwd might show ~/link_to_dir while /bin/pwd shows /actual/path/to/dir.

Question 3: How can pwd be effectively used in Bash scripting for more than just printing a path?
Answer 3: pwd is invaluable in Bash scripting for robust Linux directory navigation and conditional logic. You can capture its output in variables to construct dynamic paths, perform checks to ensure a script runs from an expected location (e.g., if [ "$(pwd)" == "/expected/path" ]; then ... fi), or use it with other commands like dirname to extract parent directories. A recent example of its importance is in CI/CD pipelines, where build scripts often need to verify their exact working directory (physical vs. logical) to correctly locate dependencies or output artifacts, especially in containerized environments where path mappings can be complex.

Unique Tip: For advanced Linux directory navigation, explore pushd and popd commands. Instead of just knowing your current directory (pwd), pushd allows you to save your current directory to a stack and then navigate to a new one, while popd lets you quickly return to the last directory pushed onto the stack. This provides a powerful, stack-based way to manage multiple working directories without constantly typing out full paths.



Read the original article

0 Like this
command Linux Practical pwd
Share. Facebook LinkedIn Email Bluesky Reddit WhatsApp Threads Copy Link Twitter
Previous ArticleOpenAI agents discussed ways to escape their sandbox on public wiki
Next Article Demystifying the iPhone 18 Pro’s Variable Aperture

Related Posts

Linux

Linux Kernel is Getting Ready to Purge Old 32-bit Arm Platform Code

September 19, 2026
Linux

Tether Brings the Apple Continuity Experience to Linux

September 4, 2026
Linux

Linux head, tail, and cat Commands for Viewing Files and Logs

August 30, 2026
Add A Comment
Leave A Reply Cancel Reply

Top Posts

AI Developers Look Beyond Chain-of-Thought Prompting

May 9, 202515 Views

6 Reasons Not to Use US Internet Services Under Trump Anymore – An EU Perspective

April 21, 202512 Views

Andy’s Tech

April 19, 20259 Views
Stay In Touch
  • Facebook
  • Mastodon
  • Bluesky
  • Reddit

Subscribe to Updates

Get the latest creative news from ioupdate about Tech trends, Gaming and Gadgets.

About Us

Welcome to IOupdate — your trusted source for the latest in IT news and self-hosting insights. At IOupdate, we are a dedicated team of technology enthusiasts committed to delivering timely and relevant information in the ever-evolving world of information technology. Our passion lies in exploring the realms of self-hosting, open-source solutions, and the broader IT landscape.

Most Popular

AI Developers Look Beyond Chain-of-Thought Prompting

May 9, 202515 Views

6 Reasons Not to Use US Internet Services Under Trump Anymore – An EU Perspective

April 21, 202512 Views

Subscribe to Updates

Facebook Mastodon Bluesky Reddit
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions
© 2026 ioupdate. All Right Reserved.

Type above and press Enter to search. Press Esc to cancel.