How to Change the Gemini CLI Working Directory

The Gemini CLI — Google's command-line interface for interacting with Gemini AI models — operates within a specific working directory context, just like any other terminal-based tool. Knowing how to change or configure that directory affects where the CLI reads files from, where it writes output, and how it resolves relative paths in your commands. If you've been getting unexpected file-not-found errors or outputs landing in the wrong folder, the working directory is usually the first place to investigate.

What the Working Directory Actually Controls

When you run the Gemini CLI, it inherits the current working directory (CWD) from your terminal session at the time of execution. This means:

  • File inputs (like passing a local document to a prompt) are resolved relative to that directory
  • Output files, if specified by path, are written relative to that same location
  • Config files or .env files the CLI looks for locally will be searched from that root

This is standard Unix/Windows shell behavior, not something unique to Gemini CLI. But because AI CLIs often deal with files as context — feeding documents into prompts, saving responses — the working directory matters more here than it might for a simple utility.

The Standard Way: Change Directory Before Running 🗂️

The most direct method is to change your terminal's working directory before invoking the CLI.

cd /path/to/your/project gemini ... 

Using cd (on macOS/Linux) or cd (on Windows Command Prompt and PowerShell) sets the current directory for your entire shell session. Any relative paths you pass to the Gemini CLI after this point will resolve from that location.

On Windows, the same logic applies:

cd C:UsersYourNameDocumentsMyProject gemini ... 

This approach requires no special CLI flags and works universally. It's the recommended starting point for most users.

Passing an Explicit Path as Part of the Command

If you don't want to change your shell's working directory globally, many CLI tools — including wrappers built on top of the Gemini API — accept explicit file paths as arguments. Instead of relying on a relative path, you supply the full absolute path:

gemini --input /home/user/documents/report.pdf 

This sidesteps the working directory issue entirely for that specific command. The tradeoff is that commands become more verbose, especially if you're referencing multiple files or running the CLI repeatedly against the same folder.

Using Shell Subshells for Isolated Directory Context

A cleaner technique for scripting or automation is to run the Gemini CLI inside a subshell with a targeted directory change:

(cd /path/to/target && gemini ...) 

The parentheses create a subshell. The cd inside only affects that subshell — your main terminal session's directory stays unchanged. This is especially useful in shell scripts where you need to run multiple tools from different directories without losing your place.

On PowerShell (Windows), the equivalent pattern uses Push-Location and Pop-Location:

Push-Location "C:ProjectsMyFolder" gemini ... Pop-Location 

Configuration Files and Where the CLI Looks for Them

Some Gemini CLI setups rely on a local configuration file (such as .gemini config, .env for API keys, or a gemini.config.json). These are often searched for starting from the current working directory and moving up the directory tree — a pattern called directory traversal lookup.

If your config file isn't being picked up, it's often because the working directory isn't anchored at the project root where the config lives. Changing to the correct directory before running the CLI resolves this.

Key config-related factors that vary by setup:

FactorImpact on Directory Behavior
Global install vs. local installLocal installs may expect CWD at project root
.env file locationMust match CWD or a parent directory
API key sourcing methodEnvironment variable vs. local file changes lookup behavior
OS (macOS/Linux vs. Windows)Path separators and home directory shortcuts differ

Environment Variables as an Alternative 🔧

Some CLI tools expose an environment variable that explicitly sets the working directory or base path — bypassing whatever directory the shell is currently in. Check the documentation for the specific Gemini CLI build you're using (Google's official CLI, a third-party wrapper, or a custom integration) to see if such a variable is available.

Common patterns include:

  • GEMINI_WORKING_DIR=/path/to/folder
  • CLI_BASE_PATH=/path/to/folder

Setting these in your shell's profile file (.bashrc, .zshrc, or Windows environment variables) makes the configuration persistent across sessions.

The Variables That Determine Your Best Approach

How you should handle working directory changes depends on several factors specific to your environment:

  • Operating system — macOS, Linux, and Windows handle paths, separators, and shell behavior differently
  • Shell type — Bash, Zsh, Fish, PowerShell, and CMD each have nuances in how subshells and directory changes work
  • How the CLI was installed — npm global package, Python pip install, standalone binary, and Docker container setups each have different file resolution behaviors
  • Whether you're running interactively or scripting — a one-off manual command versus an automated pipeline calls for different approaches
  • Where your config and input files live relative to each other — a tightly organized project folder is very different from working across scattered directories

A developer running Gemini CLI as part of a build pipeline in a Docker container has a fundamentally different directory management problem than someone using it interactively from their desktop terminal. The underlying mechanism — controlling where the CLI considers "home base" for the current session — is the same, but which method fits cleanly into the workflow depends entirely on that context.