Skip to main content

Raymii.org Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

Debian packages clean up commands

Published: 01-01-2010 | Author: Remy van Elst | Text only version of this article


Table of Contents


As all my servers run on Debian and I like to keep things clean, here are somehandy commands.

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)

Find large packages
dpkg --get-selections | cut -f1 | while read pkg; do dpkg -L $pkg | xargs -I'{}' bash -c 'if [ ! -d "{}" ]; then echo "{}"; fi' | tr '\n' '\000' | du -c --files0-from - | tail -1 | sed "s/total/$pkg/"; done | sort -rn > ~/packages.log.txt

If you run this as root, when its finished you will have a file in /root calledpackages.log.txt which has all the packages from your system in it with the sizeof the package and the files it uses:

15312 perl-modules14192 php5-cgi12588 perl12400 coreutils12396 iso-codes11232 aptitude10684 binutils9916 python2.5 

You can also use something like

dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n 

but this also takes the size of databases and extra files.

Remove config files

If you uninstall stuff with apt-get remove sometimes debian does not removesconfig files and they also take up space. Now you can just use apt-get purge butI tend to forget that every time. This command lists all the packages which areremoved but still have config files on your system:

dpkg --list | grep '^rc 'rc binutils     2.20.1-15           The GNU assembler, linker and binary utilitiesrc dbus         1.2.24-3            simple interprocess messaging systemrc dpkg-dev     1.15.8.5            Debian package development toolsrc erlang-base  1:14.a-dfsg-2       Erlang/OTP virtual machine and base applicationsrc fakeroot     1.14.4-1            Gives a fake root environment

Now, just to make sure check the output and then remove the config files withthis command:

dpkg --list | grep '^rcb' | awk '{ print $2 }' | xargs dpkg -P
Cleanup afterwards

To save up some space via apt get you can use these commands:

apt-get autoremoveapt-get clean 

autoremove removes unused dependencies, packages which were installed by otherpackeges but which are no longer needed by your system. clean just removes allthe packages in the apt cache. You can also use autoclean but clean frees upmore space. This command shows all the packages which are installed on yoursystem because some package recommends it, but they are not actuallydependencies of packages:

aptitude search '?and( ?automatic(?reverse-recommends(?installed)), ?not(?automatic(?reverse-depends(?installed))) )' 

can give something like this:

i A apt-xapian-index    -   maintenance and search tools for a Xapian index of Debian packages i A exim4               -   metapackage to ease Exim MTA (v4) installation i A file                -   Determines file type using "magic" numbers i A heirloom-mailx      -   feature-rich BSD mail(1)  

Check the output and if needed remove them all:

aptitude search '?and( ?automatic(?reverse-recommends(?installed)), ?not(?automatic(?reverse-depends(?installed))) )' | awk '{ print $3 }' | xargs dpkg -P

Here are some commands to sort files by size:

du -h | grep ^[0-9.]*M | sort -rndu -h | grep ^[0-9.]*G | sort -rn
Tags: apt-get, aptitude, cleanup, debian, df, dpkg, size, tutorials, ubuntu