Simple Script to Keep Up Your System Clean

ratick
  13 years ago
  19

If you are an geeky user who keeps installing various software and updates then with the passage of time you will feel that your system is messed up. This is a shell script which lets you clean your system by removing the following:

  • apt cache
  • Config file for Uninstalled .deb packages
  • Unused Kernels
First lets discuss the different items which this script removes.
 
APT Cache
 
APT is the package management tool which is used to install/upgrade and remove packages in your Ubuntu system. If you use it often then it’s cache consumes a lot of space. By removing the apt cache you get plenty of free space.
 
Config Files For Uninstalled .Deb Packages
 
Suppose you downloaded a package for any software and it did not work. Many users forget to delete such packages. The Ubucleaner takes care of such packages and throws them out of the system.
 
Unused Kernel
 
There could be more than one kernel present in your system at the same time. But only one will be functional. This script gets rid of all unused kernels to free up system space and keep your system neat and clean.
 
SO, WHERE THE SCRIPT ???

here is it

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)
YELLOW="\033[1;33m"
RED="\033[0;31m"
ENDCOLOR="\033[0m"
 
if [ $USER != root ]; then
  echo -e $RED"Error: must be root"
  echo -e $YELLOW"Exiting..."$ENDCOLOR
  exit 0
fi
 
echo -e $YELLOW"Cleaning apt cache..."$ENDCOLOR
aptitude clean
 
echo -e $YELLOW"Removing old config files..."$ENDCOLOR
sudo aptitude purge $OLDCONF
 
echo -e $YELLOW"Removing old kernels..."$ENDCOLOR
sudo aptitude purge $OLDKERNELS
 
echo -e $YELLOW"Emptying every trashes..."$ENDCOLOR
rm -rf /home/*/.local/share/Trash/*/** &> /dev/null
rm -rf /root/.local/share/Trash/*/** &> /dev/null
 
echo -e $YELLOW"Script Finished!"$ENDCOLOR
#!/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)
YELLOW="\033[1;33m"
RED="\033[0;31m"
ENDCOLOR="\033[0m"
 
if [ $USER != root ]; then
  echo -e $RED"Error: must be root"
  echo -e $YELLOW"Exiting..."$ENDCOLOR
  exit 0
fi
 
echo -e $YELLOW"Cleaning apt cache..."$ENDCOLOR
aptitude clean
 
echo -e $YELLOW"Removing old config files..."$ENDCOLOR
sudo aptitude purge $OLDCONF
 
echo -e $YELLOW"Removing old kernels..."$ENDCOLOR
sudo aptitude purge $OLDKERNELS
 
echo -e $YELLOW"Emptying every trashes..."$ENDCOLOR
rm -rf /home/*/.local/share/Trash/*/** &> /dev/null
rm -rf /root/.local/share/Trash/*/** &> /dev/null
 
echo -e $YELLOW"Script Finished!"$ENDCOLOR
 
open text editor, copy-paste code above, named to mintcleaner.sh and save it to your home folder
 
Then, open terminal, type 
sudo chmod +x mintcleaner.sh
 
Now, run the script with the following command and it will take care of all things that mentioned above
sudo sh mintcleaner.sh
 
following my terminal log, show how it works
arboyz@DynaBook-Portege-2000 ~ $ sudo chmod +x mintcleaner.sh
[sudo] password for arboyz: 
chmod: cannot access `mintcleaner.sh': No such file or directory
arboyz@DynaBook-Portege-2000 ~ $ sudo chmod +x mintcleaner.sh
arboyz@DynaBook-Portege-2000 ~ $ sudo sh mintcleaner.sh
-e Cleaning apt cache...
-e Removing old config files...          
No packages will be installed, upgraded, or removed.
0 packages upgraded, 0 newly installed, 0 to remove and 108 not upgraded.
Need to get 0B of archives. After unpacking 0B will be used.
                                         
-e Removing old kernels...
No packages will be installed, upgraded, or removed.
0 packages upgraded, 0 newly installed, 0 to remove and 108 not upgraded.
Need to get 0B of archives. After unpacking 0B will be used.
                                         
-e Emptying every trashes...
-e Script Finished!
arboyz@DynaBook-Portege-2000 ~ $ sudo chmod +x mintcleaner.sh
[sudo] password for arboyz: 
arboyz@DynaBook-Portege-2000 ~ $ sudo sh mintcleaner.sh
-e Cleaning apt cache...
-e Removing old config files...          
No packages will be installed, upgraded, or removed.
0 packages upgraded, 0 newly installed, 0 to remove and 108 not upgraded.
Need to get 0B of archives. After unpacking 0B will be used.
                                         
-e Removing old kernels...
No packages will be installed, upgraded, or removed.
0 packages upgraded, 0 newly installed, 0 to remove and 108 not upgraded.
Need to get 0B of archives. After unpacking 0B will be used.
                                         
-e Emptying every trashes...
-e Script Finished!
 
hope it useful for you
 
If you found problem, please refer to the original source
Comments
Pjotr 8 years ago

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


draco1 9 years ago

Works excellent!!! Been using for awhile now and no complaints. Thanks for sharing.


Vlad22 9 years ago

worked like a charm on my LM 17.2
Thank you very much!


Mr_Latte 10 years ago

Seems to be working well on Linux Mint 17.1 Cinnamon. Thanks for sharing.


kilham 10 years ago

Worked well, Mint 16.


AlanWalker 10 years ago

Nice script! Good for studying scripts. There was a problem running it on my system however (LMDE-201403 (Cinnamon 64bit)).

A dialog box came up advising me that the script wanted to erase my boot image; when asked if I wanted to do that I said "NO".

The script finished executing and, after several boots and the exercise of several of my most often used applications, everything seems ok.

I'd "... refer to the original source." but that points to a generic Linux post some 3 years old now. The original poster probably has no particulars on Linux Mint.


atlantianking 10 years ago

Worked perfectly. My system needed to remove a few files and I ran it 3x and it ran straight through with nothing to report.


ratick 11 years ago

ourdeaux@lourdeaux-Pavilion-dv5000-EE393AV ~ $ sudo chmod +mintcleaner.sh
chmod: missing operand after `+mintcleaner.sh'
lourdeaux, your parameter is incorrect. the correct one must be $ sudo chmod +x mintcleaner.sh not


ratick 11 years ago

thanks for feedback all. glad if my article helpful


rodeloli 12 years ago

Great job! This is exactly what I was looking for. kudos!


lourdeaux 12 years ago

This didn't work with my text editor, Kate, in Mint 13 KDE. What next, please?
Lee
ourdeaux@lourdeaux-Pavilion-dv5000-EE393AV ~ $ sudo chmod +mintcleaner.sh
chmod: missing operand after `+mintcleaner.sh'
Try `chmod --help' for more information.
lourdeaux@lourdeaux-Pavilion-dv5000-EE393AV ~ $ chmod --help
Usage: chmod [OPTION]... MODE[,MODE]... FILE...
or: chmod [OPTION]... OCTAL-MODE FILE...
or: chmod [OPTION]... --reference=RFILE FILE...
Change the mode of each FILE to MODE.

-c, --changes like verbose but report only when a change is made
--no-preserve-root do not treat `/' specially (the default)
--preserve-root fail to operate recursively on `/'
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--reference=RFILE use RFILE's mode instead of MODE values
-R, --recursive change files and directories recursively
--help display this help and exit
--version output version information and exit

Each MODE is of the form `[ugoa]*([-+=]([rwxXst]*|[ugo]))+'.

Report chmod bugs to bug-coreutils@gnu.org


roht 12 years ago

very nice.


Diogo_Almeida 12 years ago

Works as promised!