Setting the working directory keeps Codex anchored to the intended project, so relative paths, file discovery, and generated changes land in the right place instead of the directory the terminal happened to be in.
The -C option sets the working directory for a single codex exec run. Commands launched by Codex start from that directory, which changes what pwd reports, how ./relative/path resolves, and which repository context is detected for operations that rely on git metadata.
Using the wrong directory can cause edits in an unrelated checkout or a parent folder with multiple projects. Examples use POSIX-style paths (Linux, macOS, or WSL), so adjust the path format when running from a native Windows shell.
Steps to set Codex working directory:
- Confirm the target working directory path exists before passing it to -C.
$ ls -ld /home/user/projects/example-repo drwxr-xr-x 6 user user 192 Jan 19 11:30 /home/user/projects/example-repo
Prefer an absolute path in scripts to avoid surprises from relative paths.
- Run Codex with a fixed working directory.
$ codex exec -C /home/user/projects/example-repo "Print the working directory by running pwd." /home/user/projects/example-repo
Using the wrong directory can apply edits to the wrong project and is easy to miss when prompts mention only relative filenames.
- Confirm the shell working directory is unchanged after the run.
$ pwd /home/user
-C affects only the Codex run and does not change the current shell directory.
- Quote the -C path when it contains spaces.
$ codex exec -C "/home/user/Projects/Example App" "Print the working directory by running pwd." /home/user/Projects/Example App
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
