Terminal · foundation
Current working directory
The current working directory is the directory a process uses as the starting point when resolving a relative pathname.
Why it matters
The same relative command can read, create, or delete different files after a directory change, so knowing the current directory prevents wrong-target mistakes.
Mental model
How to reason about current working directory
Every process carries its own current-directory reference; cd changes that reference in the shell process, and commands launched afterward inherit it.
Analogy
It is the map location marked 'you are here': relative directions start there, while a full street address does not depend on it.
Examples
See the boundary, not just the happy path
Worked example · Change context deliberately
cd /var/log && pwdcd updates the current shell's directory, and pwd reports the resulting logical pathname only if cd succeeded.
Worked example · Relative target inherits context
cd /tmp; mkdir terminaster-demomkdir resolves terminaster-demo relative to /tmp because it inherits the shell's current directory when launched.
Avoid · Assumed location
rm -r buildRunning a destructive relative command without first confirming the current directory risks removing a different build directory than intended.
Common mistakes
Misconceptions to remove early
Expecting a child to change its parent's directory
A separately executed program can change only its own current directory; cd must be a shell built-in to affect the interactive shell.
Treating PWD as the kernel's source of truth
PWD is a shell-maintained logical path that may retain symlink components, while pwd -P asks for a physical path with symlinks resolved.
Quick check
Can you predict the result?
1. After cd /srv/app, where does the relative path logs/api.log begin resolving?
- • /srv/app
- • /
- • The user's home directory
2. Why is cd normally implemented as a shell built-in?
Keep building