Debian packages clean up commands

01-01-2010 | Remy van Elst

Table of Contents

As all my servers run on Debian and I like to keep things clean, here are some handy commands:

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 called packages.log.txt which has all the packages from your system in it with the size of the package and the files it uses:

15312 perl-modules
14192 php5-cgi
12588 perl
12400 coreutils
12396 iso-codes
11232 aptitude
10684 binutils
9916 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 removes config files and they also take up space. Now you can just use apt-get purge but I tend to forget that every time. This command lists all the packages which are removed but still have config files on your system:

dpkg --list | grep '^rc '

rc binutils     2.20.1-15           The GNU assembler, linker and binary utilities
rc dbus         1.2.24-3            simple interprocess messaging system
rc dpkg-dev            Debian package development tools
rc erlang-base  1:14.a-dfsg-2       Erlang/OTP virtual machine and base applications
rc fakeroot     1.14.4-1            Gives a fake root environment

Now, just to make sure check the output and then remove the config files with this 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 autoremove
apt-get clean 

autoremove removes unused dependencies, packages which were installed by other packeges but which are no longer needed by your system.
clean just removes all the packages in the apt cache. You can also use autoclean but clean frees up more space.
This handy command shows all the packages which are installed on your system because some package recommends it, but they are not actually dependencies 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

Then we have the all time favorites to see sizes:

df -h
du -h | grep ^[0-9.]*M | sort -rn
du -h | grep ^[0-9.]*G | sort -rn

With these commands I keep my servers clean and lean, but if you do something wrong or remove a package like libc6, sysv-rc, apt ore coreutils you'll have to spend your free evening to fix it up :P.

Note that I ran everything as root. If you get strange errors about permissions then try again as root.

Thanks to:
Command Line Fu

Tags: apt-get, aptitude, cleanup, debian, df, dpkg, size, ubuntu,