I have a backup folder with directories of mysql backup


  • 2022-08-20
  • ...
  • 2022-09-12

I am running a crontab job to make the backups and then trying to delete the oldest directories, but keeping 14 of them.

This gives me all the directories modified in the last 14 days

find backup/ -type d -mtime +14

I had tried exec rm()

find backup/ -type d -mtime +14 -exec rm {}

This gives rm: cannot remove ‘backup/2022-08-29’: Is a directory

So I tried the -delete option

find backup/ -type d -mtime +14 -delete

But this gives this error find : cannot delete 'backup/2022-08-29': Directory not empty

How can I do both at the same time ?

  • 2
    Try: find backup/ -type d -mtime +14 -exec rm -r {} +. The problem is that you have non-empty directories, so, the command rm will fail. With rm -r it removes the directories and their contents recursively. Commented Sep 13, 2022 at 23:42
  • I had tried -r before, but without the +. All good now. Commented Sep 14, 2022 at 0:06
  • Did you solve your problem?
    – JRichardsz
    Commented Sep 18, 2022 at 14:30
  • Yes, with the line given by @EdgarMagallon Commented Sep 18, 2022 at 22:49


You must log in to answer this question.

Browse other questions tagged .