current working directory
setting directory
duplicate question
directory management
troubleshooting

How to set the current working directory?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

The current working directory is the base folder used for relative paths. Setting it is simple, but the correct method depends on where you want the change to happen: in your shell session, inside a running program, or for a child process launched by that program.

Change It in the Shell With cd

If you want the terminal itself to use a different working directory, use the shell command cd:

bash
cd /Users/markqian/projects/demo
pwd

After that, relative paths in the same shell session are resolved from the new location. This is the most common case when running scripts, invoking build tools, or opening files from the command line.

The key detail is scope: cd changes the directory only for the current shell process. It does not permanently change other terminals, editors, or already-running programs.

Change It Inside a Program

Programs can also change their own working directory. In Python, for example:

python
1import os
2
3print("Before:", os.getcwd())
4os.chdir("/tmp")
5print("After:", os.getcwd())

After os.chdir, relative file access in that process uses the new directory. Node.js has a similar API:

javascript
console.log("Before:", process.cwd());
process.chdir("/tmp");
console.log("After:", process.cwd());

This is useful when a script expects config files, templates, or output directories relative to a known location.

Use Absolute Paths When You Can

Changing the working directory is sometimes necessary, but many bugs disappear if you build absolute paths instead. For example, in Python:

python
1from pathlib import Path
2
3project_root = Path("/Users/markqian/projects/demo")
4config_path = project_root / "config" / "settings.json"
5
6print(config_path.read_text())

This avoids hidden coupling between one part of the code and whatever directory a different part happened to set earlier. In library code especially, silently changing the process working directory is usually a bad surprise for callers.

Child Processes Inherit the Parent Directory

When a program launches another command, the child process normally inherits the parent's current directory unless you override it.

In Python:

python
import subprocess

subprocess.run(["pwd"], cwd="/tmp", check=True)

This changes the working directory only for the child process running pwd. The Python process itself stays where it was. That is often the safest pattern because it limits the scope of the change.

Know What You Cannot Do

A frequent point of confusion is expecting a script to change the current directory of the shell that launched it. A child process cannot directly change the parent shell's working directory.

For example, if a Bash script runs cd /tmp, the script itself moves to /tmp, but the shell that invoked the script stays where it started after the script exits. To affect the current shell, you need to run the command in that shell context, such as by typing cd directly or sourcing a script.

Common Pitfalls

  • Changing the working directory in one part of an application and breaking relative paths somewhere else.
  • Expecting a child process or standalone script to change the parent shell's directory after it exits.
  • Using relative paths in long-running services where the starting directory may differ between environments.
  • Assuming the working directory and the script file location are the same. They often are not.
  • Forgetting to verify the current directory with pwd, os.getcwd(), or process.cwd() while debugging path issues.

Summary

  • Use cd to change the working directory in a shell session.
  • Use APIs such as os.chdir or process.chdir to change it inside a program.
  • Prefer absolute paths when you want predictable file access.
  • Set the directory per child process when you only need the change for one command.
  • Remember that a child process cannot permanently change the parent shell's working directory.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.