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!
Systemd version (2026-10-14)
Service unit file:
~/.config/systemd/user/clear-old@.service
[Unit]
Description=Clear files not accessed for days from folder: %i
[Service]
Type=oneshot
ExecStart=/bin/bash -xc 'IFS="-" read -r days dir <<< "%i"; find "$HOME/$dir" -type f,d -atime "+$days" -mindepth 1 -print -delete'
Timer file:
~/.config/systemd/user/clear-old@.timer
[Unit]
Description=Periodically clear old files: %i
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
And enable with
systemctl --user daemon-reload
systemctl --user enable --now clear-old@0-Downloads.timer
systemctl --user enable --now clear-old@7-Desktop.timer
Update 2024-10-15: Added the -mindepth 1 filter, to avoid having the folders themselves removed.
Update 2026-10-14: Now with more systemd