## Using and Changing Working Directories

Each program (process) has two “working directories” associated with it at all times: a “data directory” and an “execution directory”. The working directory mechanism allows the name searching involved in pathlist processing to start at any level (subtree) of the file system hierarchy. Any directory that the user has permission to access (see the section called “The File Security System”) can be made a working directory.

The rules used to determine whether pathlists refer to the current working directory or not are simple:

---> When the first character of a pathlist is a “/”, processing of the pathlist starts at the device directory, e.g., the first name must be a device name.

---> When the first character of a pathlist is not a “/”, processing of the pathlist starts at the current working directory.

Notice that pathlists starting with a “/” must be complete, in other words, they must have all names required to trace the pathlist from the device directory down through all intermediate directories (if any). For example:

/d2/JOE/WORKINGFILES/testresults

On the other hand, use of the current working directory allows all names in the file hierarchy tree to be implied instead of explicitly given. This not only makes pathlists shorter, but allows OS-9 to locate files faster because (typically) fewer directories need be searched. For example, if the current working directory is `/D1/PETE/GAMES` and a pathlist is given such as:

baseball

the actual pathlist implied is:

/D1/PETE/GAMES/baseball

Pathlists using working directories can also specify additional lower-level directories. Referring to the example above, the pathlist:

ACTION/racing

implies the complete pathlist:

/D1/PETE/GAMES/ACTION/racing

### Automatic Selection of Working Directories

Recall that two working directories are referred to as the “current execution directory” and the “current data directory”. The reason two working directories are maintained is so that files containing programs can be organized in different directories than files containing data. OS-9 automatically selects either working directory, depending on the usage of the pathlist:

---> OS-9 will search the execution directory when it attempts to load files into memory assumed to be executable programs. This means that programs to be run as commands or loaded into memory must be in the current execution directory.

---> The data directory is used for all other file references (such as text files, etc.)

Immediately after startup, OS-9 will set the data directory to be (the root directory of) the system disk drive (usually “D0”), and the working directory to be a directory called `cmds` on the same drive (`/D0/cmds`). On timesharing systems, the login command selects the initial execution and data directories to the file names specified in each user's information record stored in the system password file(ref. the section called “Loading Multiple Programs”).

Here is an example of a shell command statement using the default working directory notation, and its equivalent expansion:

    copy file1 file2

If the current execution directory is `/D0/CMDS` and the current data directory is `/D0/JONES`, the same command, fully expanded to show complete pathlists implied is:

    OS9: /D0/CMDS/copy /D0/JONES/filel /D0/JONES/file2

Notice that the first pathlist copy expands to the current working directory pathlist because it is assumed to be an executable program but the two other file names expand using the data directory because they are not assumed to be executable.

### Changing Current Working Directories

The built-in shell commands chd and chx can be used to independently change the current working data and execution directories, respectively. These command names must be followed by a pathlist that describes the new directory file. You must have permission to access the directory according to normal file security rules. Here are some examples:

    OS9: chd /D1/MY.DATAFILES

    OS9: chx /D0/TESTPROGRAMS

When using the chd or chx commands, pathlists work the same as they do for regular files, except for the last name in the pathlist must be a directory name. If the pathlist begins with a “/” , OS-9 will begin searching in the device directory for the new working directory, otherwise searching will begin with the present directory. For example, the following sequence of commands set the working directory to the same file:

    OS9: CHD /D1/SARAH
    OS9: CHD PROJECT1

    OS9: CHD /D1/SARAH/PROJECT1    (same effect as above)

### Anonymous Directory Names

Sometimes is useful to be able to refer to the current directory or the next higher-level directory, but its name (full pathlist) may not be known. Because of this, special “name substitutes” are available. They are:

`.` | refers to the present working directory
`..` | refers to the directory that contains the name of the present directory (e.g., the next highest level directory)
`...` | refers to directory two levels up, and so on

These can be used in place of pathlists and/or the first name in a pathlist. Here are some examples:

OS9: dir . | lists file names in the working data directory
OS9: dir .. | lists names in the working data directory's parent directory.
OS9: del ../temp | deletes the file `temp` from the working data directory's parent directory.

The substitute names refer to either the execution or data directories, depending on the context in which they are used. For example, if `..` is used in a pathlist of a file which will be loaded and/or executed, it will represent the parent directory of the execution directory. Likewise, if `.` is used in a pathlist describing a program's input file, it will represent the current data directory.