Skip to content

Instantly share code, notes, and snippets.

@barretts
Created July 28, 2025 15:51
Show Gist options
  • Save barretts/8218b3b108176e5f50a986d0e6e773a9 to your computer and use it in GitHub Desktop.
Save barretts/8218b3b108176e5f50a986d0e6e773a9 to your computer and use it in GitHub Desktop.
Run Claude Code /init in subdirectories
#!/usr/bin/expect -f
# You can comment this out now that we've found the problem
# exp_internal 1
# --- Configuration ---
set command "/Users/bsonntag/.nvm/versions/node/v24.0.2/bin/claude"
set args "/init"
# CHANGE THIS LINE: Add a wildcard * to jump over the formatting codes
set watch_phrase "esc*to interrupt"
set initial_timeout 30
set monitor_timeout 1
# --- End Configuration ---
spawn $command $args
send_user "🚀 Starting '$command $args'.\n"
# --- STAGE 1: Wait for the phrase to APPEAR ---
send_user "Stage 1: Waiting for '$watch_phrase' to appear (max $initial_timeout seconds)...\n"
expect {
timeout {
send_user "\n❌ ERROR: Timed out. The phrase '$watch_phrase' never appeared.\n"
exit 1
}
# CHANGE: Using -gl (glob match) with wildcards (*) to ignore formatting.
-gl "*$watch_phrase*" {
send_user "✅ Phrase found. Proceeding to Stage 2.\n"
}
eof {
send_user "\nℹ️ INFO: Claude process exited before monitoring could start.\n"
exit 0
}
}
# --- STAGE 2: Monitor for the phrase to DISAPPEAR ---
send_user "Stage 2: Monitoring for '$watch_phrase' to disappear...\n"
set timeout $monitor_timeout
expect {
# CHANGE: Using -gl (glob match) with wildcards (*) as well.
-gl "*$watch_phrase*" {
exp_continue
}
timeout {
send_user "\n✅ '$watch_phrase' disappeared. Sending Ctrl+C twice.\n"
send "\x03"
send "\x03"
exit 0
}
eof {
send_user "\nℹ️ INFO: Claude process finished on its own during monitoring.\n"
exit 0
}
}
#!/bin/bash
# --- Configuration ---
# The starting directory where the script is run.
START_DIR=$(pwd)
# Define what "not short" means. Skip if CLAUDE.md is bigger than this many bytes.
MIN_SIZE=10
# --- End Configuration ---
# Find all directories in the current folder
for dir in */; do
# Remove trailing slash from directory name
dir=${dir%/}
echo "====================================="
echo "Inspecting directory: $dir"
# --- New Check ---
# Check if CLAUDE.md exists and its size is greater than MIN_SIZE.
if [ -f "$dir/CLAUDE.md" ] && [ $(wc -c < "$dir/CLAUDE.md") -gt $MIN_SIZE ]; then
echo "⏩ Skipping '$dir' because a non-empty CLAUDE.md already exists."
echo "====================================="
echo ""
continue # Immediately skip to the next directory in the loop
fi
# --- End of Check ---
echo "✅ Processing directory: $dir"
# Navigate to the target directory
cd "$START_DIR/$dir"
# Call the expect script to run the CLAUDE process.
../monitor_claude.exp
echo "Finished processing $dir."
echo "====================================="
echo ""
# Return to the starting directory for the next loop
cd "$START_DIR"
done
echo "✅ All directories have been processed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment