1825782 Members
2134 Online
109687 Solutions
New Discussion

Re: schedule cron

 
Jim92900
Occasional Advisor

schedule cron

I want to schedule a cron job to remove files/Directories older than two days under a particular directory(/tmp/PSFTP). Please help me with this.

Thanks

Jim
6 REPLIES 6
Pete Randall
Outstanding Contributor

Re: schedule cron

01 01 * * 01 /usr/bin/find /tmp/PSFTP -type f -mtime +10 -exec rm {} \;


Pete

Pete
Pete Randall
Outstanding Contributor

Re: schedule cron

Sorry, correction:

01 01 * * 01 /usr/bin/find /tmp/PSFTP -type f -mtime +2 -exec rm {} \;


Pete

Pete
Patrick Wallek
Honored Contributor

Re: schedule cron

You would use find to do the actual removal. Something like:

For files:
find /starting-dir -type f -mtime +2 -exec rm -f {}+

For dirs:
find /starting-dir -type d -mtime +2 -exec rm -f {}+

You can then either put those 2 commands in a script and then schedule that via cron, or you can just use those 2 lines as a command in cron and schedule them separately.

To learn how to schedule in cron I suggest perusing the crontab man page (man crontab).

Regarding the above finds, I would also advise doing a 'exec ll -d {}+' prior so you know how find will behave before you actually do the removal.

Once the file is deleted, you have no recourse for getting it back other than restoring from backup.
James R. Ferguson
Acclaimed Contributor

Re: schedule cron

Hi Jim:

I agree with Patrick insofar as you said "files" and "directories", but for directory removal you should use 'rmdir':

# find /starting-dir -type d -mtime +2 -exec rmdir {} \+

Note that the "+" character terminates the '-exec command' and like the use of the semicolon in Pete's post *must* be escaped for the shell.

The "+" terminator is available in 11.11 and later and is faster than the semicolon (";") terminator because it bundles multiple arguments together and passes the bundle to the command to run rather than spawning one command for each argument. The same goal can be met by piping to 'xargs'.

Regards!

...JRF...
#
Dennis Handly
Acclaimed Contributor

Re: schedule cron

>JRF: But for directory removal you should use 'rmdir':

If you use rmdir, you need to be prepared for non-empty directory errors.

I suppose removing the files, will touch the directory, so they will be empty first. But you could use:
# find /starting-dir -type d -mtime +2 -exec rmdir {} + 2> /dev/null

>Note that the "+" character terminates the '-exec command' and like the use of the semicolon in Pete's post *must* be escaped for the shell.

I asked before, what shell requires "+" to be escaped?? Neither sh nor ksh require it.
James R. Ferguson
Acclaimed Contributor

Re: schedule cron

Hi (again):

Dennis > JRF: But for directory removal you should use 'rmdir': If you use rmdir, you need to be prepared for non-empty directory errors.

Exactly. Depending upon the user's objectives, one may want to remove *files* older than n-days; remove empty directories, too; and leave non-empty ones. The statement of requirements needs to be clearer.

As for my comments regarding escaping the "+" terminator, I base them on the 'find(1)' manpages (either for 11.11, 11.23 or 11.31) that note under the discussion of '-exec cmd':

/* begin quote */

The end of cmd must be punctuated by a semicolon (;) or a plus sign (+) (semicolon and plus are special to the shell and must be escaped).

/* end quote */

I agree, however, that the absence of the escape character, in practice, doesn't seem to matter.

Regards!

...JRF...