I am working on a project to automatically delete specific directories which are older than a day. I have got the following piece of code working:
find Directory/ -type d -name "Directory.To.Delete.*" -mtime +1 -exec rm -rf {} \;
It runs fine and deletes the directory (and contents) as expected, however it always ends with an error:
find: cannot chdir to Directory/ : No such file or directory
Is there a way to run this code without facing this error? I don’t understand why this code is trying to chdir after deleting the directory.
Add
-prunebefore-exec; that will stopfindfrom entering matches directories: