Do you have your root filesystem on a small SSD? are you working on an embedded system, maybe a Raspberry Pi on a 2 GB SD card? Sooner or later we need to have a look at what is chewing up our space. Here is a quick reference.
Basic maintenance
There is a couple of things to do when we want to free space in a no-brainer way. First, we want to remove those deb packages that get cached every time we do apt-get install.
1 |
apt-get clean |
Also, the system might keep packages that were downloaded as dependencies but are not needed anymore. We can get rid of them with
1 |
apt-get autoremove |
If we want things tidy, we must know that whenever we apt-get remove a package, the configuration will be kept in case we want to install it again. In most cases we want to use apt-get purge. To clean those configurations from removed packages, we can use
1 |
dpkg --list | grep "^rc" | cut -d " " -f 3 | xargs --no-run-if-empty sudo dpkg --purge |
So far we have not uninstalled anything. If now we want to inspect what packages are consuming the most space, we can type
1 |
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n |
Entering the danger zone
The next step would be to inspect our filesystem and see where the space is wasted. This is were we start breaking things, so more care is needed, proceed at your own risk!
The most basic way of discovering this from the command line is to use du
1 2 3 4 5 6 7 8 9 10 11 |
# du -sc /etc/* | sort -n | tail -10 28 default 32 cron.daily 36 security 40 iproute2 40 mysql 44 pam.d 52 apt 52 init.d 640 apache2 1432 total |
If we want a little eye candy, we can install durep
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# durep -td 1 /etc # or -td 2 [ /etc 319.4K (60 files, 43 dirs) ] 90.2K [######## ] 28.26% apache2/ 27.5K [## ] 8.62% apt/ 23.7K [## ] 7.43% mime.types 19.7K [# ] 6.18% cron.daily/ 18.7K [# ] 5.87% services 18.0K [# ] 5.64% security/ 14.7K [# ] 4.61% init.d/ 13.2K [# ] 4.13% ssl/ 11.1K [# ] 3.48% ld.so.cache 9.0K [ ] 2.81% locale.gen 7.8K [ ] 2.43% pam.d/ 4.5K [ ] 1.42% default/ 4.3K [ ] 1.35% skel/ 2.9K [ ] 0.92% locale.alias |
If we find some big file and we want to know what package it belongs to, we can do
1 |
dpkg -S <file> |
, also we quite often ask ourselves what files does a package install. We can list those with
1 |
dpkg-query -L <pkg> |
Some other locations can be using up a lot of space as execution byproducts. We might find huge logs in /var/logs , or maybe lots of cached things in /var/cache. It is generally ok to get rid of those.
If we have gotten this far, we can see that much of the space left is used by icons, fonts and locales. We can investigate what icons and fonts we do not need. This will vary from system to system, but generally we will have them at /usr/share/fonts and /usr/share/icons, maybe ~/.local/share/fonts .
List installed fonts with
1 |
fc-list |
We can investigate which one our X server uses looking at $XDG_CONFIG_HOME/fontconfig/fonts.conf , the X server logs or Xorg.conf
The way we installed icons will depend on our setup and the particular desktop environment that we using. Again, see the X server logs.
At this point, the biggest part is taken by locales. A safe and clean way to get rid of unused locales is localepurge .This tool get rid of locales that we do not use, as well as localized packages and man pages.
1 2 3 4 5 6 |
# dpkg-reconfigure localepurge # localepurge localepurge: Disk space freed in /usr/share/locale: 26160 KiB localepurge: Disk space freed in /usr/share/man: 1304 KiB Total disk space freed by localepurge: 27464 KiB |
More exotic stuff
Aside from cleaning up our desktop, we might need to use these basic tools for more advanced purposes where space requirements are really tight.
We might be interested in things like creating really tiny docker containers to test out LAMP systems, or putting together a small chroot to access a full fledged debian system on a non-debian system like OpenWRT or Android.
In those cases we really do not need the kernel, drivers, boot, systemd, bash, man pages, nor many other things so you can literally destroy the distro as long as the libraries and binaries are there, and you have access to apt and dpkg.
We will do some of these things in the next posts.
References
25 Useful Basic Commands of APT-GET and APT-CACHE for Package Management
I would also recommend looking at deborphan, it can help get rid of unwanted libraries and packages.
“aptitude purge ~c” will also purge any removed package with config files remaining. “aptitude search ~c” will list them.