Can't see anything obvious. Doubt your problem is related to unlink. Putting more tracing in your code listing all files in the foreach loop (before and after the -M test) should clarify what is going on.
Be aware that there are some significant differences in unlink semantics between Unix and Windows:
In reply to Re: Unlink works in windows, not in Linux?
by eyepopslikeamosquito
in thread Unlink works in windows, not in Linux?
by jagexCoder
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |