You are aware of “pwd” command, aren’t you? The pwd command (stands for Present Working Directory) is used to print the current working directory. What about “pwdx”? Have you ever used or heard of it? No? No problem! The pwdx command is to used report current working directory of a process. In this guide, we will see how to find current working directory of a process using pwdx command in Linux.
Find Current Working Directory Of A Process Using Pwdx In Linux
The general usage of pwdx command is given below:
$ pwdx <PID>
For the purpose of this guide, we will find the working directory of the firefox process.
First, we need to find the process ID of the the firefox. To do so, use “ps” command like below:
$ pgrep firefox
The PID of firefox is 5238. Now, find out the working directory of the PID 5238 like below:
$ pwdx 5238
Alternatively, you can combine both commands as a single command and find the current working directory of the firefox process like below:
$ pwdx $(pgrep firefox)
As you can see, the current working directory of firefox process is /home/sk. This way we can easily find out on which directory a process is currently running! Please note that these commands doesn’t display where a process was invoked from, only where it currently is.
If you want to print the current directory of multiple processes, mention the PIDs with space-separated like below:
$ pwdx 5238 21126
For more details, refer man pages.
$ man pwdx
Find current working directory of a Linux process using ls, lsof and readlink commands
If pwdx is not available for any reason, the following commands can get you the working directory of Linux processes:
First, find the PID of the process with pgrep command:
$ pgrep firefox
Next, find the process’s current working directory using”ls” command like below:
$ ls -l /proc/5238/cwd
Here, cwd indicates current working directory.
lrwxrwxrwx 1 sk sk 0 Jun 17 15:31 /proc/5238/cwd -> /home/sk
To find out the current working directory of firefox process using “lsof” command, run:
$ lsof -p 5238 | grep cwd
Alternatively, combine both commands and get the result with the following one-liner :
$ lsof -p $(pgrep firefox) | grep cwd
firefox 5238 sk cwd DIR 8,1 4096 4325378 /home/sk
Find out the current working directory of firefox process using “readlink” command, run:
$ readlink -e /proc/5238/cwd/
Refer man pages for details:
$ man ls
$ man lsof
$ man readlink
Source : Ostechnix