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


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 14.2 Using Relative and Absolute Pathnames Chapter 14
Moving Around in a Hurry
Next: 14.4 How Does UNIX Find Your Current Directory?
 

14.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:

  • Your shell script (which runs in a separate process) can cd to another directory without affecting the shell that started it (the script doesn't need to cd back before it exits).

  • If you have more than one window or login session to the same computer, they probably run separate processes. So, they have independent current directories.

  • When you use a subshell (13.7 , 38.4 ) or a shell escape, you can cd anywhere you want. After you exit that shell, the parent shell's current directory won't have changed. For example, if you want to run a command in another directory without cd ing there first (and having to cd back), do it in a subshell:

    % pwd
    
    
    /foo/bar
    % (cd baz
    
    ; somecommand 
    
    > somefile
    
    )
    
    
    % pwd
    
    
    /foo/bar

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 . Without a current directory and relative pathnames, you'd always have to use absolute pathnames (14.2 ) like /usr/joe/projects/alpha/afile .

- JP


Previous: 14.2 Using Relative and Absolute Pathnames UNIX Power Tools Next: 14.4 How Does UNIX Find Your Current Directory?
14.2 Using Relative and Absolute Pathnames Book Index 14.4 How Does UNIX Find Your Current Directory?

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