pwd Command in Linux

The pwd command stands for "print working directory". It displays the full path of the current directory you are working in. It’s a simple yet essential command used frequently in Linux terminal sessions.

Basic Syntax

pwd [options]

Common Examples

Useful Options

Example

$ pwd
/home/user/projects

$ cd ../Documents
$ pwd
/home/user/Documents

Tips

pwd Command in Linux – Print Working Directory Explained

The pwd command in Linux is one of the simplest yet most essential commands for any user working in a terminal. Whether you’re navigating directories, writing shell scripts, or managing file paths, knowing your current location in the filesystem is crucial. This is where the pwd command comes in. In this comprehensive guide, we will explore what pwd does, how it works, syntax, real-world use cases, options, and advanced usage patterns.

What is pwd in Linux?

The pwd command stands for “Print Working Directory.” It displays the full path of the current directory you're working in. It is especially useful when you’ve navigated deep into nested folders and need to confirm your exact location in the file system.

Basic Syntax

pwd [OPTION]

The command has no required arguments but accepts a few optional flags.

Simple Example

$ pwd
/home/venkatesh/projects/webapp

This output shows that the user is currently in the webapp directory inside /home/venkatesh/projects/.

Why Use the pwd Command?

Options Available with pwd

The pwd command supports a few options that influence its behavior.

1. -L (Logical)

This is the default. It prints the logical path, including symbolic links.

pwd -L

2. -P (Physical)

This prints the actual physical directory path, resolving all symbolic links to their real targets.

pwd -P

Example of Logical vs Physical

cd /var/www/html
ln -s /var/www/html /home/user/website
cd /home/user/website
pwd -L  # Output: /home/user/website
pwd -P  # Output: /var/www/html

This demonstrates how -L shows the path with symlinks, while -P resolves them.

Understanding Absolute and Relative Paths

pwd always returns an absolute path, which starts from the root directory (/). It’s useful for creating scripts that require full directory references rather than relative paths like ../.

Integration with Other Commands

You can combine pwd with other Linux commands using shell features like variables and piping.

1. Assign Current Directory to a Variable

CUR_DIR=$(pwd)

This stores the current directory path in a variable.

2. Use in Bash Scripts


#!/bin/bash
echo "Current Directory: $(pwd)"
cd /tmp
echo "Now in: $(pwd)"
  

Using pwd in Shell Prompt Customization

You can modify your shell prompt to include the current directory using pwd or shell variables like $PWD.

PS1="\u@\h:\$(pwd)\$ "

This custom prompt shows username, host, and current directory.

pwd vs $PWD

pwd is a command, whereas $PWD is a shell variable holding the current directory path. Both generally show the same output, but pwd always invokes a command while $PWD is faster in scripts.

Use Cases of pwd in Real Life

pwd in Different Shells

Most Unix-like shells (bash, zsh, sh, fish) support pwd. Some implement it as a shell builtin for speed.

Check if pwd is Built-in

type pwd

Output: pwd is a shell builtin

Limitations of pwd

pwd and Symbolic Links

Symbolic links are shortcuts pointing to other directories. The pwd command helps you detect whether you're in a real directory or a symlinked path using -P.

Security and Scripting Caution

Examples for Practice

Common Errors and Fixes

How pwd Works Internally

Under the hood, pwd uses the system call getcwd() (get current working directory) to retrieve the absolute path. In shells, $PWD is often updated dynamically by the shell during each cd command.

pwd in GUI vs CLI

While graphical file managers display your location in a window, CLI users rely on pwd to show the same. It's often more efficient to use CLI in server environments or when scripting.

pwd Command in Job Interviews

Interviewers often ask about pwd to test a candidate’s understanding of directory structures, absolute vs relative paths, and script-writing fundamentals.

Sample Question: How does pwd -L differ from pwd -P?

Aliases and Shortcuts

alias mydir='pwd -P'

This alias helps quickly get the resolved physical path of the current directory.

Final analysis

The pwd command might seem basic, but it's an indispensable tool for Linux users, from beginners to seasoned system administrators. Its ability to provide the absolute path of the working directory plays a vital role in navigation, automation, and scripting. When combined with other commands and used wisely, pwd enhances efficiency, accuracy, and control in terminal-based workflows.

