Introduction

Hello! In this article, we’ll learn about the pwd command.

pwd stands for “Print Working Directory” - basically it’s a command that tells you “where am I right now?” Super simple, but super useful.

What is the pwd Command?

When you move around different directories in Linux, you sometimes think “Wait, where am I right now?” That’s when you use the pwd command.

When you run it, it displays the absolute path of your current directory right away.

Basic Syntax

1
pwd [options]

Honestly, you’ll mostly use it without any options.

Main Options

Option Description
-L Show path including symbolic links (default)
-P Show actual path with symbolic links resolved

To be honest, I rarely use options with this command.

Usage Examples

Example 1: Basic Usage

1
pwd

Output:

1
/home/user/documents

That’s it. Simple, right?

Example 2: Using with cd

1
2
cd /var/log
pwd

Output:

1
/var/log

A common pattern is using pwd to confirm where you are after moving.

1
2
3
# Move via symbolic link
cd /home/user/mylink
pwd -L

Output:

1
/home/user/mylink
1
2
# Show actual path
pwd -P

Output:

1
/home/user/actual/path

When using symbolic links, the -P option shows you the real path.

Tips & Notes

  • Combine with cd: It’s handy to check if you actually arrived at your intended destination after changing directories

    1
    2
    
    cd ~/projects/myapp
    pwd  # Confirm location
    
  • Use in shell scripts: Often used in scripts when you need to get the current directory

    1
    2
    3
    
    #!/bin/bash
    CURRENT_DIR=$(pwd)
    echo "Current directory: $CURRENT_DIR"
    
  • Displayed in prompt: Actually, most terminals already show your current directory in the prompt

    1
    
    user@hostname:~/documents$
    

    But pwd is still useful when you want to see the full path or use it in scripts.

  • PWD environment variable: The same information is also stored in the $PWD environment variable

    1
    
    echo $PWD
    

Practical Usage

Return to Original Directory in Scripts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#!/bin/bash
# Save current directory
ORIGINAL_DIR=$(pwd)

# Work in another directory
cd /tmp
# Do some work...

# Return to original directory
cd "$ORIGINAL_DIR"

This is a common pattern in scripts.

When Lost in Deep Hierarchies

1
pwd

When you’re deep in directory hierarchies and thinking “Where am I?”, just run this and you’re good.

Summary

In this article, we learned about the pwd command.

Key Points:

  • pwd shows you the full path of your current directory
  • Basically just use it without options
  • Frequently used in shell scripts too
  • Your lifesaver when you’re lost

It’s a super simple command, but you’ll use it often. Good to remember cd and pwd as a set - they’re quite handy together.

Keep learning Linux commands!