
Aug 23, '06 07:30:02AM • Contributed by: robg

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 :)...
- find /path/to/folder/* -- this bit launches the find command, and provides the directory (folder, in this example) and files (*, or all files) to act on.
- -type f -- this action should work only on files, not directories or symbolic links. Files are what we want to remove, not anything else.
- -mtime +90 -- mtime is the modification time of the file, and +90 is the number of days to check. The + sign is important, as it tells find to look at files that are "more than" 90 days old. So yes, technically, that should be +89 to find those 90-day-old files. If you leave off the plus sign, you'll find only those files that are exactly 90 days old.
- -exec rm -f {} \; -- this bit runs the rm command, which actually deletes the files. The -f flag forces the removal of a locked file, the curly braces pass in the results of the find (the path to the file, in this case), and the \; bit ends the exec command. Note that find has a -delete option, but I've always used the exec solution. I'm not sure why; someone must have told me once that it was a Good Thing to do.