A little CLEANING Script I wrote ...

Forum rules
Before you post please read how to get help
User avatar
jeanpaulberes
Level 2
Level 2
Posts: 69
Joined: Thu Jun 04, 2015 7:08 am
Location: Belgium
Contact:

A little CLEANING Script I wrote ...

Postby jeanpaulberes » Sun Jul 30, 2017 4:07 pm

Dear all,
Hereby a little cleaning script I wrote .. It's completely safe (use it already a long time with no problems), feel free to copy and use ...
Don't forget to install the Trash-cli utility to make use of all the features of the script ... Have fun ! By the way, sorry folks, but I use Bash and not Dash !

!/bin/bash

OLDCONF=$(dpkg -l|grep "^rc"|awk '{print $2}')
CURKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
LINUXPKG="linux-(image|headers|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)"
OLDKERNELS=$(dpkg -l|awk '{print $2}'|grep -E $LINUXPKG |grep -vE $METALINUXPKG|grep -v $CURKERNEL)

# Standard Bash Color definitions
LIGHT_RED="\e[91m"
LIGHT_GREEN="\e[92m"
LIGHT_YELLOW="\e[93m"
REVERSE_RED="\e[101m"
DEFAULT="\e[39m"

clear
echo -e $LIGHT_RED"**********************************"$DEFAULT
echo -e $LIGHT_RED"*** Start Cleaning MyLinuxMint ***"$DEFAULT
echo -e $LIGHT_RED"**********************************"$DEFAULT


if [ $USER != root ]; then
echo -e $REVERSE_RED"Error: must be root"
echo -e $REVERSE_RED"Exiting..."$DEFAULT
exit 0
fi

echo -e
echo -e $LIGHT_GREEN"Cleaning apt cache..."$DEFAULT
aptitude -v clean

echo -e
echo -e $LIGHT_GREEN"Removing old config files..."$DEFAULT
sudo aptitude -v purge $OLDCONF

echo -e
echo -e $LIGHT_GREEN"Removing old kernels..."$DEFAULT
sudo aptitude -v purge $OLDKERNELS

echo -e
echo -e $LIGHT_GREEN"Removing thumbnails..."$DEFAULT
rm -v -f ~/.cache/thumbnails/*/*.png ~/.thumbnails/*/*.png
rm -v -f ~/.cache/thumbnails/*/*/*.png ~/.thumbnails/*/*/*.png

echo -e
echo -e $LIGHT_GREEN"Removing logfiles..."$DEFAULT
sudo rm -r -v -f /var/log/*

echo -e
echo -e $LIGHT_GREEN"Removing Temporary Files ..."$DEFAULT
rm -r -v -f /tmp/* &> /dev/null

echo -e
echo -e $LIGHT_GREEN"Removing Browser & Mail Cache (Chromium & FireFox, Evolution) ..."$DEFAULT
rm -r -v -f ~/.cache/chromium
rm -r -v -f ~/.cache/mozilla
rm -r -v -f ~/.cache/evolution

echo -e
echo -e $LIGHT_YELLOW"Emptying the Trash Can using the Trash-cli utility ..."$DEFAULT
trash-empty

echo -e
echo -e $LIGHT_RED"***********************"$DEFAULT
echo -e $LIGHT_RED"*** End-Of-Cleaning ***"$DEFAULT
echo -e $LIGHT_RED"***********************"$DEFAULT
echo -e

User avatar
chrisuk
Level 4
Level 4
Posts: 499
Joined: Thu Jun 12, 2008 6:16 am

Re: A little CLEANING Script I wrote ...

Postby chrisuk » Sun Aug 06, 2017 3:55 am

Just a note: you can delete everything in ~/.cache no need for separate lines - so rm -rf ~/.cache/* will do the job. Also /tmp is cleaned on reboot, so no real need to worry about it. More importantly; why would you want this sudo rm -r -v -f /var/log/*? You'd be deleting all of your log files!

I'll let others comment on the rest of the script.
Chris

Manjaro MATE - Antergos MATE - MX Linux - LMDE MATE

User avatar
Pjotr
Level 18
Level 18
Posts: 8981
Joined: Mon Mar 07, 2011 10:18 am
Location: The Netherlands (Holland)
Contact:

Re: A little CLEANING Script I wrote ...

Postby Pjotr » Sun Aug 06, 2017 4:48 am

In addition to chrisuk: for deleting log files, or rather restricting their size, it's better to use logrotate. Sometimes you need a log for debugging.

Furthermore, it's advisable to use purge-old-kernels (part of the byobu set) for deleting old kernels, because it'll leave one spare older kernel. You should always have one older, tried and tested kernel in reserve.

In general, I don't like such scripts. Demolition should be done carefully, step by step, and scripted cleaning is too much like a wrecking ball....
Tip: 10 things to do after installing Linux Mint 18.3 Sylvia
Keep your Linux Mint healthy: Avoid these 10 fatal mistakes
Twitter: twitter.com/easylinuxtips
All in all, horse sense simply makes sense.

User avatar
Termy
Level 3
Level 3
Posts: 170
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: A little CLEANING Script I wrote ...

Postby Termy » Fri Sep 08, 2017 4:16 pm

I would agree with the log thing. No way I would ever delete all those logs like that. Logs are key if you're compromised or trying to troubleshoot, as mentioned. Once logs get to a certain size, you could make them .bak then crawl them now and then to delete outdated logs.

However, at least IMO, it's not needed. You could just compress older logs -- since it's only plain text, compression does wonders. I have a log consisting of over 6200 lines which takes up only 392K, and that's not compressed. With gzip, it's down to a mere 32K.

I can imagine a situation when clearing /tmp could be useful: if it's an always-on machine, or oft-on.
Here to help.

User avatar
WharfRat
Level 20
Level 20
Posts: 10205
Joined: Thu Apr 07, 2011 8:15 pm

Re: A little CLEANING Script I wrote ...

Postby WharfRat » Fri Sep 08, 2017 5:11 pm

I would agree with not touching the logs.

It shouldn't be necessary but /usr/sbin/logrotate -f /etc/logrotate.conf should be used instead.
Image ImageImage

User avatar
jeanpaulberes
Level 2
Level 2
Posts: 69
Joined: Thu Jun 04, 2015 7:08 am
Location: Belgium
Contact:

Re: A little CLEANING Script I wrote ...

Postby jeanpaulberes » Wed Oct 11, 2017 8:34 am

Dear all,
Script has been adapted in such a way that the user decides (with Y/N) answer if he wants the logfiles be cleaned !!!

Code: Select all

!/bin/bash
 
OLDCONF=$(dpkg -l|grep "^rc"|awk '{print $2}')
CURKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
LINUXPKG="linux-(image|headers|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)"
OLDKERNELS=$(dpkg -l|awk '{print $2}'|grep -E $LINUXPKG |grep -vE $METALINUXPKG|grep -v $CURKERNEL)

# Standard Bash Color definitions
LIGHT_RED="\e[91m"
LIGHT_GREEN="\e[92m"
LIGHT_YELLOW="\e[93m"
REVERSE_RED="\e[101m"
REVERSE_OFF="\e[49m"
DEFAULT="\e[39m"

clear
echo -e $LIGHT_RED"**********************************"$DEFAULT
echo -e $LIGHT_RED"*** Start Cleaning MyLinuxMint ***"$DEFAULT
echo -e $LIGHT_RED"**********************************"$DEFAULT

 
if [ $USER != root ]; then
  echo -e $REVERSE_RED"Error: must be root"
  echo -e $REVERSE_RED"Exiting..."$DEFAULT
  exit 0
fi

echo -e
echo -e $LIGHT_GREEN"Cleaning apt cache..."$DEFAULT
aptitude -v clean

echo -e
echo -e $LIGHT_GREEN"Removing old config files..."$DEFAULT
sudo aptitude -v purge $OLDCONF
echo -e
 
echo -en $REVERSE_RED
read -n1 -p "Remove the old Kernels [Y/N]? " RemoveKernel
case $RemoveKernel in
    Y | y)  echo -e $REVERSE_OFF
            echo -e $LIGHT_GREEN"Removing old kernels..."$DEFAULT
            sudo aptitude -v purge $OLDKERNELS
            echo -e $LIGHT_GREEN"Old Kernels Removed..."$DEFAULT;;

    N | n)  echo -e $REVERSE_OFF;;

    *)      echo -e $REVERSE_OFF;; 
esac

echo -e $LIGHT_GREEN"Removing thumbnails..."$DEFAULT
rm -v -f ~/.cache/thumbnails/*/*.png ~/.thumbnails/*/*.png
rm -v -f ~/.cache/thumbnails/*/*/*.png ~/.thumbnails/*/*/*.png
echo -e

echo -en $REVERSE_RED
read -n1 -p "Removing/Clearing the LogFiles [Y/N]? " ClearLogs
case $ClearLogs in
    Y | y)  echo -e $REVERSE_OFF
            echo -e $LIGHT_GREEN"Removing logfiles..."$DEFAULT
            sudo rm -r -v -f /var/log/*
            echo -e $LIGHT_GREEN"Log Files Cleared/Removed..."$DEFAULT;;

    N | n)  echo -e $REVERSE_OFF;;

    *)      echo -e $REVERSE_OFF;; 
esac
 
echo -e $LIGHT_GREEN"Removing Temporary Files ..."$DEFAULT
rm -r -v -f /tmp/* &> /dev/null

echo -e
echo -e $LIGHT_GREEN"Removing Browser & Mail Cache (Chromium & FireFox, Evolution) ..."$DEFAULT
rm -r -v -f ~/.cache/chromium
rm -r -v -f ~/.cache/mozilla
rm -r -v -f ~/.cache/evolution

echo -e
echo -e $LIGHT_GREEN"Disable Saving Cache on Exit ..."$DEFAULT
rm -r -v ~/.cache/sessions/*

echo -e
echo -e $LIGHT_YELLOW"Emptying the Trash Can using the Trash-cli utility ..."$DEFAULT
trash-empty

echo -e
echo -e $LIGHT_RED"***********************"$DEFAULT
echo -e $LIGHT_RED"*** End-Of-Cleaning ***"$DEFAULT
echo -e $LIGHT_RED"***********************"$DEFAULT
echo -e
read -p "Press [Enter] key to finish/Quit..."
clear
Last edited by WharfRat on Wed Oct 11, 2017 10:51 am, edited 1 time in total.
Reason: Added code tag

User avatar
Termy
Level 3
Level 3
Posts: 170
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: A little CLEANING Script I wrote ...

Postby Termy » Wed Oct 11, 2017 12:32 pm

That's awesome.

BUG: Error on line 84, though: "trash-empty" is not found. Was it supposed to point to a now-removed function?
BUG: There's also an error where it's looking for a user directory "mint", when you need $USER or use $HOME for /home/$USER.
BUG: Error on line 1 too; the shebang should read:#!/bin/bash
Here to help.


Return to “Scripts & Bash”