Find the Current Working Directory of a Running Process in Linux

One of the basic tasks when working with processes on a Linux system is determining the current working directory of a process. The current working directory, also known as the "current directory" or "current working folder," is the directory in which a process runs and interacts with files. Knowing the current working directory of a process can be useful for debugging, understanding the environment in which a process is running, or monitoring process activity.

In this article, we will discuss how to find the current working directory of a running process on Linux. We will cover several methods including using the ps command, the lsof command, and the /proc filesystem.

Using the ps Command

The ps command is a standard command-line utility used to display information about processes currently running on a system. To use ps to find the current working directory of a process, use the following command

$ ps -o cwd -p PID

Where PID is the ID of the process for which we want to find the current working directory. For example, to find the current working directory of the process with PID 1234

$ ps -o cwd -p 1234

The -o option allows us to specify the output format for ps and the cwd argument tells ps to display the current working directory of the process.

Example Output

$ ps -o cwd -p 1234
CWD
/home/user/documents

In this example, the process with PID 1234 has a current working directory of /home/user/documents.

Using the lsof Command

The lsof command stands for "list of open files" and is used to display information about files currently open by processes. It can also show the current working directory of a process.

To use lsof to find the current working directory of a process

$ lsof -a -d cwd -p PID

Where PID is the process ID. For example

$ lsof -a -d cwd -p 1234

The -a option tells lsof to AND the selection criteria, and the -d option specifies the descriptor type. The cwd descriptor represents the current working directory.

Example Output

$ lsof -a -d cwd -p 1234
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
firefox 1234 user  cwd    DIR  253,1     4096  789 /home/user/downloads

Using the /proc Filesystem

The /proc filesystem is a virtual filesystem that provides a view of kernel and process information. Each running process has a directory under /proc/PID containing process details, including a symbolic link to its current working directory.

Method 1: Using readlink

The most direct approach is to read the cwd symbolic link

$ readlink /proc/PID/cwd

For example

$ readlink /proc/1234/cwd
/home/user/documents

Method 2: Using ls -l

You can also use ls -l to see where the symbolic link points

$ ls -l /proc/1234/cwd
lrwxrwxrwx 1 user user 0 Nov 15 10:30 /proc/1234/cwd -> /home/user/documents

Comparison of Methods

Method Command Advantages Disadvantages
ps command ps -o cwd -p PID Simple, clean output Limited process information
lsof command lsof -a -d cwd -p PID Detailed file information May require installation
/proc filesystem readlink /proc/PID/cwd Fast, always available Requires valid PID

Practical Examples

Finding Current Directory of Running Firefox

# First find Firefox PID
$ pgrep firefox
1234

# Then check its working directory
$ readlink /proc/1234/cwd
/home/user/downloads

Monitoring Multiple Processes

# Check working directories of all bash processes
$ for pid in $(pgrep bash); do
    echo "PID $pid: $(readlink /proc/$pid/cwd 2>/dev/null || echo 'N/A')"
done

Conclusion

Finding the current working directory of a running process in Linux can be accomplished using several methods. The /proc filesystem approach with readlink is generally the fastest and most reliable method, while ps and lsof provide additional process information. Each method has its place depending on your specific requirements and the level of detail needed.

Updated on: 2026-03-17T09:01:38+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements