Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4: Use Automator to remove older files from a folder Apps
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 :)...
  • 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.
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...
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[24,671 views]  

10.4: Use Automator to remove older files from a folder | 10 comments | Create New Account
Click here to return to the '10.4: Use Automator to remove older files from a folder' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Use Automator to remove older files from a folder
Authored by: bhcohen on Aug 23, '06 08:19:15AM
There's a preference pane called "Hazel" from

http://www.noodlesoft.com/

that lets you specify rules for folders like: move all files that haven't been touched in 90 days. There are a bunch of other things you can do as well like set labels, add keywords etc..

Its still in beta but looks pretty nice.

-Bruce

[ Reply to This | # ]
Why does no-one use zsh???
Authored by: gidds on Aug 23, '06 03:21:31PM
It's free and open source, it's supplied as part of Mac OS X, it does pretty much all that ksh and bash do (in fact it can pretend to be ksh), and it has really cool features like recursive file completion which can turn that rather unwieldy
find /path/to/folder/* -type f -mtime +90 -exec rm -f {} ;
command into the rather simpler
rm -f /path/to/folder/**/*(.m+90)
command. (The ** means to search subdirectories recursively, the . includes only plain files, and the m+90 includes only files with a modification time more than 90 days before now.)

It can do far more than that, of course, but this is a rather good example. (I should point out that I didn't have to look anything up; the common criteria are fairly easy to remember, and when I tried that command just now it worked first time.)

Recursive file completion (the ** bit) seems like such a no-brainer that I'm still amazed the more common shells haven't copied it yet. I don't think I've used find once since I found out about it, as it can do all of that and more -- and because it's right there in the shell, you can use it in really powerful ways.

You can run zsh by name at any command prompt, and you can set it as your default shell in NetInfo Manager or (if it still works) the chsh command.

---
Andy/

[ Reply to This | # ]

Why does no-one use zsh???
Authored by: S Barman on Aug 23, '06 08:06:36PM
The recursive directories do work in bash and csh, but not the modifier (.m+90). That appears to be a zsh only construct. But you can use it in an automator by entering the command as:
zsh -c 'rm -f /path/to/folder/**/*(.m+90)'
Note that the quotes are necessary to prevent the current shell from trying to interpret the path.

[ Reply to This | # ]
Why does no-one use zsh???
Authored by: dmetzcher on Aug 25, '06 01:53:25PM

Excellent!
Here is a script that can be used to remove older the contents of a folder.
This really helped me a lot, as I was just trying to figure this out the other day.
=======================
-- Get the path to the home folder of the current user.
set homeFolderPath to (path to home folder) as string
-- Set myPath to the home folder, and add the folder within the home folder to the end.
set myPath to homeFolderPath & "Backups:Firefox"
-- Convert the full path to the required folder to the POSIX path so that it can be used in a shell script.
set posixPath to POSIX path of myPath -- convert the path of the file to a POSIX path (uses slashes (/) as directory separators. e.g., 'macintosh hd/users/johndoe/desktop'

do shell script "zsh -c 'rm -f " & quoted form of posixPath & "/**/*(.m+2)'"
=======================



[ Reply to This | # ]
10.4: Use Automator to remove older files from a folder
Authored by: johnemac on Aug 24, '06 05:45:40AM

Is there a way to run this as a shell script on older machines? I would like to use something like this on a G4 running 10.3.4. I don't think 10.3 includes Automator.



[ Reply to This | # ]
10.4: Use Automator to remove older files from a folder
Authored by: pfrown on Aug 28, '06 04:49:45PM
I've posted a shell script in the forums that basically wraps the "find" command and does the same thing as above (plus provides you a log file of deleted files). Here's the link: http://forums.macosxhints.com/showthread.php?t=25953

As stated above (and in the forum) - this is potentially a very destructive command to play with - be careful.

Trystan

[ Reply to This | # ]
10.4: Use Automator to remove older files from a folder
Authored by: JohnDCCIU on Sep 09, '06 10:34:48AM
Another tool that has interesting options and uses cron to do the scheduled tasks is TrashLater X.

[ Reply to This | # ]
10.4: Use Automator to remove older files from a folder
Authored by: barrymiami on Sep 03, '07 08:30:47PM
Does anyone know if this can be used to delete older files from a REMOTE ftp server?

I tried:

find ftp://username:password@ftp.server.com/path/to/folder/* -type f -mtime +30 -exec rm -f {} \;

in Automator but it failed. I guess I could run a cron job to delete files older than say, 30 days, but it would be nice to have it integrated into my Automator app.

[ Reply to This | # ]
10.4: Use Automator to remove older files from a folder
Authored by: DrDoak on Oct 11, '07 09:59:04AM

I also love zsh, but there's an EVEN BETTER way to do this. The original poster warned about the permanent deletion of files using this method. Well, instead we can simplify the bash command down to:

find /path/to/folder/* -type f -mtime +90

AND THEN follow that action up with a "Move to Trash" action (under the Finder Library). It will successfully read the filenames which are returned by the shell script action, and move them all to Trash to be emptied at the user's discretion. Ah, the beauty of AppleScript hooks!

(BTW, if you're looking for directories rather than files, use '-type d' ... to search for both I THINK that this will do it: '-type d -or -type f')
Happy Automating!



[ Reply to This | # ]
10.4: Use Automator to remove older files from a folder
Authored by: DrDoak on Oct 11, '07 11:46:37AM

Hmm, you know what? I'm following up b/c this is my first time using Automator and I am JUST NOW discovering that Automator... isn't really automatic at all!! WHY THE HELL would you release this product without incorporating a simple scheduler, maybe just an interface for crond?!! How the @*#$&! is that useful??? Retarded, retarded, retarded.

Moving on...

1. I see that you can schedule workflows using iCal... must iCal be running for this function to work? Anyone know?

2. Another way to achieve this without a dreaded 'man cron' command is to have your workflow run whenever you log in (i.e. on startup of your computer). You can save the workflow as an APPLICATION and then go into System Pref -> Accounts -> Login Items and add the application you just created to the list, making sure the box next to it is checked.

But like I said, that runs whenever you boot up your computer, not at a certain time of day, change of season, phase of the moon, etc. Also, this works ONLY when you reboot or log in (NOT when waking from sleep!).

Does anyone know of a better way?



[ Reply to This | # ]