Unix Power ToolsUnix Power ToolsSearch this book

31.3. What Good Is a Current Directory?

People who think the cd command is all they need to know about current directories should read this article! Understanding how Unix uses the current directory can save you work.

Each Unix process has its own current directory. For instance, your shell has a current directory. So do vi, ls, sed, and every other Unix process. When your shell starts a process running, that child process starts with the same current directory as its parent. So how does ls know which directory to list? It uses the current directory it inherited from its parent process, the shell:

% ls
   ...Listing of ls's current directory appears,
         which is the same current directory as the shell.

Each process can change its current directory and that won't change the current directory of other processes that are already running. So:

When you really get down to it, what good is a current directory? Here it is: relative pathnames start at the current directory. Having a current directory means you can refer to a file by its relative pathname, like afile. Programs like ls access the current directory through its relative pathname . (dot) (Section 1.16). Without a current directory and relative pathnames, you'd always have to use absolute pathnames (Section 31.2) like /usr/joe/projects/alpha/afile.

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.