Linux has a /proc
directory and file‑system, which as far as I can tell, is not part of POSIX. In each /proc/$PID
subdirectories, is a symbolic link, cwd
, pointing to the actual working directory of the process of this PID (the cwd
link is always up to date).
This symbolic link is convenient for some use case, like working with distinct shells and exchanging files between the two shells (formally, their working directories).
Is there a simple way to get something similar, only using POSIX feature?
More on the question
After a comment, more precision: it does not have to necessarily be a link and an environment variable lile $<PID>_CWD
, would be as much fine too, although at first sigh, I don't believe such a solution exist. It just has to be easy to refer to (ex. symbolic link or environment variable) and be always up to date each time the other process switch it's working directory.
The solution does not need to necessarily be POSIX, and the most important aspect is portability, but POSIX is surely a guarantee.
cwd
of a process? – enedil Jul 08 '14 at 13:38pwdx
command, so you may just create an alias or function calledpwdx
on Linux that wraps aroundreadlink
if you're looking for something portable. – Bratchley Jul 08 '14 at 14:07pwdx
command that comes withprocps
so that may be your answer. – Bratchley Jul 08 '14 at 14:10cp foo $(pwdx $PID)/bar
?. That's an option I though about indeed. Great you can confirmpwdx
is widely known. I will still wait for any future replies. Feel free to add your own answer usingpwdx
. – Hibou57 Jul 08 '14 at 14:16pwdx
. But FreeBSD doesn't. – enedil Jul 08 '14 at 14:18getcwd(3)
used to work by callingstat(".")
andreaddir("..")
, finding a matching inode number, and repeating the process upward until it hit the root directory. Good luck doing that in the context of another process. (I suppose one could useptrace
to inject a call togetcwd
...) – zwol Jul 08 '14 at 20:22