DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Cover image for Bash CleanMyMac alternative ๐Ÿงผ
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Bash CleanMyMac alternative ๐Ÿงผ

As you might know by now, I really like Bash!

So far, we have built some fun stuff using Bash like hacking our morning routine and making an after work git check.

For a lot of my articles I use Carbon to generate these cool previews.
Very cool app, but my Downloads folder gets full of these things.

So how can Bash help us CleanOurMacs?

Bash CleanMyMac

Bash clean files

Note: This script can be very dangerous since it uses Bash to remove files. Please be cautious!

We will be using a txt file to loop over files we want to remove.

These files can include a wildcard (*) or start with a specific string:

  • Start with: carbon
  • Wildcard: carbon*.png

Then on to the script!

while read line; do
    find . -name "$line" -exec rm -rf {} \;
done < ~/www/cleanMyPc/files.txt
echo 'All done';
Enter fullscreen mode Exit fullscreen mode

Wow, simple as that right!

We loop through the lines in our files.txt, and for each line we execute the find command it searches for a specific name and calls -exec rm -rf

The rm stands for remove and rf for recursive force.

Running the script

This script initially worked so it would scan all your files, but I adjusted it for security reasons to only scan a specific folder.

  1. So open a Terminal
  2. Navigate to the folder you want: cd Downloads
  3. Execute command ~/path/to/script/bash.sh
  4. Voila, all done

It's a very simple script, but it can be useful to find/remove certain files on your computer.

Find this project on GitHub

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (2)

Collapse
 
waylonwalker profile image
Waylon Walker

Next up

alias cleanmac=~/www/cleanMyPc/bash.sh

Or filter to files older than a few days and add it to your crontab ๐Ÿค”

Collapse
 
dailydevtips1 profile image
Chris Bongers Author

The alias is welcome indeed, crontab not too sure, always a bit "worry" of auto-deleting.
Also, the reason why I deleted it searching in every folder haha.

๐ŸŒš Turn on dark mode

๐Ÿ”  Change your default font

๐Ÿ“š Adjust your experience level to see more relevant content

ย 

Head to your account's Settings to do all this and more.