Linux / Unix: pwdx Command Examples

I am new Linux and Unix system administrator. How do I find the current working directory of a process or server on Linux or Unix-like system using shell prompt?

On Linux and Solairs Unix you can use pwdx comamnd to display the current working directory of each process. [donotprint]

pwdx command details
DescriptionShow current working directory
of a process
CategoryN/A
DifficultyEasy
Root privilegesYes
Estimated completion time1m
Contents
[/donotprint] This command depends upon /proc/ file system to get a list of process-ids (PID). The pwdx command only available on Linux and Sun/Oracle Solaris Unix. If this command not available on your Unix-like system, try using combination of any one of the following command:

  1. lsof command
  2. ps command
  3. pgrep command

Purpose

Print the current working directory of each process.

Syntax

The basic syntax is as follows:

pwdx pid

OR

pwdx pid1 pid2 pid3

pwdx command examples

In this example, find out the working directory of a process called lighttpd. First, run ps command to find out PID of a process called lighttpd:
$ ps aux | grep {process-name}
$ ps aux | grep lighttpd

You can skip ps and grep combo and use pgrep command as follows for the same purpose:
$ pgrep lighttpd
Sample outputs:

2225

To display the current working directory of PID # 2225, enter:
$ sudo pwdx 2225
OR
# pwdx 2225
Sample outputs:

2225: /

Using /proc/ file system to find out the current working directory of a process

You can run the following command to get the same information:

pgrep processNameHere
ls -d  /proc/$PID/cwd
 
## if pid is 1313, try ##
ls -d  /proc/1313/cwd

Finding jail directory

For security concern certain process run inside the jail (see chroot command for more information). Here is an example of named (BIND DNS server) process running inside the jail:
# pgrep named
# pwdx 1617
# ls -d /proc/1617/cwd

Sample outputs:

In this example, output indicate that named process has root directory at /var/cache/bind location.

pwdx command options

From the pwdx(1) command man page:

Option Description
pids State pid a process or processes.
-V Displays version of program.
See also
  • pwdx(1) Linux/Unix command man page
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.