Web Hosting Talk







View Full Version : Help with CronJob - Please!


telmehow
05-18-2008, 10:36 PM
Hi Guys,
Can really use your help in getting a cronjob working. I have a reseller account (Hsphere), under which I am running a FTP server on a Linux account.
Here are the requirements
Under ftp root I have the DIR_SP, Dir A, Dir B, Dir C…….
I want to delete any files / sub directories older than 14 days, but would like to skip one special director (DIR_SP), also I want all the files and sub-directories under the other directories deleted, but not the directory themselves (in other words, delete all the files and sub-dirs under DirA, but not the DirA itself). Here is what I have so far and it works:

find . -name DIR_SP -prune -o -mtime +14 -exec rm -rf {} \;

but for some reason, I cannot get the other part working (delete all the files and sub-dirs under DirA,DirB, DirC… but not the directory itself). If I execute this command, it will delete any old directory as well (DirA, DirB…). I tried,

find . -name DIR_SP -prune -o -mtime +14 -depth +1 -exec rm-rf {} \;

but adding the ‘-depth +1’ gives me a find error (find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]).

Thanks in advance.

SM

isurus
05-21-2008, 10:07 AM
Hello,

find . -mindepth 1 -maxdepth 1 -type d -not -name DIR_SP -exec echo rm -rf {}/* \;I suggest leaving the "echo" in there until you're sure that find is only picking up exactly what you want it to ;)

You'll also need to add the 14 day thing back in when you're happy that this works as required.


Incidentally, if you have a massive number of sub directories to clean out, you will see a big speed increase by also using xargs.


HTH,

Simon

telmehow
05-21-2008, 10:28 AM
thanks, will try this out and report back on the results.

isurus
05-21-2008, 11:37 AM
Thinking about it, if there are a lot of files in the directories that are to be emptied (ie enough to exceed the command line buffer size) then you will need to use xargs anyway.

jstanden
05-21-2008, 05:52 PM
I love find as much as the next guy, but you should see if your server has the tmpwatch or tmpreaper commands installed. They're made to do those kinds of clutter cleanups on directories from crons.

telmehow
05-23-2008, 04:40 PM
interesting, I did not know about tmpwatch or tmpreaper commands, will see if they can be used, but I believe, I will still need to answer the two requirements. How to skip one particular dir and more importantly how to not delete the root dir structure, only the sub-dirs and files.

blueroomhosting
05-23-2008, 07:53 PM
I'm not sure that removing directories which have not been modified in the last 14 days is quite what you want. For instance file updates might not modify their containing directory (depending on how the FTP server works).

How about splitting this up into two passes. First limit the find match with "! -type d" to take away just old files. Then remove empty ("-empty") directories ("-type d"), using the "-mindepth" option so you don't remove the toplevel ones you want to keep.

I think mindepth is what you were asking about anyway, rather than depth which you tried but I don't think exists.

Jim

jstanden
05-23-2008, 08:29 PM
I think mindepth is what you were asking about anyway, rather than depth which you tried but I don't think exists.

Yeah, I was tempted to say the same thing. A script doing two passes makes a lot more sense. It'll keep the crontab a lot cleaner too.

-depth does exist as a find parameter, but it doesn't take an argument. It simply reorders the output so nested files are above their directories in the output. That way you aren't deleting a directory using -exec and then iterating over its previously deleted contents.

example# mkdir dira dirb dirc; touch dira/somefile dirb/somefile dirc/somefile

example# find *
dira
dira/somefile
dirb
dirb/somefile
dirc
dirc/somefile

example# find * -depth
dira/somefile
dira
dirb/somefile
dirb
dirc/somefile
dirc