Working Directory
The directory in a file system that a process or shell session is currently operating in, commonly referenced via PWD or CWD.
Also known as: Current Working Directory, CWD, PWD, Present Working Directory, Print Working Directory
Category: Software Development
Tags: software-engineering, tools, cli
Explanation
A working directory (also called current working directory or CWD) is the directory within a file system that a process or shell session is currently operating in. It serves as the reference point for resolving relative file paths. When a user or program references a file without specifying a full path, the operating system looks for that file relative to the working directory.
The concept is central to command-line interfaces. In Unix-like systems, the `pwd` (print working directory) command displays the current working directory, while the `cd` (change directory) command changes it. The `PWD` environment variable stores the current working directory path, and `OLDPWD` stores the previous one, enabling `cd -` to toggle between directories.
Every running process has a working directory, not just interactive shells. When a program opens a file using a relative path like `data/config.json`, the operating system resolves it by prepending the process's working directory. This is why the same command can produce different results depending on where it is executed from.
The terms PWD (Present Working Directory or Print Working Directory) and CWD (Current Working Directory) are often used interchangeably, though they have slightly different origins. PWD typically refers to the shell command or environment variable, while CWD is the more general operating system concept referring to a process's current directory. In C programs, the `getcwd()` function retrieves the current working directory, and `chdir()` changes it.
Understanding working directories is essential for shell scripting, build systems, and development workflows. Many tools behave differently based on the directory from which they are run. Build tools look for configuration files in the working directory, version control systems use it to determine the repository context, and package managers resolve dependencies relative to it.
Common pitfalls include assuming a script's working directory matches its file location (it may not), hardcoding absolute paths instead of using relative ones appropriately, and forgetting that child processes inherit the parent's working directory unless explicitly changed.
Related Concepts
← Back to all concepts