ArticleslgStudy

science

Working directory

Working directory is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Working directory rather than just read about it. In short: In computing, the working directory is the directory of a file system to which a relative path is relative. In a command-line environment, a user often uses files in or relative to the working directory.

Key takeaways

  • Working directory belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Working directory to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Working directory from memory before moving on to harder problems.

Reference excerpt

In computing, the working directory is the directory of a file system to which a relative path is relative. In a command-line environment, a user often uses files in or relative to 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, each process has a separate working directory, which allows users to have a different working directory for each shell running on the operating system. 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, as all pathnames are either absolute or relative to the system volume. Alternate names include current working directory (CWD) and just current directory.

Use Change directory Many shells provide a cd command for setting the working directory. Some systems provide a command with a different name. For example, chdir is an alternate name for cd in 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 is cd without arguments. Using cd without 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 Windows shortcut A Windows file shortcut can point to a working directory to be used when activating the shortcut's target. Command-line prompt The working directory is displayed by the $P token of the DOS prompt command. To keep the prompt short in a deep directory, 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 (using $W and $w) was added to 4DOS as well. 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). 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. DOS Plus and older issues of DR DOS (up to DR DOS 6.0, with BDOS 6.7 in 1991) had no such limitation 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. 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. 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. 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.Path interface, or the java.io.File class. The working directory cannot be changed.

Examples COMMAND.COM in DR-DOS 7.02 and higher provides ECHOS, a variant of the ECHO command that omits 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 (for "Batch"). 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 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:\)Pages displaying short descriptions of redirect targets Long filename – Extension of the 8.3 file naming scheme Root directory – First or top-most directory in a hierarchy

Notes

References

Further reading 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 Working Directory (wd) at Github – a software package that tracks commonly used directories in a bash session

Worked examples

Example 1 — a first encounter with Working directory

Start with the simplest possible case. Write down what Working directory claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Working directory before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Working directory ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Working directory

In research
Working directory appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Working directory in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Working directory is common in secondary-school and first-year university syllabi. It links to neighbouring topics File system directories, so understanding it makes those chapters shorter.
In everyday life
Look for Working directory outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Working directory” →

How to study Working directory in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Working directory means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Working directory out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Working directory in simple terms?

In computing, the working directory is the directory of a file system to which a relative path is relative. In a command-line environment, a user often uses files in or relative to the working directory.

Why does Working directory matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Working directory?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Working directory.

Tags

  • File system directories

Keep exploring