 |
 |
How to? Cron job to automatically delete 14-day old files
|
 |
|
 |
|
Mac Enthusiast
Join Date: Jan 2001
Location: Leesburg, Virginia
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
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
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|