Setting the working directory for Codex at launch keeps a task pointed at the intended project or scratch folder without first moving the parent shell, which is useful when jumping between repositories, comparing branches, or calling Codex from scripts.
The -C and --cd flags tell Codex which directory to treat as its working root. That working root is where interactive sessions read project files and where codex exec resolves relative paths, project instructions, and Git context for the run.
Current Codex CLI help exposes -C, --cd <DIR> as the working-root switch for both interactive and non-interactive use. For codex exec, a Git repository under the chosen directory keeps the repository check enabled, while scratch directories need --skip-git-repo-check; --add-dir only adds extra writable paths and does not change the working root.
Related: How to start a Codex interactive session
Related: How to run Codex exec with a command
Steps to set the working directory for Codex:
- Identify the directory that Codex should use as its working root.
$ pwd /home/user/work/example-repo
If the path is already known, use it directly. An absolute path makes the launch target unambiguous.
- Start the interactive Codex session with that directory as the working root.
$ codex -C /home/user/work/example-repo
-C and --cd are equivalent. When the interface opens, the directory line should show the selected path even if the current shell stayed elsewhere.
- Verify the same working-root change in non-interactive mode with codex exec.
$ codex -a never exec -C /home/user/work/example-repo --sandbox read-only "Reply with the absolute working directory only, with no explanation." /home/user/work/example-repo
The returned path confirms that Codex resolved the run inside the chosen working root. --sandbox read-only keeps the check non-destructive.
Related: How to run Codex exec with a command
- Add --skip-git-repo-check only when the chosen working directory is not inside a Git repository.
$ codex -a never exec -C /home/user/tmp/codex-scratch --skip-git-repo-check --sandbox read-only "Reply with the absolute working directory only, with no explanation." /home/user/tmp/codex-scratch
codex exec stops with a trusted-directory error when the working root is not a repository and the skip flag is omitted. Use the override only for intentional scratch directories.
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.