Advanced Scripting with pwd

In advanced scripting scenarios, pwd becomes a critical component. Especially in CI/CD pipelines, Dockerfile creation, deployment tasks, and bash automation, the current working directory is used to determine context and execution locations.

Example: Save Current Path, Change Directory Temporarily


#!/bin/bash
ORIGINAL_DIR=$(pwd)
cd /tmp
# Do something in /tmp
cd "$ORIGINAL_DIR"
  

This snippet ensures that a script always returns to the original location, which is useful for writing reliable and reversible shell scripts.

Dynamic Path Generation

When working on large projects with multiple folders, dynamically capturing the current working directory using pwd helps in:

pwd in Docker and Containers

Within Docker containers, pwd is used to understand the context of the running container. Developers often use it while building images or writing Dockerfiles.

Example: Dockerfile with pwd


FROM ubuntu:latest
WORKDIR /app
RUN echo "Current directory is $(pwd)"
  

Note: The shell may not interpret $(pwd) at Dockerfile build time without a wrapper script, but it’s still conceptually valuable.

Using pwd with Git Repositories

Developers frequently work within Git repositories and use pwd to determine if they're in the correct working directory before issuing Git commands.

Example: Git Safety Check


if [[ $(pwd) == *"/my-project"* ]]; then
  git pull origin main
else
  echo "Not inside my-project repo"
fi
  

pwd Command in Python Scripts

Even though pwd is a Linux command, it’s often mimicked in programming languages like Python to get the current working directory.

Equivalent in Python


import os
print("Current directory:", os.getcwd())
  

This is functionally the same as pwd in a bash terminal.

Combining pwd with Tar or Rsync

Backup and archive scripts often use pwd to dynamically create archive names or destination folders.

Example: Tar Backup with pwd


tar -czf "$(basename $(pwd))_backup.tar.gz" .
  

This archives the current folder with a name based on the directory you're in.

Example: Rsync Backup


rsync -avz ./ /mnt/backup/$(basename $(pwd))/
  

Perfect for automated scripts that run in multiple project folders.

pwd with Pipes and Command Substitution

You can use pwd in command substitution ($(pwd)) to inject the current directory path into other commands.

Copy File to Another Folder Named After Current Directory


cp file.txt /backups/$(basename $(pwd))/
  

pwd in Cron Jobs

Cron runs in a limited environment and doesn’t always have predictable starting directories. Using pwd inside cron jobs helps you log or verify where the task was executed.

Example Cron Script


#!/bin/bash
echo "Running backup from $(pwd) at $(date)" >> /var/log/cron-backup.log
  

Security Use Cases

Environment Variable: $PWD vs pwd

While pwd is a command that runs a system-level or shell builtin, $PWD is a shell variable. In many cases, they behave the same, but here are some differences:

Performance

$PWD is faster since it doesn’t invoke a subprocess.

Accuracy

pwd -P is more accurate if you want the true path (resolving symlinks), which $PWD doesn’t always provide.

Checking Script Location Using pwd

In bash, you can determine the directory of the script itself using pwd combined with ${BASH_SOURCE[0]}.

Example


SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "Script is in $SCRIPT_DIR"
  

This is widely used in complex bash applications and software install scripts.

pwd in Multi-User Environments

System administrators use pwd to confirm the working directory while switching between multiple user sessions, especially using su or sudo -i.

pwd with Tmux or Screen Sessions

When running terminal multiplexers like tmux or screen, each session may run in a different working directory. pwd helps identify where you are inside each pane.

Internationalization and pwd

In non-English locales, directory names can include UTF-8 or Unicode characters. pwd still returns the full path correctly if your locale settings are configured to support Unicode (like en_US.UTF-8).

pwd with ANSI Escape Sequences

Some shells allow embedding pwd in colorful prompts using ANSI escape sequences. This improves readability in complex terminal sessions.

Example Bash Prompt with pwd


PS1='\[\e[32m\]\u@\h:\w\$\[\e[m\] '
  

Fun Fact: pwd is Over 40 Years Old!

The pwd command has existed since the early days of Unix (1970s). It was part of the original Unix operating system developed at Bell Labs and has since become a standard in all POSIX-compliant systems.

Quick Recap: pwd Highlights

FAQs – Quick Answers About pwd

See Also