I want a script to clean my system.

  1. Clean unused dependencies.
  2. Clean apt-cache.
  3. Clean residual-config files.
  4. Remove broken packages.
  5. Only keep the latest version of packages and remove other. Specially linux-kernel.

And all that you think that the script should contain. Please guide me. Ubuntu 12.04 with Kernel 3.2.xxxx I exactly don't know.

closed as too broad by mikewhatever, Braiam, Eric Carvalho, qbi, Seth Nov 11 '13 at 16:18

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Synaptic does a good job of cleaning junk from your system. Set the section to Status and if there is junk, one of the options will be autoclean. You can also get rid of old kernels in the 'local or obsolete' section, but I recommend you keep the one before the current. Also, a residual config files option will appear if you have any. From my experience, it's safe and gives you a cancel option if you're unsure. – Christopher Mar 6 '14 at 17:25
  • There are some tools and commands in this thread : ubuntuforums.org/showthread.php?t=140920. – Aurélien Ooms Aug 2 '14 at 9:41

In your favorite editor

apt-get autoremove
apt-get clean
aptitude purge ~c
aptitude -f
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs apt-get -y purge

Make sure you run as root, and you have aptitude installed. Save this file as whatever. However, I do not recommend removing old versions, as sometimes they have legacy items in them. The final line ONLY takes care of linux-kernel (see last sentence why). Even so, I would recommend having multiple kernels just in case one goes BA-ZOINK.


Considering cleaning out old kernelversions, this is something I use regularly:


#keep the most recent 4 kernels and remove the rest.
sudo apt-get purge $( dpkg --list | grep -P -o "linux-image-\d\S+"| head -n-4 )
sudo apt-get autoremove
sudo apt-get clean
sudo update-grub
  • 1
    The first line might not do what you want, be sure not to remove the kernel used by your installation (check with uname -r). – Aurélien Ooms Aug 2 '14 at 10:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.