HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

crontab entry to delete old files/folders error

 

crontab entry to delete old files/folders error

I am trying to automate the delete of a backup directory that is 14 days old
this is the crontab entry I am trying.

find /backup_to_disk/some_dir/ -depth -mtime +14 -exec sh -c 'test -d "$1" && rmdir "$1" || rm "$1"' {} {} \;

This is the error I am receiving
rmdir: /backup_to_disk/some_dir/11042010/prod/bakdir: Directory not empty
rm: /backup_to_disk/some_dir/11042010/prod/bakdir directory

Any ideas?
3 REPLIES
Steven Schweda
Honored Contributor

Re: crontab entry to delete old files/folders error

> Any ideas?

Plain "rm" won't delete a directory. "rmdir"
won't delete a non-empty directory.

man rmdir

man rm

Look for "-r"?

> [...] test -d [...]

man find

Look for "-type"?
James R. Ferguson
Acclaimed Contributor

Re: crontab entry to delete old files/folders error

Hi:

The case of the error on a non-empty directory (from 'rmdir') is the result of 'find' having found a directory that meets the criteria but yet none of its contents (files) satisfy the criteria.

It might be better to use a two-step process where you first remove *files* ('-type f') and then, in a second pass attempt to remove directories ('-type d') ignoring non-empty errors.

Regards!

...JRF...

Re: crontab entry to delete old files/folders error

>JRF: in a second pass attempt to remove directories ('-type d') ignoring non-empty errors.

Right, the first pass may cause the directory to be modified and 14 more days must go by before you finally remove that. But that shouldn't take up too much space.

Or if you assume only new files are added, the directory is always modified at the same date so you can just use rm -rf.