0

I am currently configuring crontab to delete all files and directories within the path shown every hour.

My current solution is two lines so I was hoping someone could show me how to make this into one line instead. At the moment, the second command removes the complete directory as well. I would like to keep the complete directory and only delete all directories within complete.

What am I doing wrong? Should there be no slash after complete? /*?

To delete files older than 60 minutes in directory:

0 */1 * * * find /root/Downloads/complete/ -type f -mmin +60 -delete

To delete directories older than 60 minutes within directory:

0 */1 * * * find /root/Downloads/complete/ -type d -mmin +60 -exec rm -r "{}" \;

3 Answers 3

2

You probably meant

0 */1 * * * find /root/Downloads/complete/* -type d -mmin +60 -exec rm -r "{}" \;

because in your example, find will match on the directory-name (and everything under it), but with a wildcard, the shell will expand that to everything (except dot-names) under the directory.

0
0

Use this one line only instead.

 0 */1 * * * find /root/Downloads/complete/ \( -type f -o -type d \) -mmin +60 -delete

Will delete only files and directories under /root/Downloads/complete/ path with modified aging of since an hour ago.

0
find /root/Downloads/complete/ -type d -mmin +60 -exec rm -r "{}" \;

This removes a directory and all of its contents if the directory itself hasn't been modified for an hour, even if files in that directory or in subdirectories have been modified recently. This is probably not what you want.

The modification time on a directory is probably not that useful. I suggest that you delete old files, then directories as they become empty. To avoid deleting the root directory, pass -mindepth 1.

cd /root/Downloads/complete && find . -type f -mmin +60 -delete && find . -depth -mindepth 1 -type d -empty -delete

Note that this could be disruptive, for example you might remove a directory that has just been created and that another program was using. Deleting things based on a timeout is rarely a good idea. If you need to delete things, you should do it when the files have been processed to your satisfaction, rather than after an arbitrary delay.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .