how to automatically remove files older than 1 day from a given folder?

I have a folder that gets filled up with log files. I'd like to have a script or some other background task that would automatically remove files from that folder when they get to be older than a day or two. What's the easiest way to do that?

Posted on Dec 27, 2012 12:22 PM

Reply
Question marked as Top-ranking reply

Posted on Dec 27, 2012 5:30 PM

You could create an Automator app using "Run Shell Script", then replace the example 'cat' command with


find /the/path/to/your/folder -iname "*.log" -type f -mtime +1 -delete


Save as an application


Now setup an iCal repeating event that has an 'alert' that specifies "Opens file", where you specify the file as your Automator created application.


The Unix 'find' command will find all the files that match the specified criteria.


NOTE: Be very CAREFUL with this command as if you point it at the wrong location it will delete things you do not want, and there is no way to return from a delete via 'find'.


The /the/path/to/your/folder is how you tell 'find' where you want it to start searching.


The -iname "*.log" gives it a file pattern to match. I'm assuming your log files end in .log, but the -iname can use any wildcard pattern to match your log files.


The -type f tells the 'find' command to only select actual files, but ignore directories, symbolic links, devices, FIFOs, etc...


The -mtime +1 tells 'find' to select files which have a modification date at least a day old. Anything less than a day old will be ignored.


The -delete will delete any file that matches the previous selection critieria. This is the DANGEROUS part of the command. WIthout this option, 'find' will just display the files it finds. With the -delete it will delete them without any undo capability.


You should test your 'find' command very carefully WITHOUT the -delete to make sure it is selecting the correct files from the correct directory. Once you are happy, you can add the -delete option.


Message was edited by: BobHarris

8 replies
Question marked as Top-ranking reply

Dec 27, 2012 5:30 PM in response to Michael Levin

You could create an Automator app using "Run Shell Script", then replace the example 'cat' command with


find /the/path/to/your/folder -iname "*.log" -type f -mtime +1 -delete


Save as an application


Now setup an iCal repeating event that has an 'alert' that specifies "Opens file", where you specify the file as your Automator created application.


The Unix 'find' command will find all the files that match the specified criteria.


NOTE: Be very CAREFUL with this command as if you point it at the wrong location it will delete things you do not want, and there is no way to return from a delete via 'find'.


The /the/path/to/your/folder is how you tell 'find' where you want it to start searching.


The -iname "*.log" gives it a file pattern to match. I'm assuming your log files end in .log, but the -iname can use any wildcard pattern to match your log files.


The -type f tells the 'find' command to only select actual files, but ignore directories, symbolic links, devices, FIFOs, etc...


The -mtime +1 tells 'find' to select files which have a modification date at least a day old. Anything less than a day old will be ignored.


The -delete will delete any file that matches the previous selection critieria. This is the DANGEROUS part of the command. WIthout this option, 'find' will just display the files it finds. With the -delete it will delete them without any undo capability.


You should test your 'find' command very carefully WITHOUT the -delete to make sure it is selecting the correct files from the correct directory. Once you are happy, you can add the -delete option.


Message was edited by: BobHarris

Dec 28, 2012 8:04 AM in response to Michael Levin

Why not use Automator to create a "Folder Action" associated with that Folder, then everytime something is changed in the folder, the Folder Action automator script will run, and purge files older than 1 day.


If you have a .scpt, you could maybe select the Folder you want to purge, Right-Click -> Services -> Folder Action Setup... -> <Cancel> to dismiss the Pick List -> [+] and specify your script, or your Automator app.

Dec 28, 2012 8:34 AM in response to BobHarris

> Why not use Automator to create a "Folder Action" associated with that Folder


what is the difference (functionally) between Automator and just having a .scpt (which I downloaded on-line and looks like it will do what I need)?


> Right-Click -> Services -> Folder Action Setup... -> <Cancel> to dismiss the Pick List -> [+]

> and specify your script, or your Automator app.


aha. I was trying to get my file onto the Pick List and couldn't figure out how to do it. If I dismiss it and try to add, my .scpt file is grayed out and I can't add it. What am I doing wrong - what has to be done to the file to make it selectable for this?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

how to automatically remove files older than 1 day from a given folder?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.