A little CLEANING Script I wrote ...

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

A little CLEANING Script I wrote ...

Post by 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 !

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"
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 5
Level 5
Posts: 581
Joined: Thu Jun 12, 2008 6:16 am

Re: A little CLEANING Script I wrote ...

Post by 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 - MX Linux - LMDE MATE

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

Re: A little CLEANING Script I wrote ...

Post by 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 19 Tara
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 5
Level 5
Posts: 762
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: A little CLEANING Script I wrote ...

Post by 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.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/channel/UCfp-lN ... naEE6NtDSg
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife

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

Re: A little CLEANING Script I wrote ...

Post by 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.
ImageImage

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

Re: A little CLEANING Script I wrote ...

Post by 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 5
Level 5
Posts: 762
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: A little CLEANING Script I wrote ...

Post by 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.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/channel/UCfp-lN ... naEE6NtDSg
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife

dhdurgee
Level 4
Level 4
Posts: 295
Joined: Thu Jul 02, 2009 7:56 pm

Re: A little CLEANING Script I wrote ...

Post by dhdurgee » Wed Mar 14, 2018 12:50 pm

A few questions regarding this script before I consider using it. This script appears similar to one I found here:

https://community.linuxmint.com/tutorial/view/373

There were two points brought out in comments by another user:
Pjotr
Don't use old scripts without research: much has changed since 2011/2012. Some objections to this script:

It removes kernels. You should only remove kernels by means of the kernel tool in Update Manager (a tool that wasn't around five years ago, when this script was created).

It uses aptitude. Don't mix the use of aptitude and apt-get. They probably use different databases, so they might come to different conclusions about what's safe to delete and what not. You've already used apt-get when using Synaptic and Update Manager....
Can you address these concerns and modify the script appropriately, assuming that Pjotr is correct in his assertions?

Dave

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

Re: A little CLEANING Script I wrote ...

Post by Pjotr » Wed Mar 14, 2018 1:06 pm

I advise to treat the tutorials in the community section with as much caution as any other how-to. They aren't being checked for quality and are often simply outdated....
Tip: 10 things to do after installing Linux Mint 19 Tara
Keep your Linux Mint healthy: Avoid these 10 fatal mistakes
Twitter: twitter.com/easylinuxtips
All in all, horse sense simply makes sense.

dhdurgee
Level 4
Level 4
Posts: 295
Joined: Thu Jul 02, 2009 7:56 pm

Re: A little CLEANING Script I wrote ...

Post by dhdurgee » Wed Mar 14, 2018 1:13 pm

Pjotr wrote:
Wed Mar 14, 2018 1:06 pm
I advise to treat the tutorials in the community section with as much caution as any other how-to. They aren't being checked for quality and are often simply outdated....
That was what I gathered from your comment in the community section. The script that is given in this thread appears to be similar to the one you commented on, so I assume the same points you raise there are applicable here.

I would appreciate your guidance in modifying the script to address these issues, replacing the aptitude calls with the equivalent apt-get calls and utilizing the update manager to address the kernels.

Dave

User avatar
thx-1138
Level 5
Level 5
Posts: 959
Joined: Fri Mar 10, 2017 12:15 pm
Location: Athens, Greece

Re: A little CLEANING Script I wrote ...

Post by thx-1138 » Wed Mar 14, 2018 1:17 pm

...for the record, sudo rm -r -v -f /var/log/* should never be used...it can lead to various problems (folders residing there should NOT be deleted). So...
Pjotr wrote:
Sun Aug 06, 2017 4:48 am
Demolition should be done carefully, step by step, and scripted cleaning is too much like a wrecking ball...
(repeat in chorus) :)

dhdurgee
Level 4
Level 4
Posts: 295
Joined: Thu Jul 02, 2009 7:56 pm

Re: A little CLEANING Script I wrote ...

Post by dhdurgee » Wed Mar 14, 2018 5:04 pm

I must concur that it makes more sense to approch this in pieces as opposed to scripted, although it would be a good idea to simply list the various safe steps that can be taken to clean up a root partition that is getting space warnings.

After looking at the man page I decided to run:

sudo apt-get clean

This freed up a bit over 3G on the root partition, so that is probably sufficient at this time. Looking at my /boot directory I see less than 500M in use, so even were I to eliminate a number of the older kernels there the savings are not even close to that obtained by cleaning with apt-get.

Dave

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

Re: A little CLEANING Script I wrote ...

Post by Termy » Wed Mar 14, 2018 5:24 pm

Yeah, when I do some cleaning, I just run the above mentioned command, hsh (alias: https://github.com/terminalforlife/bash ... liases#L88), and whenever I remove packages, I have them autoremove-ing (alias: https://github.com/terminalforlife/bash ... liases#L62) as well (plus purged). I also tend to use roks (https://github.com/terminalforlife/roks) to ensure I only have one kernel installed and in use, although I wouldn't recommend that for someone not at least a fair bit experienced with Linux. By default, /tmp gets blasted away during a reboot, so I don't need to do anything with that, and it's a 4GB tmpfs (RAM Disk, essentially) anyway.
Here to help.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/channel/UCfp-lN ... naEE6NtDSg
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife

User avatar
trytip
Level 7
Level 7
Posts: 1664
Joined: Tue Jul 05, 2016 1:20 pm

Re: A little CLEANING Script I wrote ...

Post by trytip » Thu Mar 15, 2018 12:21 am

small heads up in XFCE Desktop using this script. .cache/sessions/xfce4-session-YOU:0 gives errors in .xsession-errors if deleted. i usually delete everything in .cache and if i mistakenly delete .cache/sessions/xfce4-session-YOU:0 i create a blank text and name xfce4-session-YOU:0 <<< YOU is the hostname
Image

Post Reply

Return to “Scripts & Bash”