dirk.ndrvn.nl

Auto-delete older files from Download and Desktop folders

I often use ~/Downloads and ~/Desktop as scratch folders, placing files that I need for a short time there. To keep these from filling up too much, and keep them easily accessible for finding fresh stuff, I added these lines to my .zshrc file:

## find all files and directories in ~/Downloads that have not been accessed in the last day and delete them
find ~/Downloads -mindepth 1 \( -type f -or -type d \) -atime +0 -delete
## find all files and directories on the Desktop that have not been accessed in the last week and delete them
find ~/Desktop -mindepth 1 \( -type f -or -type d \) -atime +7 -delete

Let’s see how these work out!


Update 2024-10-15: Added the -mindepth 1 filter, to avoid having the folders themselves removed.

#Tech #Shell