1. Overview
Let’s suppose that we have two similar running processes, each one is launched in a different terminal, and both are reading files using a relative path.
If we want to know the exact files the processes are reading, we need to find the current working directory of both.
In this tutorial, we’re going to see how we can find the current working directory (CWD) of a running process. We’ll take a look at a few ways to find the CWD, including pwdx and lsof. Additionally, we’ll read the CWD link in the /proc directory.
2. pwdx
Let’s start a simple process in the terminal:
$ sleep 1000 &
[1] 5620
We use & to run the sleep process in the background because we want to enter other commands in the same terminal. We could also run sleep in the foreground (without &) and open another shell for further commands.
Let’s use the pwdx command to identify the current working directory of the process:
$ pwdx <PID>
Since pwdx needs the process ID (PID) to print the CWD of sleep, we’ll first find the PID of the process using the pgrep command:
$ pgrep sleep
5620
The output of pgrep is the PID of our process. Note that this number is equal to the number returned by the shell in the sleep example. Now, let’s see what is the current working directory of the sleep process:
$ pwdx 5620
5620: /home/pi
The CWD of the pwdx is /home/pi.
To understand the CWD concept, let’s run pwd in the same terminal that we run sleep:
$ pwd
/home/pi
We can see that the CWD of the process is the same as the CWD of the shell. Therefore, if we use cd to go to the /home/pi/Rey path, and then run a new sleep process, the CWD of the new process would be /home/pi/Rey. Of course, the CWD of the previous process won’t change:
$ cd Rey/
$ sleep 2000 &
[2] 23217
$ pwdx 23217
23217: /home/pi/Rey
Note that the pwdx command doesn’t display where the process was invoked. It just shows where it currently runs from. Also, notice that we can pass multiple PIDs to the pwdx command and get the current working directory of multiple processes at the same time:
$ pwdx 5620 23217
5620: /home/pi
23217: /home/pi/Rey
3. lsof
The lsof (list open files) command returns information about all processes that are currently active. Since everything in Linux is considered a file, we can get a lot of information about processes with this command. Let’s find the current working directory of the sleep process with the lsof command:
$ lsof -p 5620 | grep cwd
sleep 5620 pi cwd DIR 179,2 4096 1347 /home/pi
We specify the sleep process with the -p option and the PID. We can find the PID of the process with pgrep as we mentioned in the previous section. Since the output of the lsof contains a lot of information about the process, we’ll limit the output by piping it to a grep command that prints only the lines containing the CWD.
4. Checking the /proc Directory
The directory /proc represents a virtual filesystem in Linux that contains runtime system information, including process information. There are some numbered directories in the /proc directory, each corresponding to an actual process ID. So, details about the process can be obtained by looking at the directory for that process, which is the path /proc/PID.
Since we’re looking for the current working directory of sleep, we need to look at the cwd link in the /proc/PID path:
$ ls -l /proc/5620/cwd
lrwxrwxrwx 1 pi pi 0 Sep 18 16:52 /proc/5620/cwd -> /home/pi
We use ls -l to see the detailed list of the directory. Let’s find the cwd of the second process:
$ ls -l /proc/23217/cwd
lrwxrwxrwx 1 pi pi 0 Sep 18 12:58 /proc/23217/cwd -> /home/pi/Rey
The result is a link to the CWD. We can also use the readlink command, which reads and prints the contents of a symbolic link in /proc/PID/cwd:
$ readlink -e /proc/23217/cwd
/home/pi/Rey
We use the -e option for readlink to be sure that all the components in the symbolic link exist.
5. Conclusion
Every process has a current working directory that is assigned to the process when it starts up. In this article, we looked at some common Linux commands to find the current working directory of a process.