31 Days of OS X Tips: Clean Up Your Files Automatically Using Automator and A Shell Script

Since we’re getting to the end of the month, we figured it was time to put out some more advanced Mac tips: We’re going to use Automator to schedule a shell script to run once a week and clean up our Mac’s Trash and Downloads folder.

Sound scary? It isn’t; it’s just a bit time consuming.

This script does two things: First, it empties the Trash on a weekly basis—something even seasoned Mac users forget to do regularly. Second, it clears out the files in your Downloads folder and archives them in a separate folder, just in case you need them. Come back for Part 2 tomorrow and we’ll show you how to create a script that periodically clears out the download archives that this script creates.

This tutorial assumes you’re somewhat familiar with Automator and the concept of the OS X command line (basically, that you know it exists).

Before you begin, go to your user account’s home folder and create a new folder (formally called a “directory”), and name it OldDownloads. If you can’t find your user folder—OS X hides it away by default on Yosemite—open the Finder’s Go menu, select Go to folder…, type ~/, and press Go.

  1. Open Automator, and select Calendar Alarm from the new document picker, then press Choose.
  2. Click on Utilities in the left hand panel.
  3. Select Run Shell Script, and double-click it to add it to your Automator workflow.
  4. In the panel that comes up, enter in the following code:

    #!/bin/bash
    thisUser="$(whoami)"
    echo "Creating Backup of Downloads for $thisUser"
    zip /users/$thisUser/OldDownloads/Old-Downloads-$(date +"%m-%d-%y").zip /users/$thisUser/Downloads/*
    echo "Deleting the following folders for $thisUser"
    echo "Downloads"
    echo "Trash"
    rm -rdf ~/.Trash/*
    rm -rdf /users/$thisUser/Downloads/*
  5. Click File -> Save and name your event.
  6. Calendar will open and it will create a new event for the script at the current time. Edit the Calendar event and set the appropriate repeat frequency, and you’re all set to have your files cleaned up automatically every week.

If you’re looking for extra punches on your nerd card, you can do this with cron jobs—a standard method for scheduling tasks from your Mac’s command line. MacLife has a quick overview on how those work.

Mac geek? Gamer? Why not both? Mike is a writer from Wisconsin who enjoys wasting immense amounts of time on the Internet. You can follow him on Twitter.