A command like pwd
inherits the current directory of the process
that started it (usually, a shell).
It could be started from anywhere.
How does pwd
find out where it is in the filesystem?
for a picture of the current directory /usr/joe
and its parent
The current directory doesn't contain its own name, so that doesn't help
But it has an entry named
The current directory has i-number 234.
asks UNIX to open the parent directory file,
the directory one level up,
through the relative pathname (
looks at the
Same steps: look in the parent, i-number 12. What's its name? Hmmm. The i-number of its parent, 12, is the same as its own-and there's only one directory on the filesystem like this: the root directory. So, pwd adds a slash to the start of the pathname and it's done: /usr/joe .
That's really missing one or two parts: Filesystems can be mounted on other filesystems, or can be mounted across the network from other hosts. So, at each step, pwd also needs to check the device that the current directory is mounted on. If you're curious, see the stat (2) manual page or check a UNIX internals book. Also see the last few paragraphs of article 18.4 for more about the links between directories.