You have 2 free member-only stories left this month.

How to Automate Cleaning up Your Trash and Downloads Directory

Set a cleanup schedule on macOS with a single line of code

Shinichi Okada
Jun 5 · 5 min read
Photo by The Creative Exchange on Unsplash

Introduction

ls ~/Downloads | wc -l
ls ~/.Trash | wc -l
find ~/Downloads -mtime +7 | wc -l | xargs
find ~/.Trash -mtime +7 | wc -l | xargs

Installation

Homebrew

brew tap shinokada/cleanit && brew install cleanit

Awesome package manager

curl -s https://raw.githubusercontent.com/shinokada/awesome/main/install | bash -s install
awesome install shinokada/cleanit
awesome rm cleanit

Manual installation

mkdir ~/awesome
cd ~/awesome
git clone https://github.com/shinokada/cleanit.git
mkdir ~/bin
echo $PATH
/Users/your-username/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
export PATH="/Users/your-username/bin:$PATH"
source ~/.zshrc
echo $PATH
ln -s ~/awesome/cleanit/cleanit ~/bin/cleanit
cleanit -v
0.1.2

How to use Cleanit

cleanit
cleanit -D 10
cleanit -D 10 -f ~/Mydir
cleanit -D 10 -f ~/Mydir -t ~/Anotherdir
cleanit -D 10 -f trash

Update

cleanit downloads
cleanit trash
cleanit -D 5 downloads

Set up auto clean using a Crontab

Full Disk permissions

which bash
/bin/bash
# you may have
/opt/homebrew/bin/bash
System Preferences, Security & Privacy view. Image by Author.
Selecting /bin/bash. Press SHIFT+CMD+. to show hidden files. Image by Author.
You may have /opt/homebrew/bin/bash. Image by the author.
You may have /opt/homebrew/bin/bash. Image by the author.
You may have /opt/homebrew/bin/bash. Image by the author.
cleanit cron -H 10 -M 0 -D 30
cleanit cron -H 10 -M 0 -D 10
cleanit -r

Conclusion

wrote an excellent introduction article, Bash Scripting: Everything you need to know about Bash-shell programming.

mkdir Awesome

A Medium publication. A showcase of App/tools and programming tips.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Start a blog