home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 14.3 What Good Is a Current Directory? Chapter 14
Moving Around in a Hurry
Next: 14.5 Saving Time When You Change Directories: cdpath
 

14.4 How Does UNIX Find Your Current Directory?

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? See Figure 14.2 for a picture of the current directory /usr/joe and its parent directories. The current directory doesn't contain its own name, so that doesn't help pwd . But it has an entry named . (dot), which gives the i-number ofthe directory . ( 18.2 )

Figure 14.2: Finding the Current Directory Name

Figure 14.2

The current directory has i-number 234. Next, pwd asks UNIX to open the parent directory file, the directory one level up, through the relative pathname ( .. ). It's looking for the name that goes with i-number 234. Aha; the current directory is named joe . So the end of the pathname must be joe .

Next step. pwd looks at the . entry in the directory one level up to get its i-number: 14. Like always, the name of the one-level-up directory is in its parent ( .. , i-number 12). To get its name, pwd opens the directory two levels up and looks for i-number 14: usr . Now pwd has the pathname usr/joe .

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.

- JP


Previous: 14.3 What Good Is a Current Directory? UNIX Power Tools Next: 14.5 Saving Time When You Change Directories: cdpath
14.3 What Good Is a Current Directory? Book Index 14.5 Saving Time When You Change Directories: cdpath

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System