Blog Init

Automating Directory Structure Backup

Problem:

Solution

Step 1:

  1. First, create a shell script that will generate the directory structure file with the current date.

  2. 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"
  1. Make the script executable
cd scripts && chmod +x update_directory_structure.sh

Step 2: Set Up the Cron Job

  1. Open your crontab file: EDITOR=nano crontab -e

  2. 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

  1. 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:

  1. 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>
  1. Load the agent:
launchctl load ~/Library/LaunchAgents/com.user.dirstructure.plist

Verification

To verify it's working:

  1. Wait for the next hour to pass
  2. Check that a new file is created in your directory-structure folder
  3. 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

  1. Check cron logs (most reliable method) grep CRON /var/log/syslog

  2. on macOS: grep cron /var/log/system.log

Deleting a Cron

Method 1: Remove the Cron Job via crontab

  1. Open your crontab for editing (using nano): EDITOR=nano crontab -e
  2. Find the line that contains:
/bin/bash /Users/anmoltomer/Desktop/side-projects/rent-tracker-wizard/scripts/update_directory_structure.sh
  1. Delete the entire line (in nano, move cursor to the line and press Ctrl+K to cut it).

  2. Save & exit (Ctrl+O → Enter → Ctrl+X).

  3. Verify it’s gone: crontab -l (Should no longer show the job.)

Method 2: Completely Clear All Cron Jobs (Nuclear Option) 🔴

Alternative:

launchctl unload ~/Library/LaunchAgents/local.dirstructure.plist
rm ~/Library/LaunchAgents/local.dirstructure.plist

Why this works