Kaptain K
Kaptain K

Reputation: 77

Cron to delete folders older than required time deletes parent folder

I have a cron job that creates folders within the "backup" directory \tmp\backup.

I am looking to have a second job to delete folders within "backup" which are older than 1 minute using the job below

55 19 * * * find /tmp/backup/ -maxdepth 1 -type d -mmin +1 -execdir rm -rf {} \;

But this job deletes the parent directory "backup" too, I am confused on where I am going wrong. Any help is appreciated !

Upvotes: 0

Views: 559

Answers (1)

lprent
lprent

Reputation: 119

Easy enough to test.

for a in {1..3}; do mkdir -p /tmp/backup/${a}; done
find /tmp/backup/ -maxdepth 1 -type d -mmin +1

This returned

/tmp/backup
/tmp/backup/2
/tmp/backup/1
/tmp/backup/3

But

find /tmp/backup/* -maxdepth 1 -type d -mmin +1

returned

/tmp/backup/2
/tmp/backup/1
/tmp/backup/3

Add a asterix

Upvotes: 3

Related Questions