Bash – Deleting all folders older than a specified date, except some of them

bashdatedirectory

i had to do a script job in bash to delete all directory in a path which are older than a specified date except some of them.
i know the name of the directories which shouldn't be deleted…

Can you help me???

i'm sorry but i'm a beginner…

lot of thanks!

Best Answer

This must be my find day.

find /yourpath -mmin +60 -type d -not \( -name dirname1 -o -name dirname2 \) -print0 | xargs -0 rm -r

will find and delete all directories older than 60 minutes (adapt accordingly) which are not named dirname1 or dirname2. You can extend this list with additional -o name dirname parts. Also, I would strongly recommend to replace the rm -r part with echo for testing.

Related Topic