Linux / Unix: pwdx Command Examples

by on January 22, 2013 · 0 comments· LAST UPDATED February 1, 2014

in , ,

pwdx command

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.

pwdx command details
DescriptionShow current working directory
of a process
Category
Difficulty
Root privilegesYes
Estimated completion time1m
Contents
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:
pwdx command demo
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:

OptionDescription
pidsState pid a process or processes.
-VDisplays version of program.
See also
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdighostip
Processes Managementbgchrootdisownfgjobskillkillallpwdxtimepidofpstree
Searchingwhereiswhich
User Informationgroupsidlastlastcommlognameuserswwhowhoamilidmembers

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as: , , ,

Previous Faq:

Next Faq: