Using Automator to delete files

closeThis post was published 4 years 6 months 14 days ago. Therefore, it may well be out of date. Do not reply on the contents of this post being accurate.

I use Automator to perform various backup functions on my Macs. I backup different data at different schedules depending on how often the source data is likely to change. For stuff that gets backed up regularly there’s the problem of accumulating too many ‘old’ backup files.

I found a script at macosxhints.com that solves this problem by cleaning up a specified folder based on the modification date of each item in the folder. By using the Run Shell Script function within Automator and saving it as an iCal plug-in the whole process is automated.

See the original article for more info. Warning: This action will delete files in the specified folder.

find /path/to/folder/* -type f -mtime +28 -exec rm -f {} \;

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 +28mtime is the modification time of the file, and +28 is the number of days to check. The + sign is important, as it tells find to look at files that are “more than” 28 days old. So yes, technically, that should be +27 to find those 28-day-old files. If you leave off the plus sign, you’ll find only those files that are exactly 28 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.

This entry was posted in Applications. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>