82

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:

#!/bin/sh

## 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:[email protected]/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?

2
  • This question should be re-opened, because the other one is too much specific to __pycache__, plus this one have better answers and has more votes.
    – kenorb
    Commented Jan 27 at 23:42
  • 2
    @kenorb what do you mean by "too much specific to __pycache__? You could replace that with any directory name in the commands there and it would work just as well. Voting to leave closed.
    – muru
    Commented Jan 29 at 4:49

1 Answer 1

111

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; ...

8
  • 16
    As a complement, I prefer use xargs for that purpose. pipe your command with xargs rm -rv
    – alexises
    Commented Dec 15, 2015 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
    Commented Dec 15, 2015 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
    Commented Dec 15, 2015 at 17:37
  • 7
    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
    Commented Jan 27, 2018 at 18:13
  • 1
    You can avoid find deleting the folder before the file with the -depth option. Replace -delete with -exec rm -rv {} + -depth
    – Peter
    Commented Feb 11, 2021 at 6:45

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