Automating Directory Structure Backup
Problem:
- When vibe-coding full-stack applications, a good hack I have found is giving the directory structure of the entire project (minus the node_modules and lib folders of python etc.) it gives the LLM tool a better understanding of the project and quality of responses also improve.
- Now instead of running this every time I want directory_structure to be updated every hour or to run at a specified duration automatically.
- To achieve this, I setup a cron here, that runs the tree command and creates a new file inside directory-structure folder in the root of the project.
Solution
Step 1:
First, create a shell script that will generate the directory structure file with the current date.
Then set up a cron job to run this script hourly.
#!/bin/bash
# Set your project directory
PROJECT_DIR="/Users/anmoltomer/Desktop/side-projects/rent-tracker-wizard"
OUTPUT_DIR="$PROJECT_DIR/directory-structure"
# Create directory if it doesn't exist
mkdir -p "$OUTPUT_DIR"
# Generate filename with current date
FILENAME="project_directory_structure_$(date +'%Y-%m-%d').txt"
# Generate the directory structure
cd "$PROJECT_DIR" && tree -L 5 --prune -I 'node_modules|venv|.expo|__pycache__|backend/app/lib|*.pyc' > "$OUTPUT_DIR/$FILENAME"
# Optional: Add a timestamp to the file
echo -e "\n\nLast updated: $(date)" >> "$OUTPUT_DIR/$FILENAME"
- Make the script executable
cd scripts && chmod +x update_directory_structure.sh
Step 2: Set Up the Cron Job
Open your crontab file:
EDITOR=nano crontab -e
Add this line to run the script hourly (at the top of the hour):
0 * * * * /bin/bash /Users/anmoltomer/Desktop/side-projects/rent-tracker-wizard/scripts/update_directory_structure.sh
- If you want to run it at a different minute (e.g., 15 minutes past the hour), change the first number:
15 * * * * /bin/bash /Users/anmoltomer/Desktop/side-projects/rent-tracker-wizard/scripts/update_directory_structure.sh
Alternative: Use a Launch Agent (macOS specific)
If you're on macOS, you might prefer to use a Launch Agent:
- Create a plist file at
~/Library/LaunchAgents/com.user.dirstructure.plist
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.dirstructure</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/path/to/update_directory_structure.sh</string>
</array>
<key>StartInterval</key>
<integer>3600</integer> <!-- Run every 3600 seconds (1 hour) -->
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
- Load the agent:
launchctl load ~/Library/LaunchAgents/com.user.dirstructure.plist
Verification
To verify it's working:
- Wait for the next hour to pass
- Check that a new file is created in your directory-structure folder
- The next day, verify that a new file with the new date is created
This setup will automatically create a new file each day (named with that day's date) and update it hourly while you're working on that day.
Debugging
- To verify if the cron ran properly
Check cron logs (most reliable method)
grep CRON /var/log/syslog
on macOS:
grep cron /var/log/system.log
Check if your output file was updated:
ls -l /Users/anmoltomer/Desktop/side-projects/rent-tracker-wizard/directory-structure/
(Check the modification timestamp of your file)Check mail for cron errors (cron sends output to mail):
mail
-> Press 1 to see the details of cron run.
Deleting a Cron
Method 1: Remove the Cron Job via crontab
- Open your crontab for editing (using nano):
EDITOR=nano crontab -e
- Find the line that contains:
/bin/bash /Users/anmoltomer/Desktop/side-projects/rent-tracker-wizard/scripts/update_directory_structure.sh
Delete the entire line (in
nano
, move cursor to the line and pressCtrl+K
to cut it).Save & exit (
Ctrl+O
→Enter
→Ctrl+X
).Verify it’s gone:
crontab -l
(Should no longer show the job.)
Method 2: Completely Clear All Cron Jobs (Nuclear Option) 🔴
- If you want to remove ALL cron jobs for your user:
crontab -r
Alternative:
- If You Used launchd (Alternative to Cron)
- If you previously set up a launchd job on your mac (.plist file), remove it with:
launchctl unload ~/Library/LaunchAgents/local.dirstructure.plist
rm ~/Library/LaunchAgents/local.dirstructure.plist
Why this works
crontab -e
edits your user’s personal cron jobs.Deleting the line stops the script from running hourly.
No leftover processes or files remain (unlike launchd).