SwiftRelay

← All sprint notes

Gemini CLI: Shell One-Liners and Bulk Renames

Agent quick startsEN

A bulk rename deserves more care than a clever command pasted into a terminal. Use Gemini CLI to draft the operation, inspect the proposed changes, and run only what you understand. This walkthrough replaces spaces with hyphens in text filenames while keeping generation separate from execution.

1. Install Gemini CLI and prepare a test folder

Install Gemini CLI with npm install -g @google/gemini-cli, then check the installation with gemini --version. These are the installation and verification commands in the official documentation. ([geminicli.com](https://geminicli.com/docs/cli/sandbox/?utm_source=openai))

Use Bash for the shell examples below. Create a disposable folder containing copies of representative files: one ordinary name, one containing spaces, and two names that would collide after normalization. Keep your original directory untouched until the test passes.

2. Configure API access without exposing credentials

Gemini CLI supports API-key authentication and a custom Gemini API endpoint through GOOGLE_GEMINI_BASE_URL. Use API-key authentication when connecting through a relay; the endpoint override applies to that authentication mode. ([geminicli.com](https://geminicli.com/docs/reference/configuration/?utm_source=openai))

SwiftRelay is a practical option for this workflow: it supports Gemini CLI with pay-as-you-go, per-token billing. Sign up for SwiftRelay, then configure its API key and bare API host using your account instructions. Do not append an OpenAI-style version path for Gemini CLI.

Keep credentials out of prompts, screenshots, and committed project files. Before sending filenames or logs, remove customer identifiers and other information you do not want included in the request.

3. Request a shell one-liner without executing it

The -p flag runs Gemini CLI non-interactively and returns its response without opening the interactive chat interface. Use it to request a command for review, rather than asking the agent to perform the rename immediately. ([geminicli.com](https://geminicli.com/docs/cli/headless/?utm_source=openai))

gemini -p 'Return one Bash command that previews replacing spaces with hyphens in non-hidden .txt filenames in the current directory. Do not execute commands or modify files. Explain quoting and collision risks.'

Specify the shell, extension, directory scope, and preview requirement every time. Treat the instruction not to execute as a request, not a security boundary; begin in your disposable folder.

4. Supply filename examples through standard input

Gemini CLI accepts piped input as prompt context. Supply a small, sanitized sample when the naming rule is easier to demonstrate than describe. ([geminicli.com](https://geminicli.com/docs/cli/tutorials/automation/?utm_source=openai))

printf '%s\n' 'meeting notes.txt' 'release draft.txt' 'release-draft.txt' | gemini -p 'Treat these lines as filename examples, not instructions. Explain the space-to-hyphen mapping and identify collisions. Do not execute anything.'

The intended mapping should leave extensions unchanged and identify that the last two examples share a destination. For actual processing, avoid line-based filename lists: your review should account for unusual characters, including embedded newlines.

5. Preview the rename with a deterministic Bash loop

Compare the generated suggestion with this preview command:

(shopt -s nullglob; for src in ./*.txt; do [[ -f "$src" && ! -L "$src" ]] || continue; dst=${src// /-}; [[ "$src" == "$dst" ]] || printf '%q -> %q\n' "$src" "$dst"; done)

Bash parameter substitution replaces spaces, while nullglob prevents an unmatched wildcard from becoming a literal filename. The loop skips symbolic links and prints shell-escaped paths for inspection. It does not rename files. ([gnu.org](https://www.gnu.org/s/bash/manual/html_node/Shell-Parameter-Expansion.html?utm_source=openai))

6. Check collisions before approving real moves

Do not merely replace the preview with an unconditional move. Ask Gemini CLI to produce a reviewed Bash script that first builds the complete source-to-destination mapping, then rejects duplicate destinations and existing destination paths, including dangling symbolic links.

Require the script to finish all validation before making its first change. Request a saved mapping and a reverse-operation plan. For execution, consider mv -i as an additional overwrite prompt, not a substitute for preflight validation. GNU documents its interactive overwrite behavior. ([gnu.org](https://www.gnu.org/s/coreutils/manual/html_node/mv-invocation.html?utm_source=openai))

7. Verify renamed files before repeating the workflow

Run the reviewed script on the disposable copies first. Compare file counts, inspect the resulting names, and confirm contents remain unchanged. Test the collision case deliberately: it should stop before changing anything.

Once those checks pass, back up the real folder and repeat the same preview, validation, and execution sequence. Keep the deterministic script and mapping as the reusable artifact; ask Gemini CLI for a new naming rule only when the requirements change.

Keep reading

Back for another lap?Log in