File Operations

Work with files efficiently using Gemini CLI for reading, analyzing, processing, and automating file-related tasks. Whether you are reviewing a single configuration file or processing an entire codebase, Gemini CLI provides a flexible, command-line-native interface for AI-powered file operations.

Reading and Analyzing Files

The simplest way to give Gemini CLI access to a file is via standard input redirection (<). This streams the file contents directly to the model as context, letting you ask any question about the file without copying and pasting.

Read a Single File

Pipe any text file into Gemini CLI to summarize, explain, or critique its contents. This works with source code, configuration files, documentation, and plain-text data.

gemini "Summarize this file" < document.md

gemini "What does this code do?" < script.js

gemini "Find issues in this configuration" < config.json

gemini "Explain each section of this Dockerfile" < Dockerfile

Process Multiple Files

When you need to compare two files or apply the same operation across a set of files, pass them as positional arguments or use the batch subcommand with a glob pattern.

gemini "Compare these two files and list the differences" file1.js file2.js

gemini batch "*.py" "Add Google-style docstrings to every function"

gemini batch "src/**/*.ts" "Check for any TypeScript strict-mode violations"

File Processing Operations

Beyond reading, Gemini CLI can transform file contents and write the result directly to a new file using output redirection (>). This makes it straightforward to integrate AI transformations into shell scripts and CI pipelines.

Content Analysis

Use Gemini CLI to extract structured insights from unstructured files. This is especially useful for log triage, security audits, and quick code inventory tasks.

gemini "Extract key error patterns from this log" < app.log

gemini "List every exported function name in this file" < large-script.js

gemini "Find potential SQL injection vulnerabilities" < auth.py

gemini "Identify hardcoded credentials or secrets" < config.yaml

File Transformation

Redirect Gemini CLI output to a new file to perform format conversions, generate documentation, or produce derived artifacts without touching the source file.

gemini "Convert this JSON to YAML" < data.json > data.yaml

gemini "Generate a README from this code" < main.py > README.md

gemini "Translate all comments to English" < legacy.php > legacy_en.php

gemini "Rewrite this shell script using Python" < deploy.sh > deploy.py

File Organization and Management

Passing a directory path (such as .) lets Gemini CLI inspect your project layout and reason about the overall structure. This is helpful when onboarding to a new codebase or planning a refactor.

Project Analysis

Ask Gemini CLI to describe how a project is organized, identify anti-patterns in the folder structure, or suggest a migration path to a more standard layout.

gemini "Analyze the structure of this project" .

gemini "Suggest improvements to the file organization" .

gemini "Which files are most likely to be entry points?" .

Documentation Generation

Automatically generate module-level docs, API references, or usage examples from existing source files, saving hours of manual writing.

gemini "Create module-level documentation for this file" < module.py

gemini "Generate OpenAPI-style docs from this router" < api.js > api-docs.md

gemini "Write usage examples for every exported class" < lib.ts

File Safety Tips

AI-assisted file operations can modify your data in unexpected ways if used carelessly. Follow these practices to keep your files safe.

Always Redirect Output to a New File

Never overwrite your source file in a single command. Write AI output to a separate file first, review it, then replace the original if satisfied.

# Safe: write to a new file

gemini "Refactor this module" < module.py > module_refactored.py

# Risky: do not pipe back into the same file

gemini "Refactor this module" < module.py > module.py

Use Version Control Before Bulk Operations

Before running a gemini batch command that writes files, make sure your working tree is clean so you can easily diff or revert the changes.

git status # ensure a clean working tree

git stash # stash any in-progress changes

gemini batch "*.js" "Add JSDoc comments"

git diff # review what changed

Avoid Sending Sensitive Files to the API

Do not pipe files containing passwords, private keys, API tokens, or personally identifiable information to Gemini CLI. Strip or redact sensitive fields before analysis.

Working with Large Files

Every Gemini model has a finite context window. When a file exceeds the window, the CLI will either truncate the input or return an error. The following strategies help you work around this limitation.

Split Files with Standard Unix Tools

Use head, tail, or split to extract a manageable portion of a large file before sending it to Gemini CLI.

# Analyze just the first 500 lines of a large log

head -n 500 server.log | gemini "Summarize the errors in this excerpt"

# Inspect the most recent entries

tail -n 200 server.log | gemini "What happened in the last 200 lines?"

Process Files in Chunks with a Loop

For large datasets or lengthy source files, split the file into numbered parts and iterate over them, collecting results into a summary file.

# Split a large CSV into 1000-line chunks

split -l 1000 large_data.csv chunk_

for f in chunk_*; do

gemini "Extract anomalies from this data slice" < "$f" >> anomalies.txt

done

Focus on Relevant Sections

Instead of sending an entire file, use grep or sed to extract only the section relevant to your question. This keeps the prompt small and the response more focused.

# Extract only lines containing ERROR before analyzing

grep "ERROR" app.log | gemini "Categorize these errors by type"

# Extract a specific function from a large source file

sed -n '/^def authenticate/,/^def /p' auth.py | gemini "Review this function"

Common File Operation Use Cases

Log Analysis

Extract insights from application logs and error reports to quickly pinpoint root causes without manual scanning.

Code Review

Automated code quality assessment and improvement suggestions across single files or entire modules.

Configuration Validation

Check configuration files for syntax errors, missing required fields, and deviations from best practices.

Data Processing

Transform and analyze CSV, JSON, and YAML data files, converting formats or extracting summaries in a single command.

Frequently Asked Questions

Can Gemini CLI process binary files like images or PDFs?

Gemini CLI is primarily designed for text-based files such as source code, logs, JSON, YAML, Markdown, and plain text. For PDFs, you can first convert them to text using a tool like pdftotext and then pipe the output to Gemini CLI. Direct binary image analysis is not supported via the CLI file redirection syntax.

How do I prevent Gemini CLI from modifying my original files?

Use output redirection to write results to a new file rather than overwriting the source. For example: gemini "Refactor this code" < original.py > refactored.py. Always keep a backup or use version control before running transformation commands on important files.

What is the file size limit for Gemini CLI file operations?

Gemini CLI inherits the context window limit of the underlying Gemini model. For very large files, split them into smaller chunks and process each part individually — see the "Working with Large Files" section above for concrete examples. Files smaller than a few hundred kilobytes generally work without issues.

Next Steps

Explore more advanced file operations: