Subscribe

Bash CleanMyMac alternative 🧼

✍️

Quickly removing files using a Bash script

11 Sep, 2020 Β· 1 min read

As you might know by now, I like Bash!

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

I use Carbon to generate these cool previews for many of my articles. 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 use 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';

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 straightforward script, but it can be helpful to find/remove specific 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

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next πŸ“–

Counting all words across markdown files ~ CLI

13 Jun, 2022 Β· 1 min read

Counting all words across markdown files ~ CLI

Bash ~ never forget to git commit again πŸ’₯

4 Sep, 2020 Β· 3 min read

Bash ~ never forget to git commit again πŸ’₯

Join 1990 devs and subscribe to my newsletter