Working directory


In computing, the working directory of a process is a directory of a hierarchical file system, if any, dynamically associated with each process. It is sometimes called the current working directory , e.g. the BSD function, or just current directory. When the process refers to a file using a simple file name or relative path, the reference is interpreted relative to the working directory of the process. So for example a process with working directory /rabbit-shoes that asks to create the file foo.txt will end up creating the file /rabbit-shoes/foo.txt.

In operating systems

In most computer file systems, every directory has an entry which points to the directory itself.
In most DOS and UNIX command shells, as well as in the Microsoft Windows command line interpreters cmd.exe and Windows PowerShell, the working directory can be changed by using the CD or CHDIR commands. In Unix shells, the pwd command outputs a full pathname of the working directory; the equivalent command in DOS and Windows is CD or CHDIR without arguments.
The environment variable PWD, or the pseudo-environment variables CD, or _CWD, _CWDS, _CWP and _CWPS can be used in scripts, so that one need not start an external program. Microsoft Windows file shortcuts have the ability to store the working directory.
COMMAND.COM in DR-DOS 7.02 and higher provides ECHOS, a variant of the ECHO command omitting the terminating linefeed. This can be used to create a temporary batchjob storing the working directory in an environment variable like CD for later use, for example:
ECHOS SET CD=> SETCD.BAT
CHDIR >> SETCD.BAT
CALL SETCD.BAT
DEL SETCD.BAT
Alternatively, under Multiuser DOS and DR-DOS 7.02 and higher, various internal and external commands support a parameter /B. This modifies the output of commands to become suitable for direct command line input or usage as a parameter for other commands. Where CHDIR would issue a directory path like C:\DOS, a command like CHDIR /B would issue CHDIR C:\DOS instead, so that CHDIR /B > RETDIR.BAT would create a temporary batchjob allowing to return to this directory later on.
The working directory is also displayed by the $P token of the PROMPT command To keep the prompt short even inside of deep subdirectory structures, the DR-DOS 7.07 COMMAND.COM supports a $W token to display only the deepest subdirectory level. So, where a default PROMPT $P$G would result f.e. in C:\DOS> or C:\DOS\DRDOS>, a PROMPT $N:$W$G would instead yield C:DOS> and C:DRDOS>, respectively. A similar facility was added to 4DOS as well.
Under DOS, the absolute paths of the working directories of all volumes are internally stored in an array-like data structure called the Current Directory Structure, which gets dynamically allocated at boot time to hold the necessary number of slots for all drives had no such limitation due to their implementation using a DOS emulation on top of a Concurrent DOS- derived kernel, which internally organized subdirectories as relative links to parent directories instead of as absolute paths. Since PalmDOS and DR DOS 6.0 and higher switched to use a CDS for maximum compatibility with DOS programs as well, they faced the same limitations as present in other DOSes.

In programming languages

Most programming languages provide an interface to the file system functions of the operating system, including the ability to set the working directory of the program.
In the C language, the POSIX function chdir effects the system call which changes the working directory. Its argument is a text string with a path to the new directory, either absolute or relative to the old one. Where available, it can be called by a process to set its working directory.
There is the same function in other languages. In Visual Basic it is usually spelled CHDIR.
Unlike the Windows API or POSIX C function, neither the Java programming language nor the Java Virtual Machine supports chdir directly; a change request remained open for over a decade while the team responsible for Java considered the alternatives, though by 2008 the request was denied after only limited support was introduced.