Skip to main content Logo

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

Debian packages clean up commands

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

❗ This post is over fourteen years old. It may no longer be up to date. Opinions may have changed.

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

Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:

I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.

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

Here are some commands to sort files by size:

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