bash

Clean out old items from your Downloads folder using find & crontab

I got a bit lazy when it came to cleaning out my downloads folder. Old .DMG files, installer files and other detritus were bloating my Download folder so that the stack view in my Dock was becoming a pain to use.

I decided to adopt a bash script we run in the office at home to aid me in my quest for a tidy Downloads folder! By leveraging the find command in bash, a user can create a script that can find any files in a directory that match certain criteria, e.g. type, file name and date created/modified.

In my case, once found, the files will be deleted. Instead of using -mtime to determine age, you can also use -atime or -ctime. I'll leave it up to yourselves to read about that though! For a downloads folder and maybe for different file types,  -atime may be more appropriate for some of you. However, since I'm dealing with installer files, -mtime will suffice!

Once the script has been created, apply to your user crontab at whatever interval you so desire!

#!/bin/bash
#
#       Delete files older than 21 days in Downloads.
#
echo "Deleting (M)PKG, DMG and ZIP files that are older than 21 days in Downloads."
 
# Delete all (M)PKG's older than 21 days
find ~/Downloads/ -name "*.pkg" -type f -mtime +21 -delete
find ~/Downloads/ -name "*.mpkg" -type f -mtime +21 -delete
 
# Delete all DMG's older than 21 days
find ~/Downloads/ -name "*.dmg" -type f -mtime +21 -delete
 
# Delete all ZIP's older than 21 days
find ~/Downloads/ -name "*.zip" -type f -mtime +21 -delete