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.
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:
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:
After os.chdir, relative file access in that process uses the new directory. Node.js has a similar API:
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:
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:
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(), orprocess.cwd()while debugging path issues.
Summary
- Use
cdto change the working directory in a shell session. - Use APIs such as
os.chdirorprocess.chdirto 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
- How to set the InnerException of custom Exception class from its constructor
- How to set the working directory for debugging a Python program in VS Code?
- How to show a MySQL warning that just happened?
- How to skip corrupt (non-serializable) messages in Spring Kafka Consumer?
- How to skip record which produces runtime exception in Kafka and keep stream running?
- How to solve -Cannot use import statement outside a module in AWS lambda console
- How to Solve 403 Error in Spring Boot Post Request
- How to solve ' CUDA out of memory. Tried to allocate xxx MiB' in pytorch?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.