Home Linux Tips & Tricks Find Current Working Directory Of A Process Using Pwdx In Linux

Find Current Working Directory Of A Process Using Pwdx In Linux

By sk
Published: Updated: 3.6K views

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 "pgrep" command like below:

$ pgrep firefox
5238

The PID of firefox is 5238. Now, we can check the working directory of the PID 5238 with pwdx command like below:

$ pwdx 5238

Sample output:

5238: /home/sk

Alternatively, you can combine both commands as a single command and display the current working directory of the firefox process like below:

$ pwdx $(pgrep firefox)
Find current working directory of a process using pwdx in Linux
Find current working directory of a process using pwdx in Linux

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
5238: /home/sk
21126: /home/sk

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:

  • ls
  • lsof
  • readlink

First, find the PID of the process with pgrep command:

$ pgrep firefox
5238

Next, get the process's current working directory using "ls" command like below:

$ ls -l /proc/5238/cwd

Here, cwd indicates current working directory.

Sample output:

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

Sample output:

firefox 5238 sk cwd DIR 8,1 4096 4325378 /home/sk

Display the current working directory of firefox process using "readlink" command, run:

$ readlink -e /proc/5238/cwd/
/home/sk

Refer man pages for details:

$ man ls
$ man lsof
$ man readlink

Hope this helps.

Related read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More