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: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?

Glorfindel
  • 815
  • 2
  • 10
  • 19
grandd
  • 939
  • 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 Jan 27 '24 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 Jan 29 '24 at 04:49

1 Answers1

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

Aura
  • 103
Jason Rush
  • 1,908
  • 1
  • 12
  • 12
  • 16
    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 https://unix.stackexchange.com/questions/23576/how-do-i-recursively-delete-directories-with-wildcard – killjoy Sep 07 '17 at 15:22
  • 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 Jan 27 '18 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 Feb 11 '21 at 06:45
  • Limit the depth to 1; rm -r is already recursive. e.g. find dist -mindepth 1 -maxdepth 1 -exec rm -r -- {} + – mpen Mar 14 '21 at 04:39
  • Does not -prune do the same as -depth in this case? – felipecrs Jun 29 '22 at 18:09