shell find -delete "directory not empty"
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; ...
Related videos on Youtube
grandd
Updated on September 18, 2022Comments
-
grandd almost 2 years
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?
-
alexises over 8 yearsAs a complement, I prefer use xargs for that purpose. pipe your command with
xargs rm -rv
-
grandd over 8 yearsThank 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 ';'
-
Jason Rush over 8 years@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).
-
killjoy almost 7 yearsThanks @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/…
-
petersohn over 6 yearsThe problem with
-exec rm -r {}
is thatfind
tries doesn't know that the command deletes the file, so it throws an error when it tries to recurse into the directory. -
Peter over 3 yearsYou can avoid
find
deleting the folder before the file with the-depth
option. Replace-delete
with-exec rm -rv {} + -depth
-
mpen over 3 yearsLimit the depth to 1;
rm -r
is already recursive. e.g.find dist -mindepth 1 -maxdepth 1 -exec rm -r -- {} +