Automating File Cleanup With Scripts
Use an AI chat agent to write and validate bash scripts for bulk file operations like moving or deleting files based on content.
What it does
Use an AI chat agent to write and validate bash scripts for bulk file operations like moving or deleting files based on content.
What it does
The user leverages the chat agent to generate a bash script that identifies and moves XML files lacking specific text. After generating the code, they spot-check the results to ensure accuracy before executing the deletion process.
Example output
#!/bin/bash
# =============================================================================
# Script: quarantine_inactive_xml.sh
# Description: Scans /home/user/projects/data/xml_dump for XML files that do NOT
# contain the string "status=active" and moves them to a quarantine
# directory. Includes safety checks to prevent accidental deletion
# of non-XML files.
# =============================================================================
# Exit on error, undefined variables, or pipe failures
set -euo pipefail
# Define source and destination directories
SOURCE_DIR="/home/user/projects/data/xml_dump"
QUARANTINE_DIR="/tmp/quarantine_xml"
# Safety check: Ensure the source directory exists
if [ ! -d "$SOURCE_DIR" ]; then
echo "Error: Source directory '$SOURCE_DIR' does not exist." >&2
exit 1
fi
# Create the quarantine directory if it doesn't already exist
# Using mkdir -p to avoid errors if it already exists, and to create parent dirs if needed
if [ ! -d "$QUARANTINE_DIR" ]; then
echo "Creating quarantine directory: $QUARANTINE_DIR" >&2
mkdir -p "$QUARANTINE_DIR" || {
echo "Error: Failed to create quarantine directory '$QUARANTINE_DIR'." >&2
exit 1
}
fi
# Verify the source directory is not empty (optional but good practice)
if [ -z "$(ls -A "$SOURCE_DIR" 2>/dev/null)" ]; then
echo "Warning: Source directory '$SOURCE_DIR' is empty. Nothing to process." >&2
exit 0
fi
[... truncated, full run would continue]
## Example prompt
Generate a bash script that scans [directory path] for all XML files.
The script should identify files that do NOT contain the specific text string: [target string].
Move these identified files to [destination directory] instead of deleting them immediately.
Include comments explaining each step and add safety checks to prevent accidental deletion of other file types.
## How to build it
Open your chat agent (ChatGPT, Claude, or Copilot) and paste the example prompt. Adjust the inputs in the curly braces and run.The agent brief
Everything your agent needs, including the gotchas. Copy it and go.
You are helping me build the following AI agent workflow. ## Goal Automating File Cleanup With Scripts: Use an AI chat agent to write and validate bash scripts for bulk file operations like moving or deleting files based on content. ## Specification - What it does: Use an AI chat agent to write and validate bash scripts for bulk file operations like moving or deleting files based on content. - Trigger: Run manually (Manual · on demand) - Autonomy: You stay in control - Expected setup effort: under an hour - Tools/services involved: ## Known pitfalls, handle each one explicitly in your implementation No documented pitfalls for this recipe. Apply your own review before going live. ## Reference implementation https://news.ycombinator.com/item?id=42176800 (user_report) Fetch and inspect this before building. If it matches my stack, adapt it; if not, rebuild the pattern with my tools. ## Process requirements 1. Before building: ask me which of the listed tools I actually use and what my platform is (n8n / Make / code / other). Do not assume. 2. Adapt the pattern to my answers; do not force the reference stack. 3. Address every pitfall above; tell me how you handled each. 4. Provide a test plan I can run before letting this touch real data. 5. Ask before any step that sends messages, modifies data, or spends money. Source: https://usecasesforagents.com/use-case/automating-file-cleanup-with-scripts-fa8a/ via usecasesforagents.com
Want this running in your business?
This is what I do. I design and build AI agents like this one, and keep them running. If you want it set up for your team instead of doing it yourself, get in touch.
Who it's for
Built for anyone who wants this off their own plate, no team or company required.