I have a camera setup up to take an image every few seconds and then the image is processed and is no longer required. Can anyone help me to write a script/service that will delete files/images in a folder that are older than 3 hours? I'm trying to do this as it takes a ton of space if only purged every 24 hours using Automator.


2 Answers 2


Here is an example find command that you can modify to suit your needs:

find '/path/to/files/' -type f -name '*.jpg' -mmin +180 -delete

I'd use launchd to run it every 3 hours.

Here is an example .plist file that will run the find command every 3 hours using launchd:

<?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">

It would be saved in: $HOME/Library/LaunchAgents/com.my.delete.every.three.hours.plist

From Terminal:

touch "$HOME/Library/LaunchAgents/com.my.delete.every.three.hours.plist"
open -e "$HOME/Library/LaunchAgents/com.my.delete.every.three.hours.plist"
  • Copy and paste example XML code into the opened TextEdit document:
  • Modify as needed:
    • E.g. Change /path/to/files/ to the actual path.
    • E.g. Change *.jpg to the proper extension if not: jpg
  • Save and close the document.

Continue in Terminal, to load the plist file:

launchctl load "$HOME/Library/LaunchAgents/com.my.delete.every.three.hours.plist"

To stop and unload the "$HOME/Library/LaunchAgents/com.my.delete.every.three.hours.plist" file use:

launchctl stop "$HOME/Library/LaunchAgents/com.my.delete.every.three.hours.plist"
launchctl unload "$HOME/Library/LaunchAgents/com.my.delete.every.three.hours.plist"
  • Holy smoke, it’d be shorter in Perl... Jul 2, 2020 at 6:34
  • @Harper-ReinstateMonica The actual code is the first snippet only, the rest is to have it run automatically by launchd.
    – nohillside
    Jul 2, 2020 at 15:01

You can write a bash script that is executed by a cron job to run at any frequency that you need (e.g., every minute, every hour, etc.). You'll need to build the bash script - some of the things you probably need are:

  1. Find old files
  2. Delete files returned from find command
  • Note that cron is deprecated in macOS and has been since the release of Mac OS X 10.4; launchd is the preferred method (and eventually the only method) for running this sort of script.
    – da4
    Jul 2, 2020 at 16:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .