# Working directory

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Working_directory
> Markdown URL: https://mediated.wiki/source/Working_directory.md
> Source: https://en.wikipedia.org/wiki/Working_directory
> Source revision: 1351734780
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Default directory for file-system operations}}
{{Use dmy dates|date=July 2019|cs1-dates=y}}
{{Use list-defined references|date=December 2021}}
In computing, the '''working directory''' is the [directory](/source/Directory_(computing)) of a [file system](/source/file_system) to which a [relative path](/source/relative_path){{efn| such as a [Unix](/source/Unix) path that does not begin with slash (/) or a [Windows](/source/Windows) path that does not begin with backslash (\)}} is relative.

In a [command-line](/source/command-line) environment, a [user](/source/User_(computing)) often uses files in or relative to the working directory. The system [state](/source/State_(computer_science)) 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](/source/Fully_qualified_name) paths.  For example, for working directory <kbd>/home/user/data</kbd> on a [Unix-like](/source/Unix-like) system, the [file name](/source/file_name) <kbd>foo.txt</kbd> (a relative path) refers to absolute path <kbd>/home/user/data/foo.txt</kbd>. System state for working directory often also applies in a [graphical user interface](/source/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, each [process](/source/process_(computing)) has a separate working directory, which allows users to have a different working directory for each [shell](/source/shell_(computing)) running on the [operating system](/source/operating_system). Although common in systems that provide a [hierarchical file system](/source/hierarchical_file_system), the working directory feature is not required; for example, the [DX10 operating system](/source/DX10_operating_system) does not provide it, as all pathnames are either absolute or relative to the system volume.<ref>{{cite book |url=https://bitsavers.org/pdf/ti/990/dx10/0946250-9702D_DX10_Vol2_OperationsGuide_Sep82.pdf |title=DX10 Operating System Operations Guide |at=7.4 Using Pathnames |publisher=[Texas Instruments](/source/Texas_Instruments) |date=1982-09-01}}</ref>

Alternate names include '''current working directory (CWD)'''{{efn |as used in the command {{code |getcwd}}<ref name="getcwd"/>.}} and just '''current directory'''.<ref name="SetCurrentDirectory"/>

==Use==
; Change directory: Many [shells](/source/shell_(computing)) provide a <code>[cd](/source/CD_(command))</code> [command](/source/command_(computing)) for setting the working directory. Some systems provide a command with a different name. For example, {{code |chdir}} is an alternate name for {{code |cd}} in DOS/Windows.

; Report: Commands for reporting the working directory vary more than setting it. Typically, a [Unix shell](/source/Unix_shell) provides the command <code>[pwd](/source/pwd)</code> (short for ''print working directory'') that reports the absolute path to the working directory. The equivalent command in DOS/Windows is <code>cd</code> without [arguments](/source/parameter_(computer_science)). Using <code>cd</code> without arguments in Unix-like systems generally sets the working directory to the user's [home directory](/source/home_directory).

; Scripting: To ease scripting development, some shells provide a [variable](/source/variable_(computing)) that contains the working-directory path. Typically, a Unix-like shell provides an [environment variable](/source/environment_variable) named <kbd>PWD</kbd>. Windows shells [COMMAND.COM](/source/COMMAND.COM) and [cmd.exe](/source/cmd.exe) provide a [pseudo-environment variable](/source/pseudo-environment_variable) named <kbd>[CD](/source/CD_(pseudo-environment_variable))</kbd>. Shells [4DOS](/source/4DOS), [4OS2](/source/4OS2), and [4NT](/source/4NT_(shell)) provide <kbd>_CWD</kbd>, <kbd>_CWDS</kbd>, <kbd>_CWP</kbd>, and <kbd>_CWPS</kbd><ref name="4DOS_8.00_HELP"/> 

; Windows shortcut: A [Windows](/source/Windows) [file shortcut](/source/file_shortcut) can point to a working directory to be used when activating the shortcut's target.<ref>{{cite web |url=https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-shllink/17b69472-0f34-4bcf-b290-eccdb8de224b |title=2.4 StringData |work=[MS-SHLLINK]: Shell Link (.LNK) Binary File Format |publisher=[Microsoft](/source/Microsoft)}}</ref>

; Command-line prompt: The working directory is displayed by the <kbd>$P</kbd><ref group="nb" name="NB_PROMPT"/> token of the [DOS <code>prompt</code> command](/source/PROMPT_(DOS_command)).<ref name="Caldera_1998_USER"/> To keep the prompt short in a deep directory, the DR-DOS 7.07 COMMAND.COM supports a <kbd>$W</kbd><ref group="nb" name="NB_PROMPT"/> token to display only the deepest subdirectory level. So, where a default <kbd>PROMPT $P$G</kbd> would result f.e. in <code>C:\DOS></code> or <code>C:\DOS\DRDOS></code>, a <kbd>PROMPT $N:$W$G</kbd> would instead yield <code>C:DOS></code> and <code>C:DRDOS></code>, respectively. A similar facility (using <kbd>$W</kbd> and <kbd>$w</kbd>) was added to [4DOS](/source/4DOS) as well.<ref name="4DOS_8.00_HELP"/>

; State for each DOS drive: {{anchor|CDS}}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](/source/LASTDRIVE_(CONFIG.SYS_directive))).<ref name="Schulman_1994_Undocumented-DOS"/><ref name="Paul_1997_NWDOSTIP"/><ref name="Brown_2000_RBIL"/> 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.<ref name="Schulman_1994_Undocumented-DOS"/> 

: [DOS Plus](/source/DOS_Plus) and older issues of DR&nbsp;DOS (up to [DR&nbsp;DOS 6.0](/source/DR%26nbsp%3BDOS_6.0), with [BDOS](/source/BDOS) 6.7 in 1991) had no such limitation<ref name="Paul_1997_NWDOSTIP"/><ref name="Paul_1997_NWDOS7UN"/><ref name="4DOS_8.00_HELP"/> due to their implementations using [DOS emulation](/source/PCMODE) on top of a [Concurrent DOS](/source/Concurrent_DOS)- (and thus [CP/M-86](/source/CP%2FM-86)-)derived kernel, which internally organized subdirectories as ''relative'' links to parent directories instead of as ''absolute'' paths.<ref name="Paul_1997_NWDOSTIP"/><ref name="Paul_1997_NWDOS7UN"/> Since [PalmDOS](/source/PalmDOS) (with BDOS 7.0) and DR&nbsp;DOS 6.0 (1992 update with BDOS 7.1) and higher switched to use a CDS for [maximum compatibility](/source/bug_compatibility) with DOS programs as well, they faced the same limitations as present in other DOSes.<ref name="Paul_1997_NWDOSTIP"/><ref name="Paul_1997_NWDOS7UN"/>

; High-level language access: Most [programming-language](/source/programming_language) environments provide an [application programming interface](/source/application_programming_interface) to the [file system](/source/file_system) for getting and setting the working directory.

: The [POSIX](/source/POSIX)-defined function <code>chdir()</code> (accessible via [C](/source/C_language) and the many languages that interoperate with it) is a [system call](/source/system_call) that changes the working directory.<ref name="OGBS"/> Its argument is a [text string](/source/string_data_type) that is a path to the target directory, either absolute or relative to the existing value.

: [Visual Basic](/source/Visual_Basic) provides the same functionality via a function with the same name.

: In [Java](/source/Java_(programming_language)), the working directory can be obtained via the <code>java.nio.file.Path</code> interface, or the <code>java.io.File</code> class. The working directory cannot be changed.<ref name="Sun"/>

==Examples==

COMMAND.COM in [DR-DOS 7.02](/source/DR-DOS_7.02) and higher provides <code>ECHOS</code>, a variant of the <code>[ECHO](/source/ECHO_(DOS_command))</code> command that omits the terminating linefeed.<ref name="Caldera_1998_NEW703"/><ref name="4DOS_8.00_HELP"/> This can be used to create a temporary batchjob storing the working directory in an environment variable like <kbd>CD</kbd> for later use.  For example:

 ECHOS SET CD=> SETCD.BAT
 CHDIR >> SETCD.BAT
 CALL SETCD.BAT
 DEL SETCD.BAT

Alternatively, under [Multiuser DOS](/source/Multiuser_DOS) and [DR-DOS 7.02](/source/DR-DOS_7.02) and higher, various internal and external commands support a parameter <kbd>/B</kbd> (for "Batch").<ref name="CCI_1997_HELP"/> This modifies the output of commands to become suitable for direct command-line input (when redirecting it into a [batch file](/source/batch_file)) or usage as a parameter for other commands (using it as input for another command). Where <kbd>CHDIR</kbd> would issue a directory path like <code>C:\DOS</code>, a command like <kbd>CHDIR /B</kbd> would issue <code>CHDIR C:\DOS</code> instead, so that <kbd>CHDIR /B > RETDIR.BAT</kbd> would create a temporary batchjob allowing returning to this directory later on.

==See also==
* {{Annotated link |chroot}}
* {{Annotated link |Home directory}}
* {{Annotated link |Load drive}}
* {{Annotated link |Long filename}}
* {{Annotated link |Root directory}}

==Notes==
{{notelist}}
{{reflist|group="nb"|refs=
<ref group="nb" name="NB_PROMPT">In the same way as <kbd>$O</kbd> works as alternative to <kbd>$P</kbd> under [DR-DOS](/source/DR-DOS) [COMMAND.COM](/source/COMMAND.COM), <kbd>$W</kbd> has an alias <kbd>$Y</kbd> except for that it gets suppressed on [floppies](/source/floppies) in order to avoid [critical error](/source/critical_error) messages or delays when trying to retrieve the path of a non-inserted floppy.</ref>
}}

==References==
<references>

<ref name="SetCurrentDirectory">{{cite web |title=Microsoft Windows – SetCurrentDirectory function |publisher=[Microsoft](/source/Microsoft) |url=https://msdn.microsoft.com/de-de/library/windows/desktop/aa365530(v=vs.85).aspx |access-date=2017-05-27 |url-status=live |archive-url=https://web.archive.org/web/20180527133642/https://msdn.microsoft.com/de-de/library/windows/desktop/aa365530(v=vs.85).aspx |archive-date=2018-05-27}}</ref>
<ref name="OGBS">{{cite web |title=The Open Group Base Specifications Issue 6 and IEEE Std 1003.1 |date=2004 |edition=Issue 6, 2004 |orig-year=2001 |publisher=The [IEEE](/source/IEEE) and [The Open Group](/source/The_Open_Group) |url=http://www.opengroup.org/onlinepubs/009695399/functions/chdir.html |access-date=2018-05-27 |url-status=live |archive-url=https://web.archive.org/web/20180209195409/http://pubs.opengroup.org/onlinepubs/009695399/functions/chdir.html |archive-date=2018-02-09}}</ref>
<ref name="Sun">{{cite web |title=JDK-4045688: Add chdir or equivalent notion of changing working directory |work=Bug Database |date=2008-08-18 |orig-year=1997-04-17 |publisher=[Oracle](/source/Oracle_Corporation) ([Sun Microsystems](/source/Sun_Microsystems)) |url=http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4045688 |access-date=2011-02-20 |url-status=live |archive-url=https://web.archive.org/web/20171019003430/http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4045688 |archive-date=2017-10-19}}</ref>
<ref name="Caldera_1998_USER">{{cite book |title=Caldera DR-DOS 7.02 User Guide |publisher=[Caldera, Inc.](/source/Caldera%2C_Inc.) |date=1998 |orig-year=1993, 1997 |url=http://www.drdos.net/documentation/usergeng/uglontoc.htm |access-date=2013-08-10 |url-status=dead |archive-url=https://web.archive.org/web/20161104235434/http://www.drdos.net/documentation/usergeng/uglontoc.htm |archive-date=2016-11-04}}</ref>
<ref name="CCI_1997_HELP">{{cite book |title=CCI Multiuser DOS 7.22 GOLD Online Documentation |id=HELP.HLP |date=1997-02-10 |publisher=[Concurrent Controls, Inc.](/source/Concurrent_Controls%2C_Inc.) (CCI)}}</ref>
<ref name="Caldera_1998_NEW703">{{cite book |title=DR-DOS 7.03 WHATSNEW.TXT&nbsp;— Changes from DR-DOS 7.02 to DR-DOS 7.03 |publisher=[Caldera, Inc.](/source/Caldera%2C_Inc.) |date=1998-12-24 |url=http://www.lookas.net/ftp/incoming/darbui/Justas/DRDOS/WHATSNEW.TXT |access-date=2019-04-08 |url-status=live |archive-url=https://web.archive.org/web/20190408142232/http://www.lookas.net/ftp/incoming/darbui/Justas/DRDOS/WHATSNEW.TXT |archive-date=2019-04-08}}</ref>
<ref name="4DOS_8.00_HELP">{{cite book |title=4DOS 8.00 online help |title-link=4DOS 8.00 |author-first1=Hardin |author-last1=Brothers |author-first2=Tom |author-last2=Rawson |author-link2=Tom Rawson |author-first3=Rex C. |author-last3=Conn |author-link3=Rex C. Conn |author-first4=Matthias R. |author-last4=Paul |author-first5=Charles E. |author-last5=Dye |author-first6=Luchezar I. |author-last6=Georgiev |date=2002-02-27}}</ref>
<ref name="Schulman_1994_Undocumented-DOS">{{cite book |author-first1=Andrew |author-last1=Schulman |author-first2=Ralf D. |author-last2=Brown |author-link2=Ralf D. Brown |author-first3=David |author-last3=Maxey |author-first4=Raymond J. |author-last4=Michels |author-first5=Jim |author-last5=Kyle |title=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 |publisher=[Addison Wesley](/source/Addison_Wesley) |edition=2 |date=1994 |orig-year=November 1993<!-- first printing --> |isbn=0-201-63287-X |id={{ISBN|978-0-201-63287-3}} |location=Reading, Massachusetts |url-access=registration |url=https://archive.org/details/undocumenteddosp00andr_0 }} (xviii+856+vi pages, 3.5-inch floppy) Errata: [https://web.archive.org/web/20190417215556/http://www.cs.cmu.edu/afs/cs/user/ralf/pub/books/UndocumentedDOS/errata.ud2][https://web.archive.org/web/20190417212906/https://www.pcjs.org/pubs/pc/programming/Undocumented_DOS/#errata-2nd-edition]</ref>
<ref name="Paul_1997_NWDOSTIP">{{cite book |title=NWDOS-TIPs &mdash; Tips & Tricks rund um Novell DOS 7, mit Blick auf undokumentierte Details, Bugs und Workarounds |series=MPDOSTIP |author-first=Matthias R. |author-last=Paul |date=1997-07-30 |orig-year=1994-05-01 |edition=3 |language=de |url=http://www.antonis.de/dos/dos-tuts/mpdostip/html/nwdostip.htm |access-date=2014-08-06 |url-status=live |archive-url=https://web.archive.org/web/20170910194752/http://www.antonis.de/dos/dos-tuts/mpdostip/html/nwdostip.htm |archive-date=2017-09-10}} (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 <code>MPDOSTIP.ZIP</code><!-- still named TIPS_MP.ZIP between 1991 and 1996-11 --> collection maintained up to 2001 and distributed on many sites at the time. The provided link points to a HTML-converted older version of the <code>NWDOSTIP.TXT</code> file.) [https://web.archive.org/web/20190601152204/https://www.sac.sk/download/text/mpdostip.zip<!-- A yet older version 155 from 1997-05-13 of the 1997-07-15 distribution archive. -->]</ref>
<ref name="Paul_1997_NWDOS7UN">{{cite book |title=NWDOS7UN.TXT — Zusammenfassung der dokumentierten und undokumentierten Fähigkeiten von Novell DOS 7 |series=MPDOSTIP 
|chapter=Chapter 1.3.xi: Überlange Pfade |author-first=Matthias R. |author-last=Paul |date=1997-06-07 |orig-year=April 1994 |language=de |url=http://www.antonis.de/dos/dos-tuts/mpdostip/html/nwdos7un.htm |access-date=2016-11-07 |url-status=live |archive-url=https://web.archive.org/web/20161107122352/http://www.antonis.de/dos/dos-tuts/mpdostip/html/nwdos7un.htm |archive-date=2016-11-07}}</ref>
<ref name="Brown_2000_RBIL">{{cite web |title=Ralf Brown's Interrupt List |editor-first1=Ralf D. |editor-last1=Brown |editor-link1=Ralf D. Brown |display-editors=etal |edition=61 |id=[INTER61](/source/INTER61) |date=2000-07-16 |url=https://www.cs.cmu.edu/~ralf/files.html}}</ref>
<ref name="getcwd">{{man|3|getcwd|FreeBSD||inline}}</ref>

</references>

==Further reading==
* {{cite web |title=Why does each drive have its own current directory? |work=The New Old Thing |author-first=Raymond |author-last=Chen |author-link=Raymond Chen (Microsoft) |date=2010-10-11 |publisher=[Microsoft](/source/Microsoft) |url=https://blogs.msdn.microsoft.com/oldnewthing/20101011-00/?p=12563/ |access-date=2017-05-27 |url-status=live |archive-url=https://web.archive.org/web/20160412192309/https://blogs.msdn.microsoft.com/oldnewthing/20101011-00/?p=12563/ |archive-date=2016-04-12}}
* {{cite web |title=What are these strange =C: environment variables? |work=The New Old Thing |author-first=Raymond |author-last=Chen |author-link=Raymond Chen (Microsoft) |date=2010-05-06 |publisher=[Microsoft](/source/Microsoft) |url=https://blogs.msdn.microsoft.com/oldnewthing/20100506-00/?p=14133/ |access-date=2017-05-27 |url-status=live |archive-url=https://web.archive.org/web/20180527163745/https://blogs.msdn.microsoft.com/oldnewthing/20100506-00/?p=14133/ |archive-date=2018-05-27}}

==External links==
* [https://github.com/karlin/working-directory Working Directory (wd) at Github]{{spaced ndash}} a software package that tracks commonly used directories in a [bash](/source/Bash_(Unix_shell)) session

Category:File system directories

---
Adapted from the Wikipedia article [Working directory](https://en.wikipedia.org/wiki/Working_directory) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Working_directory?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
