This question already has an answer here:

I try to delete backup files on my Synology NAS older than 30 days. The files are in a directory which is created during download of the backup files from my webserver.
After download, I want to check -mtime and then delete the older files.

Here comes the script:


## Datum auslesen
datum=`date +%Y-%m-%d_%H-%M`

## Mit wget die Datei AutoBackupDB-1.zip laden und in einem Ordner mit Datum uns Uhrzeit speichern 
wget -m -P /volume1/Austauschordner/backup_xyz/$datum/ ftp://backup:backup@domain.tdl/AutoBackupDB-1.zip
echo "Backup von xyz wurde erstellt! "

## finde alle Ordner in einem angegebenen Verzeichnis mit dem Suchnamen die älter als die angegebenen Tage sind und lösche diese
find /volume1/Austauschordner/ -type d -name 'backup_*' -ctime +30 -delete

If I run this script it tells me that the "directory isn't empty" and nothing will be deleted.

Is there an easy way or an option to delete everything in the directory?

marked as duplicate by Stephen Kitt, terdon Dec 15 '15 at 15:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


As @Stephen Kitt mentions, this is largely a duplicate of find -delete does not delete non-empty directories which states that you're telling it to delete directories, but the directories aren't empty (just like running rm some_nonempty_directory doesn't work without the -r flag at a minimum).

That being said, if you replace -delete with -exec rm -rv {} + or -exec rm -rv {} \; then your script should delete the directory recursively without error (remove the v flag if you do not want verbose output, after testing).

Note: the + at the end will result in rm -rv file1 file2 ... while action of \; will be rm -rv file1; rm -rv file2; ...

  • 4
    As a complement, I prefer use xargs for that purpose. pipe your command with xargs rm -rv – alexises Dec 15 '15 at 15:16
  • Thank you guys for your help, i tested this now : DiskStation> find /volume1/Austauschordner/backup_files/ -type d -mmin +200 -exec rm -rv {}+; Error: find: -exec CMD must end by ';' – grandd Dec 15 '15 at 16:45
  • @grandd There should be a space between the {} and the + sign. I would also put a space between the + sign and the semicolon, or get rid of the semicolon, just in case (not 100% sure on that one). – Jason Rush Dec 15 '15 at 17:37
  • Thanks @Alexis, thats the only solution that worked finally (CentOS7) - I wanted to us it but was thrown off by Wildcard's comment in unix.stackexchange.com/questions/23576/… – killjoy Sep 7 '17 at 15:22
  • 1
    The problem with -exec rm -r {} is that find tries doesn't know that the command deletes the file, so it throws an error when it tries to recurse into the directory. – petersohn Jan 27 '18 at 18:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.