10.4: Use Automator to remove older files from a folder

Aug 23, '06 07:30:02AM

Contributed by: robg

The other day, a Macworld coworker asked me for some help with an Automator workflow. He uses a temporary folder to store each day's files, and has been archiving them into another folder at the conclusion of each day. His Automator action was designed to automate that process, and it was working well. Except for the last step: he wanted a way to remove any files that hadn't been modified in 90 days, leaving him a perpetual three-month archive of each day's files.

Automator has a few search actions to help find files -- you can use the Finder -> Find Finder Items action, or the Spotlight -> Spotlight action. Unfortunately, neither of these have the ability to find files that haven't been modified within a certain timeframe. The Finder action has some date options, but they're quite limited in scope. Unix, however, has a powerful find command that can do pretty much anything, including finding files who haven't been modified within a given period.

We've run some hints on doing just that in the past, and today's hint is a simple variation using Automator. To clean up a specified folder based on the modification date of each item in the folder, you just need to add the Run Shell Script action (from the Automator library) to your workflow. Click the Text pop-up at the top of the dialog and change the setting to 'Ignore Results of Prior Action' -- this script runs on its own, and doesn't use (nor want) input from anything else in your script. Set the Shell pop-up to /bin/bash, and the Pass input pop-up to 'to stdin,' and then enter this command:

find /path/to/folder/* -type f -mtime +90 -exec rm -f {} \;
You'll have to make sure the path/to/folder bit is coded properly for any spaces in the path to the folder. You do this by using a backslash before any space, as in this example:
/Users/joesmith/cool\ stuff/work\ projects
The bits after the * are where the hard work takes place; read on for my (non-Unix-wizard) explanation of what's going on (and I welcome improvements to the command). If you're not going to read on, the +90 is the other important bit; that's the time interval in days (technically, "24 hour periods") outside of which you'd like to delete files.

Warning: This action will delete files in the specified folder. I strongly recommend testing it thoroughly on a sample folder before using it on a production folder! Really. Files will be deleted. Consider yourself warned. Have you backed up lately?

So how does this command work? I'll give you my best breakdown of the command, with the understanding that everything I know about Unix, I've learned from running this site for five years :)...

So there you have it; an easy-to-add Automator action to help clear out older files from a specified directory. I'm sure there are better versions of the command than this, so please feel free to comment...

Comments (10)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20060823061933958