Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > How to? Cron job to automatically delete 14-day old files

How to? Cron job to automatically delete 14-day old files
Thread Tools
Mac Enthusiast
Join Date: Jan 2001
Location: Leesburg, Virginia
Status: Offline
Reply With Quote
Apr 27, 2004, 04:43 PM
 
I am looking for a way to let people use a folder on our server for intermediate storage. However, I'd like to set it up so that all files that have been left in there for 14 days get deleted by a script, some code or whatever. Then I can integrate such code into a Cron job to have the deletion take place daily.

Dominik Hoffmann
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Apr 27, 2004, 09:22 PM
 
Originally posted by DominikHoffmann:
I am looking for a way to let people use a folder on our server for intermediate storage. However, I'd like to set it up so that all files that have been left in there for 14 days get deleted by a script, some code or whatever. Then I can integrate such code into a Cron job to have the deletion take place daily.

Dominik Hoffmann
Easy enough. Put this in your crontab:

Code:
0 0 * * * find /path/to/storage/folder -ctime +14 -print -exec rm {} \; > /var/log/delete.log 2>&1
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Mac Enthusiast
Join Date: Jan 2001
Location: Leesburg, Virginia
Status: Offline
Reply With Quote
Apr 27, 2004, 10:47 PM
 
Originally posted by Arkham_c:
Easy enough. Put this in your crontab:

Code:
0 0 * * * find /path/to/storage/folder -ctime +14 -print -exec rm {} \; > /var/log/delete.log 2>&1
Yes, this should be easy, but I don't understand it all, as I am trying to parse this.[list=1][*]"0 0 * * *" is for the crontab[*]"find" is the program that does all the work[*]The search is limited to "/path/to/storage/folder".[*]"-ctime +14" makes the expression return only files that have a modify date stamp that is 14 days older than the time when "find" is being invoked.[*]"-print" prints the pathname of the found file to Standard Output.[*]"-exec rm {} \;" does the actual deletion, but I don't understand the argument to "rm".[*]"> /var/log/delete.log" puts the path of the deleted file into "delete.log".[*]"2>&1" does something I don't understand.[/list=1]
Arkham_c, if you can elucidate, I would appreciate it very much. I will also look in my book on the bash shell. Or, does crontab have nothing to do with bash?

Dominik Hoffmann
     
Mac Enthusiast
Join Date: Jan 2001
Location: Leesburg, Virginia
Status: Offline
Reply With Quote
Apr 27, 2004, 11:54 PM
 
Ad 6 of my previous post:

The find man page says it all. It explains the -exec option as follows
-exec utility [argument ...];

True if the program named utility returns a zero value as its exit status. Optional arguments may be passed to the utility. The expression must be terminated by a semicolon (``;''). If the string ``{}'' appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file. Utility will be executed from the directory from which find was executed. Utility and arguments are not subject to the further expansion of shell patterns and constructs.
"{}" gets filled in with the result of the find operation.
     
Forum Regular
Join Date: Nov 1999
Status: Offline
Reply With Quote
Apr 27, 2004, 11:58 PM
 
Originally posted by DominikHoffmann:
Yes, this should be easy, but I don't understand it all, as I am trying to parse this.[list=1][*]"0 0 * * *" is for the crontab[*]"find" is the program that does all the work[*]The search is limited to "/path/to/storage/folder".[*]"-ctime +14" makes the expression return only files that have a modify date stamp that is 14 days older than the time when "find" is being invoked.[*]"-print" prints the pathname of the found file to Standard Output.[*]"-exec rm {} \;" does the actual deletion, but I don't understand the argument to "rm".[*]"> /var/log/delete.log" puts the path of the deleted file into "delete.log".[*]"2>&1" does something I don't understand.[/list=1]
Arkham_c, if you can elucidate, I would appreciate it very much. I will also look in my book on the bash shell. Or, does crontab have nothing to do with bash?

Dominik Hoffmann
The {} after the rm command is filled in with the results from the find command. If the find command found two files named foo.txt and bar.txt then those two files get placed in the {} making the command rm foo.txt bar.txt. I'd love to take credit for this next part but I found it by doing a google search for 2>&1.

Shell redirection, and the "2>&1"

That simply means to send stderr to wherever stdout is pointing. An easy way to demonstrate all this is to create a direcory and put one file in it:
mkdir tt
cd tt
touch t

That gives us a known situation to work with. Sitting in that directory, ls foo t > y

Puts "t" in "y", but displays "ls: foo: No such file or directory" on the screen. That's because ls writes what it doesn't understand to stderr (file descriptor 2). If we want both things in y, we just do:
ls foo t >y 2>&1
http://www.apple.com/macosx/feedback/
Apple doesn't read these message boards. If you have a complaint or suggestion for OS X send feedback where it will do some good.
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Apr 28, 2004, 09:03 AM
 
Originally posted by DominikHoffmann:
Yes, this should be easy, but I don't understand it all, as I am trying to parse this.[list=1][*]"-exec rm {} \;" does the actual deletion, but I don't understand the argument to "rm".[*]"2>&1" does something I don't understand.[/list=1]
Arkham_c, if you can elucidate, I would appreciate it very much. I will also look in my book on the bash shell. Or, does crontab have nothing to do with bash?
You and uochris have covered these two points correctly. You don't technically need the "-print" and the "delete.log" parts, but I thought you'd want a record of what was deleted.

As to bash, it's only an issue on OSX, because the default shell (/bin/sh) is really bash. On most classical UNIX systems you write cron to work with the original Bourne shell rather than bash.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:34 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2