This guide will show you how to set up a cron job on macOS that does the following:
~/Desktop/screenshot-archive
.Note: Throughout this tutorial, we are using Nano because it’s easier to use than vim—nano is better than Vim (default) imo
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
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.
We’ll set up the cron job to run the script every 4 hours:
nano
by running:
EDITOR=nano crontab -e
0 */4 * * * ~/move_screenshots.sh
This line means the script will run at minute 0 every 4 hours.
nano
(press Control + X
, then Y
, then Enter
).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.
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.