Wednesday, April 3, 2013

[Linux/Mac] Always leave your trash occupied, but not full

Some people have a common "bad" practice of deleting items permanently as seeing the Trash (Recycle Bin for poor Windows users) non-empty provokes them! Rule of thumb: don't ever permanently delete your files, the trash is a great feature, so please use it! You never know when you will need those files again.

However, by time your trash size will grow up and take a lot of space of your storage. No, I won't ask you to empty it every now and then. The best practice is to only delete files that are older than a specific date, lets say 30 days.

To be short, this is a little bash script that will calculate the total trash size then deletes those old items and print again the new size:

victim=~/.Trash
echo Calculating $victim size...
echo $victim size: `du -sh $victim | cut -f1`
echo -n "Emptying $victim items older than 30 days, please be patient..."
find $victim -mtime +30d -exec rm -fr {} \; &> /dev/null
echo Done
echo Current $victim size: `du -sh $victim | cut -f1`

You can change the victim to ~/Downloads or whatsoever, you can also change the number of days to weeks or months or whatsoever. Use it at your own risk :) For your convenience I have created 2 scripts, one for Trash, the other for Downloads. Download them to your home and just execute them whenever you run out of space.
./empty-trash-older-than-30d.sh
./empty-downloads-older-than-30d.sh

3 comments:

Adel El-Atawy said...

The next step is to call this script as a daily cron.

Adel El-Atawy said...

The next step is to call this script as a daily cron.

Unknown said...

It doesn't sound nice to me to automatically delete files. I would say, only run it when you need more space, otherwise, they won't harm.