How to Find the Working Directory of a Running Process in Linux Using pwdx

When troubleshooting runaway background scripts, orphaned worker jobs, or complex multi-tenant application servers in Linux, system administrators frequently need to determine the exact filesystem path from which a specific process was launched. While commands like ps and top display command arguments and binary names, they rarely reveal the current working directory (cwd). The dedicated pwdx utility provides an instantaneous, direct method to print the working directory of any running process using its Process ID (PID).

What Is pwdx and How Does It Function?

The pwdx utility is a core component of the standard procps (or procps-ng) package—the same fundamental system toolkit that provides ps, top, free, vmstat, and kill. Because it is part of this essential toolkit, pwdx comes pre-installed on virtually every Linux distribution without requiring third-party package installation.

Under the hood, Linux stores dynamic kernel metadata about every running process inside the virtual /proc filesystem. For any active process with identifier <PID>, the kernel maintains a symbolic link at /proc/<PID>/cwd pointing to the directory in which the process is currently operating. The pwdx tool reads and resolves this kernel symlink cleanly and outputs the absolute path in a human-readable format.

Basic Usage and Syntax

The syntax for pwdx requires passing one or more Process IDs as arguments:

pwdx <PID>

For example, if you have a running Python script whose PID is 28491:

pwdx 28491

The command prints the PID followed by the resolved absolute path:

28491: /var/www/html/analytics-service

Finding Process IDs to Query with pwdx

Before running pwdx, you need to identify the PID of the target application. Several common Linux tools help locate these identifiers:

  • Using pgrep: Search for running instances by process name:
    pgrep -u $USER node
  • Using pidof: Find the PID of a specific binary:
    pidof nginx
  • Using ps with grep: Search for processes with full command lines:
    ps aux | grep worker.py

Querying Multiple PIDs Simultaneously

The pwdx command accepts multiple PIDs in a single invocation, making it ideal for auditing clustered microservices or child threads:

pwdx 1042 1088 1120 2541

The console prints each result on a clean, separate line:

1042: /etc/nginx
1088: /var/log/redis
1120: /opt/backend/worker
2541: /home/deploy/project

Combining pwdx with Shell Pipelines for Automated Auditing

System administrators can combine pwdx with commands like pgrep and xargs to audit all instances of a daemon across large production servers.

Audit Working Directories of All Python Processes

pgrep python | xargs pwdx

This command automatically finds all PIDs associated with Python and passes them directly to pwdx, immediately showing whether rogue scripts are running out of /tmp or staging directories.

Find the Working Directory of the Current Shell

pwdx $$

Because the special shell variable $$ represents the PID of the current active terminal session, passing it to pwdx outputs your current directory (functioning similarly to pwd).

Permission Requirements and Root Privileges

When running pwdx against processes owned by your current user account, no elevated privileges are necessary. However, inspecting processes owned by root or other system accounts requires administrative rights:

sudo pwdx 1

Without sudo, querying a process owned by another user returns a permission denied error because Linux kernel security restricts unprivileged access to other users’ /proc/<PID>/cwd entries.

Alternative Method: Directly Reading /proc/PID/cwd

If you find yourself working inside a minimal container environment (such as Alpine Linux or a barebones recovery image) where pwdx might not be installed, you can achieve the exact same result using native shell built-ins:

readlink -f /proc/<PID>/cwd

Alternatively, inspect the symlink directly with ls:

ls -l /proc/<PID>/cwd

Both approaches read the same underlying kernel pointer, giving you reliable diagnostic flexibility across all Unix-like platforms.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.