Working directory
In computing, the working directory is the directory of a file system which a relative path[a] is relative to. In a command-line environment, a user often uses files in or relative to a particular directory—this is called the working directory. The system state that identifies the working directory provides a convenient way to define the context of work. Users can use paths that are conceptually simpler and usually shorter, instead of always using absolute paths. For example, for working directory /home/user/data on a Unix-like system, the file name foo.txt (a relative path) refers to absolute path /home/user/data/foo.txt. System state for working directory often also applies in a graphical user interface (GUI), but often is not useful since the GUI environment maintains directory context in a different way.
As a feature, working directories vary by environment yet have some common, typical attributes. Typically, the working directory state is maintained separately for each process, which allows users to have a different working directory context for each shell running on the computer. Although common in systems that provide a hierarchical file system, the working directory feature is not required; for example, the DX10 operating system does not provide it.
Alternate names include current working directory (CWD)[b] and just current directory.[2]
Use
[edit]- Change directory
- Many shells provide a
cdcommand for setting the working directory. Some systems provide a command with a different name. For example,chdiris an alternate name forcdin DOS/Windows.
- Report
- Commands for reporting the working directory vary more than setting it. Typically, a Unix shell provides the command
pwd(short for print working directory) that reports the absolute path to the working directory. The equivalent command in DOS/Windows iscdwithout arguments. Usingcdwithout arguments in Unix-like systems generally sets the working directory to the user's home directory.
- Scripting
- To ease scripting development, some shells provide a variable that contains the working-directory path. Typically, a Unix-like shell provides an environment variable named PWD. Windows shells COMMAND.COM and cmd.exe provide a pseudo-environment variable named CD. Shells 4DOS, 4OS2, and 4NT provide _CWD, _CWDS, _CWP, and _CWPS[3]
- Windows shortcut
- A Windows file shortcut can store the working directory.[clarification needed]
- Command-line prompt
- The working directory is displayed by the $P[nb 1] token of the DOS
promptcommand.[4] To keep the prompt short in a deep directory, the DR-DOS 7.07 COMMAND.COM supports a $W[nb 1] token to display only the deepest subdirectory level. So, where a default PROMPT $P$G would result f.e. inC:\DOS>orC:\DOS\DRDOS>, a PROMPT $N:$W$G would instead yieldC:DOS>andC:DRDOS>, respectively. A similar facility (using $W and $w) was added to 4DOS as well.[3]
- State for each DOS drive
- Under DOS, the absolute path to the working directory for each logical volume is stored as the current directory structure (CDS). It is allocated at boot time with a slot for each logical drive (or as defined by LASTDRIVE).[5][6][7] This structure imposes a length-limit of 66 characters on the full path of each working directory, and thus implicitly also limits the maximum possible depth of subdirectories.[5]
- DOS Plus and older issues of DR DOS (up to DR DOS 6.0, with BDOS 6.7 in 1991) had no such limitation[6][8][3] due to their implementations using DOS emulation on top of a Concurrent DOS- (and thus CP/M-86-)derived kernel, which internally organized subdirectories as relative links to parent directories instead of as absolute paths.[6][8] Since PalmDOS (with BDOS 7.0) and DR DOS 6.0 (1992 update with BDOS 7.1) 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.[6][8]
- High-level language access
- Most programming-language environments provide an application programming interface to the file system for getting and setting the working directory.
- The POSIX-defined function
chdir()(accessible via C and the many languages that interoperate with it) is a system call that changes the working directory.[9] Its argument is a text string that is a path to the target directory, either absolute or relative to the existing value.
- Visual Basic provides the same functionality via a function with the same name.
- In Java, the working directory can be obtained via the
java.nio.file.Pathinterface, or thejava.io.Fileclass. The working directory cannot be changed.[10]
Examples
[edit]COMMAND.COM in DR-DOS 7.02 and higher provides ECHOS, a variant of the ECHO command that omits the terminating linefeed.[11][3] 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 (for "Batch").[12] This modifies the output of commands to become suitable for direct command-line input (when redirecting it into a batch file) or usage as a parameter for other commands (using it as input for another command). 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 returning to this directory later on.
See also
[edit]- chroot – Shell command and system call that changes the apparent root directory
- Home directory – Directory to hold files for a specific user
- Load drive – Alphabetical assignment to logical drives on computers (e.g., C:\)
- Long filename – Extension of the 8.3 file naming scheme
- Root directory – First or top-most directory in a hierarchy
Notes
[edit]- ^ a b In the same way as $O works as alternative to $P under DR-DOS COMMAND.COM, $W has an alias $Y except for that it gets suppressed on floppies in order to avoid critical error messages or delays when trying to retrieve the path of a non-inserted floppy.
References
[edit]- ^
- ^ "Microsoft Windows – SetCurrentDirectory function". Microsoft. Archived from the original on 2018-05-27. Retrieved 2017-05-27.
- ^ a b c d Brothers, Hardin; Rawson, Tom; Conn, Rex C.; Paul, Matthias R.; Dye, Charles E.; Georgiev, Luchezar I. (2002-02-27). 4DOS 8.00 online help.
- ^ Caldera DR-DOS 7.02 User Guide. Caldera, Inc. 1998 [1993, 1997]. Archived from the original on 2016-11-04. Retrieved 2013-08-10.
- ^ a b Schulman, Andrew; Brown, Ralf D.; Maxey, David; Michels, Raymond J.; Kyle, Jim (1994) [November 1993]. Undocumented DOS: A programmer's guide to reserved MS-DOS functions and data structures - expanded to include MS-DOS 6, Novell DOS and Windows 3.1 (2 ed.). Reading, Massachusetts: Addison Wesley. ISBN 0-201-63287-X. ISBN 978-0-201-63287-3. (xviii+856+vi pages, 3.5-inch floppy) Errata: [1][2]
- ^ a b c d Paul, Matthias R. (1997-07-30) [1994-05-01]. NWDOS-TIPs — Tips & Tricks rund um Novell DOS 7, mit Blick auf undokumentierte Details, Bugs und Workarounds. MPDOSTIP (in German) (3 ed.). Archived from the original on 2017-09-10. Retrieved 2014-08-06. (NB. NWDOSTIP.TXT is a comprehensive work on Novell DOS 7 and OpenDOS 7.01, including the description of many undocumented features and internals. It is part of the author's yet larger
MPDOSTIP.ZIPcollection maintained up to 2001 and distributed on many sites at the time. The provided link points to a HTML-converted older version of theNWDOSTIP.TXTfile.) [3] - ^ Brown, Ralf D.; et al., eds. (2000-07-16). "Ralf Brown's Interrupt List" (61 ed.). INTER61.
- ^ a b c Paul, Matthias R. (1997-06-07) [April 1994]. "Chapter 1.3.xi: Überlange Pfade". NWDOS7UN.TXT — Zusammenfassung der dokumentierten und undokumentierten Fähigkeiten von Novell DOS 7. MPDOSTIP (in German). Archived from the original on 2016-11-07. Retrieved 2016-11-07.
- ^ "The Open Group Base Specifications Issue 6 and IEEE Std 1003.1" (Issue 6, 2004 ed.). The IEEE and The Open Group. 2004 [2001]. Archived from the original on 2018-02-09. Retrieved 2018-05-27.
- ^ "JDK-4045688: Add chdir or equivalent notion of changing working directory". Bug Database. Oracle (Sun Microsystems). 2008-08-18 [1997-04-17]. Archived from the original on 2017-10-19. Retrieved 2011-02-20.
- ^ DR-DOS 7.03 WHATSNEW.TXT — Changes from DR-DOS 7.02 to DR-DOS 7.03. Caldera, Inc. 1998-12-24. Archived from the original on 2019-04-08. Retrieved 2019-04-08.
- ^ CCI Multiuser DOS 7.22 GOLD Online Documentation. Concurrent Controls, Inc. (CCI). 1997-02-10. HELP.HLP.
Further reading
[edit]- Chen, Raymond (2010-10-11). "Why does each drive have its own current directory?". The New Old Thing. Microsoft. Archived from the original on 2016-04-12. Retrieved 2017-05-27.
- Chen, Raymond (2010-05-06). "What are these strange =C: environment variables?". The New Old Thing. Microsoft. Archived from the original on 2018-05-27. Retrieved 2017-05-27.
External links
[edit]- Working Directory (wd) at Github – a software package that tracks commonly used directories in a bash session