Delete All Files in a Directory Except One on every sunday 20 hour

Solution 1:

The !(pattern) syntax is part of the extended globbing capabilities of bash and this isn't on by default. You need to make sure that your script is using bash and not sh, and you need to enable the relevant option. So make your autodelete.sh file look like this:

#!/bin/bash

shopt -s extglob
rm -rf /home/user/Public/!("VIP Folder")

The quotes are optional in this case, but don't hurt either and are a good habit to have.

Also, your crontab line will run this command every day at 20:00 and not every Sunday only. To run it at 20:00 on Sundays, use:

0 20 * * 0 /home/autodelete.sh