Log in·Sign up
BestRelay

Back to the Journal

Using Codex CLI for Everyday Shell Chores

Start with one shell chore and clear boundaries

Shell chores often begin as a command you run occasionally, then grow into scripts nobody quite trusts. Codex CLI can help turn those commands into readable automation. The useful pattern is straightforward: describe the behavior, let Codex inspect the workspace, review its changes, and test the result against disposable files.

This walkthrough builds a Bash script that removes old log files from a chosen directory. It should preview its work by default, delete only when explicitly requested, and handle filenames containing spaces. Those requirements give you concrete behavior to check instead of accepting whatever script appears first.

Install Codex CLI and prepare a workspace

With Node.js and npm available, install the CLI using npm install -g @openai/codex. Then create a small working directory with mkdir -p shell-chores, enter it with cd shell-chores, and start the interactive session with codex. Complete the authentication flow appropriate to your setup.

Keep this first exercise away from production directories. Codex can inspect files, propose edits, and run commands within the permissions configured for the session. Read execution requests carefully, especially commands that delete files or access directories outside the workspace.

BestRelay, which publishes this walkthrough, supports Codex alongside Claude Code, Gemini CLI, and OpenAI SDK clients. Its API base is https://api.bestapirelay.com. If you use it, follow its current Codex configuration instructions; a compatible endpoint still requires the correct provider configuration and credentials. Create a BestRelay account to try that setup.

Give Codex a precise cleanup script specification

Ask Codex: “Create clean-logs.sh using Bash. Accept a directory and a positive integer age in days. Recursively select regular files ending in .log whose modification age exceeds that threshold. Preview matching paths by default. Delete only when --delete is supplied. Reject missing arguments, invalid ages, and nonexistent directories. Explain the time comparison you use.”

Add two practical constraints: “Handle spaces and newlines in filenames safely, and do not follow symbolic links. Use no network access or elevated privileges.” These instructions make important assumptions visible before implementation begins.

Ask for a short usage message and explicit error handling. Avoid prescribing a particular shell option as a substitute for checking failures; commands used in conditionals and pipelines can have surprising behavior.

Review file selection before allowing any deletion

Read the proposed script before running it. Check that directory arguments are quoted, option parsing rejects unknown flags, and file selection stays inside the requested directory. Ask Codex to explain how it handles a directory name beginning with a dash.

Filename handling deserves special attention. A loop over ordinary command substitution can split paths on whitespace. A null-delimited find operation paired with a compatible reader is a safer approach. Preview output should also distinguish unusual filenames clearly.

Check age semantics too. The find -mtime predicate counts complete 24-hour periods, so its behavior may differ from an exact timestamp cutoff. Ask Codex to document the chosen interpretation and identify whether any commands require GNU utilities rather than the versions supplied with macOS.

Test the script against disposable example files

Ask Codex to create a temporary fixture directory containing recent logs, old logs, a filename with spaces, a nested directory, and a non-log file. Have it set representative modification times and show the expected matches before execution.

  • Run preview mode and verify that every fixture remains present.
  • Run deletion mode against the same fixtures and verify that only eligible logs disappear.
  • Try invalid ages, missing directories, and unknown flags; each should produce a useful error and a nonzero exit status.

Run bash -n clean-logs.sh for syntax checking. If ShellCheck is installed, ask Codex to run it and explain relevant findings. Neither check replaces verifying which files the script actually removes.

Make the verified script easy to reuse

Ask Codex to add concise usage examples and document the tested operating system. Keep preview mode as the default, and try the script manually on a real directory before scheduling deletion.

For API access, BestRelay forwards each request to the official model named in it without substitution. Its pay-as-you-go billing uses provider-reported token counts, with cache reads and writes priced separately. Members also earn referral commission when invitees top up. Keep those billing details separate from your script’s behavior: review command permissions and verify filesystem effects regardless of how you access Codex.

Want to try the models discussed here? Sign up