Tutorial: Clear Desktop Screenshots Every 24hrs

Why?

  1. my desktop, without this script, my desktop looks like this Messy Desktop
  2. all of my friends have desktops that look like that, too
  3. i have left my desktop in such a state for many years and have decided enough is enough

The Tutorial

This guide will show you how to set up a cron job on macOS that does the following:

Note: Throughout this tutorial, we are using Nano because it’s easier to use than vim—nano is better than Vim (default) imo

Step 1: Create the Shell Script

Open Terminal and create a new script file:

nano ~/move_screenshots.sh

Copy and paste the following script into the editor:

#!/bin/bash
# Source directory: Desktop (where screenshots are saved)
src="$HOME/Desktop"
# Base archive folder
archive_base="$HOME/Desktop/screenshot-archive"

# Ensure the base archive folder exists
mkdir -p "$archive_base"

# If today is the first of the month, create a new folder for last month's batch
if [ "$(date +%d)" == "01" ]; then
    # Get the previous month and year (macOS date syntax)
    prev_month=$(date -v-1d "+%B" | tr '[:upper:]' '[:lower:]')
    prev_year=$(date -v-1d "+%Y")
    dest="$archive_base/${prev_month} ${prev_year}"
else
    # Otherwise, use the base archive folder
    dest="$archive_base"
fi

# Create the destination folder if it doesn't exist
mkdir -p "$dest"

# Find and move screenshots older than 24 hours (1440 minutes)
# Assuming screenshots start with "Screenshot"
find "$src" -maxdepth 1 -type f -name "Screenshot*" -mmin +1440 -exec mv {} "$dest" \;

Save and exit nano by pressing Control + X, then Y to confirm, and finally Enter.

Make the script executable by running:

chmod +x ~/move_screenshots.sh

Step 2: Test the Script

Before setting up the cron job, run the script manually to ensure it works as expected:

~/move_screenshots.sh

Check your Desktop and the ~/Desktop/screenshot-archive folder (or the monthly folder if today is the first of the month) to verify that screenshots older than 24 hours have been moved.

Step 3: Set Up the Cron Job

We’ll set up the cron job to run the script every 4 hours:

  1. Open your crontab in nano by running:
    EDITOR=nano crontab -e
  2. Add the following line to schedule the job:
    0 */4 * * * ~/move_screenshots.sh
    This line means the script will run at minute 0 every 4 hours.
  3. Save and exit nano (press Control + X, then Y, then Enter).

Step 4: Verify the Cron Job

To confirm the cron job is set up, list your current cron jobs by running:

crontab -l

You should see the line you just added.

Conclusion

You have now successfully configured a cron job on macOS that automatically moves screenshots older than 24 hours from your Desktop to an archive folder, with monthly subfolders for the previous month's screenshots. If you have any questions or need further customization, feel free to leave a comment.